WITH: naming a step
Lesson 18 of 24 · SQL.Micro
A common table expression (CTE) gives a name to an intermediate result. Long queries become a sequence of readable steps.
WITH totals AS (SELECT …) SELECT … FROM totals
Several CTEs are separated by commas and can use the ones above.
Check yourself
What is the main benefit of WITH?
Using the spent CTE, show the name and total of customers who spent more than 200 (all orders).
SELECT o.customer_id, SUM(i.quantity * i.unit_price) AS total
FROM orders o JOIN order_items i ON i.order_id = o.id
Do it in the browser: a real SQL tool, a short mission and instant feedback. No install.
Start this lesson — free