CTE
CTE is WITH name AS (SELECT …) SELECT … FROM name; A named subquery you can read top-down. MySQL 8+. WITH fail AS (SELECT * FROM students WHERE marks < 40) SELECT COUNT(*) FROM fail;
WITH RECURSIVE walks a tree (org chart). Don’t start there. Start with one WITH as a readable subquery.
Trap — CTE on MySQL 5.7 (not supported). Trap: recursive CTE without a stop condition.
On the example next to this theory: CTE: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Exam tip
WITH fail AS (SELECT … WHERE marks < 40) SELECT * FROM fail;