WITH gives a result a temporary name
Sometimes a query needs a result that doesn't exist as a table yet. WITH lets you build that result first and give it a temporary name — called a common table expression, or CTE — then use that name in the query that follows, exactly as if it were a real table. The name only lives for that one query.
A tiny example
In plain English: Give the order list a temporary name, then show me everything in it.
WITH big_orders AS (
SELECT id, total
FROM orders
WHERE total >= 20
)
SELECT *
FROM big_orders;Query result
| id | total |
|---|---|
| 102 | 24.5 |
| 107 | 31 |
| 110 | 22.75 |
big_orders isn't a real table — it's the named result of the SELECT inside WITH, used just for this one query.
Related SQL lessons
Ready for a break?
A fresh, bite-sized SQL puzzle waits every morning. Tap tiles, see real results. No terminal, no setup, no timers.
Try today's free SQL puzzle