Join related tables before grouping when a summary needs attributes from both.
Join related tables before grouping when a summary needs attributes from both. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.
SELECT c.name, SUM(o.amount) AS revenue FROM customers c JOIN orders o ON o.customer_id = c.customer_id GROUP BY c.customer_id, c.name 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: Join, group by customer identity and name, then SUM. Close this page section mentally and explain the query from memory before proceeding.
Show each customer's name and total revenue, highest first.
Join, group by customer identity and name, then SUM.