WITH gives a named result to a query, making multi-step logic easier to read.
WITH gives a named result to a query, making multi-step logic easier to read. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.
WITH customer_totals AS (SELECT customer_id, SUM(amount) AS revenue FROM orders GROUP BY customer_id) SELECT customer_id, revenue FROM customer_totals WHERE revenue > 2500 ORDER BY revenue DESC;
Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.
In your own words, explain the main SQL feature in today’s lesson and why it answers the question.
Recall prompt: Name the aggregate CTE customer_totals and query it outside. Close this page section mentally and explain the query from memory before proceeding.
Use a CTE to calculate customer totals, then keep totals above 2500.
Name the aggregate CTE customer_totals and query it outside.