Write down what one output row means before you read any SQL
The routine's first step happens before you look at any SQL.
Predict
What do you write down before you read a line of the generated SQL?
Take this question: how many customers ordered in each city in March? Before you read a line of SQL, turn it into a contract:
Part
For this question
One output row is
a city
Which cities
those with a March order (or every city, with 0? Ask.)
The number
distinct customers with at least one order
March
placed_at from March 1 at 00:00, up to but not including April 1
Edges
a customer with two orders counts once; an order at 23:59 on March 31 counts
The bugs in this round are gaps between the question and the query. You can't see a gap until the question is written down as exactly as the query.
The first line is the grain: what one output row means. It does the most work. Every aggregate has to give one number per grain row, and every join has to keep one row per thing you count. When the query's grain drifts from the question's, a count comes out too high or a group goes missing, and nothing errors.