Which city brings in the most money?
Scroll, and one query grows from a lazy first look into the answer.
Sample data, 12 made-up orders
-
Everyone starts here.
SELECT * hands you every column of every row. Fine for a first look, useless as an answer. Nobody asked for all of it.
-
Ask for the columns you need.
The question is about cities and money, so that is all we keep. Less on screen, less to get wrong.
-
Filter the rows.
WHERE drops the orders that were cancelled. They are crossed out here so you can see what the filter removed.
-
Group, then count.
GROUP BY folds the rows into one line per city. COUNT and SUM tell you how many orders each city placed and what they were worth.
-
Put the answer on top.
ORDER BY sorts by revenue, biggest first. Five lines of SQL, and the table now answers a question a manager would ask.