Skip to content
mentoor

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?

Choose one answer

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:

  1. FROM and JOIN. Which tables are involved, and how do they link? An INNER JOIN keeps only rows that match on both sides, so customers with no orders vanish. A LEFT JOIN keeps every row from the left table and fills gaps with NULL.
  2. 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 as created_at <= '2026-10-31' stops at midnight at the start of the 31st and misses that day's orders.
  3. GROUP BY. What is one output row? One per customer, per month, per country?
  4. 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.
  5. HAVING, ORDER BY, LIMIT. HAVING filters groups after counting. A LIMIT 10 silently 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?

Choose one answer

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?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic