Lesson 6 of 12
Structured learning draftSeparate row filters from group filters
Find customers whose known paid amounts total at least 100. Filter paid rows before grouping, then apply the threshold to each customer's aggregate, so a pending order cannot inflate a paid-sales result.
Learning objectives
- Filter records before grouping.
- Compute one result per customer.
- Filter groups using an aggregate.
Separate row filters from group filters: the working sequence
Trace the three stages of separate row filters from group filters and compare them with the example.
WHERE removes Amina's pending sale before any totals are formed.
GROUP BY combines each customer's remaining paid records.
HAVING retains only groups whose known paid total meets the threshold.
WHERE filters input rows before aggregation. GROUP BY forms customer groups. HAVING filters those groups after the total is calculated. These clauses cannot be interchanged without changing the question.
WITH sales(id, customer, amount, status) AS (
VALUES (1, 'Amina', 120, 'paid'),
(2, 'Amina', 80, 'pending'),
(3, 'Brian', 50, 'paid'),
(4, 'Chen', NULL, 'paid')
)
SELECT customer, SUM(amount) AS paid_total
FROM sales
WHERE status = 'paid'
GROUP BY customer
HAVING SUM(amount) >= 100
ORDER BY customer;customer | paid_total
Amina | 120Try it, then change it
- Remove WHERE and explain why Amina's total becomes 200.
- Change the threshold to 50 and predict the included customers.
- Add a second paid sale for Brian and check how grouping changes the result.
Common mistakes
- Including pending rows in a paid-total report.
- Trying to use a group aggregate as a row-level WHERE condition.
- Grouping by a display name when distinct customers can share it in real data.
Practice activity
Apply the lesson
Return customers with at least two paid orders using COUNT(*) in HAVING. State how a missing amount affects an order count compared with an amount total. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · databases
A join returns more rows than expected.
- Find the cause
- Count matching records for each join key in both tables. A key repeated on both sides creates multiple combinations.
- Try this
- Decide the intended row grain first: one row per customer, order or item. Aggregate at that grain or correct the join condition; do not hide the problem with DISTINCT.
- Check the fix
- Use two customers and three orders as a fixture. Predict every result row and check unmatched records separately.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: databases with SQL
Watch Querying or Relating to see selection and joins explained using actual tables. Examples begin with SQLite; check syntax when using PostgreSQL.
Try it yourself: Draw two small tables, predict the joined rows, then compare your prediction with the query result.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
Where does a condition on SUM(amount) belong in this grouped query?
Lesson summary
- WHERE filters records before aggregation.
- GROUP BY defines the result grain.
- HAVING applies conditions to computed groups.
Sources and further reading
- Aggregate functions in grouped queriesPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.