🗒 Cheat Sheet← Lesson
BitWithBite
SQL Worksheet

🗃 CTEs & WITH Clause

Chapter: SQL · Level ★☆☆ · Time: 30 min
SCORE___ / 20
NameClassDate
After this worksheet you can
Write WITH ... AS (...)Chain multiple CTEsImprove readabilityReference a CTE like a table
📚 Quick Recap

A Common Table Expression (CTE) is a named temporary result set defined with the WITH keyword. CTEs make complex queries dramatically more readable — you break one large query into labeled, logical steps that you can reference multiple times.

🧠 Section A · Concept Check ● BEGINNER 4 × 1 = 4

1A CTE starts with:
2A CTE behaves like a:
3Multiple CTEs are separated by:
4Main benefit of CTEs:

🧮 Section B · Problem Solving ● INTERMEDIATE 2 + 3×3 = 11

5WITH totals (SELECT ...) names the block.
6After defining, you SELECT ... FROM .
7Rewrite: average of per-user order totals, using a CTE.
8Chain two CTEs (sales, top) - sketch the shape.
9CTE vs subquery in one line?

🚀 Section C · Challenge ● CHALLENGE 5

10Can you JOIN a CTE?
💭 Reflection — the most useful thing I learned:
A ___/4   B ___/11   C ___/5   Total ___/20 Teacher's Signature Parent's Signature
✂ answer key — fold or cut before handing out

1-A   2-A   3-A   4-A  |  5 AS   6 the CTE name   7 = WITH t AS (SELECT user_id, SUM(total) s FROM orders GROUP BY user_id) SELECT AVG(s) FROM t;   8 = WITH sales AS (...), top AS (SELECT ... FROM sales) SELECT * FROM top;   9 = Same idea, but the CTE is named up front and easier to read/reuse  |  10 = Yes - treat it exactly like a table

📄 Need offline practice?Download the print-ready PDF or open the one-page cheat sheet.
⬇ Worksheet PDF 🗒 Cheat Sheet