Topic 2 of 5 · 4 min read
Reading joins, filters and aggregates
Goal: Read an AI-written query and say in plain words which rows it counts and which it leaves out.
Read a generated query in the order the database runs it, and finish by saying in one plain sentence which rows it counts. You do not need to write SQL to do this. You need to find five clauses and ask what each one keeps.
Check yourself · 1 of 3
A query does FROM customers c INNER JOIN orders o ON o.customer_id = c.id. Which customers appear in the result?
Why it matters
An assistant gives you a query and a number. If you cannot read the query, the number is a claim you cannot check. Most wrong answers come from three places: a join that drops or repeats rows, a filter that keeps the wrong rows, and an aggregate that counts the wrong thing. Each is visible in the text.
How to do it
Read in this order, not top to bottom:
- FROM and JOIN. Which tables are involved, and how do they link? An
INNER JOINkeeps only rows that match on both sides, so customers with no orders vanish. ALEFT JOINkeeps every row from the left table and fills gaps with NULL. - WHERE. This filter runs before grouping. Check each condition against your definition: is
status = 'paid'there? Does the date range include the last day? A filter such ascreated_at <= '2026-10-31'stops at midnight at the start of the 31st and misses that day's orders. - GROUP BY. What is one output row? One per customer, per month, per country?
- The aggregate.
COUNT(*)counts rows.COUNT(DISTINCT customer_id)counts different customers.SUM(total)adds a column. After a join, rows may repeat, so these give different numbers. - HAVING, ORDER BY, LIMIT. HAVING filters groups after counting. A
LIMIT 10silently cuts the list.
Then write the plain sentence: "This counts distinct customers with at least one paid order in October, and leaves out customers with none."
Before and after
Before: "The query looks fine, it has a JOIN and a GROUP BY."
After: "It joins orders to customers (inner, so orders without a customer are dropped), keeps status = 'paid', groups by country and sums total_eur. It leaves out refunded orders only if refunds have a different status, which I need to check."
Naming what is left out is the skill. A query that looks right is not yet read.
What goes wrong
- Fan-out. Join orders to order lines, then sum order totals: each order total is repeated once per line, so revenue inflates.
- Hidden inner joins. A LEFT JOIN followed by a WHERE on the right table's column turns back into an inner join and drops the unmatched rows.
- Counting rows when you meant people.
COUNT(*)over joined rows counts orders, not customers.
Ask the assistant to "explain this query line by line and say which rows are excluded", then compare with your own reading.
Examples
One query, read clause by clause
SELECT c.country, COUNT(*) AS customers
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
AND o.created_at >= '2026-10-01'
AND o.created_at < '2026-11-01'
GROUP BY c.country;
Plain reading: one row per country; counts paid orders placed in October, not customers; customers with no paid October order do not appear. If the question was "how many customers", COUNT(DISTINCT c.id) is the fix. The date range uses < '2026-11-01', which correctly includes all of 31 October.
Questions to ask any generated query
Which table is the left one, and does every row from it need to appear? Is each filter in WHERE or in the join condition? What is one output row? Could the join repeat a row? Is there a LIMIT I did not ask for?
Key points
- Read the query in execution order: FROM and JOIN, WHERE, GROUP BY, the aggregate, then HAVING, ORDER BY and LIMIT.
- An inner join drops rows with no match, while a left join keeps them with NULL in the missing columns.
- COUNT(*) counts rows and COUNT(DISTINCT x) counts different values, which differ whenever a join repeats rows.
- Joining a parent table to a child table repeats parent values, so summing a parent column after the join inflates it.
- Finish by stating in one sentence what the query counts and what it leaves out.
Common mistakes
- Skimming the SELECT list and trusting the query because it has the right column names.
- Not noticing that an inner join silently removes customers or orders that have no match.
- Treating COUNT(*) as a count of customers or users when joins can repeat them.
- Missing an inclusive-or-exclusive date edge, so the last day of the range is partly or fully cut off.
Check yourself · 2 of 3
A query joins orders to order_lines and runs SUM(o.total_eur). An order with 3 lines is worth 90 EUR. What does this order add to the sum?
Check yourself · 3 of 3
A query ends with WHERE created_at <= '2026-10-31', and created_at is a timestamp. What does it do to orders placed on 31 October?
Practice items, not counted towards your mastery.
Start here
- SQLBolt: interactive SQL lessons (opens in a new tab)CourseFree
Short free exercises on SELECT, WHERE, JOIN and aggregates: enough to read what an assistant wrote.
- BIRD text-to-SQL benchmark (opens in a new tab)PaperFree
A benchmark built on large, messy real databases, with a public leaderboard that shows how far tools still are from perfect.
- Spider 2.0: enterprise text-to-SQL workflows (opens in a new tab)PaperFree
A benchmark of enterprise-sized schemas and multi-step tasks, showing why real warehouses are harder than demos.
More for this topic
- PostgreSQL joins and table expressions (opens in a new tab)DocsFree
Defines inner and outer joins precisely, which is what you need to say which rows a query keeps.