SQL HAVING practice problems
WHERE filters rows before they are grouped, HAVING filters the groups afterwards. Questions that say more than three orders, at least two customers or a total above a threshold are HAVING questions.
Each problem here needs a condition on an aggregate, and several need both a WHERE and a HAVING in the same query, which is the combination interviewers like to ask about.
Try one here: Repeat buyers
Try it yourself
Return the customer_id of every customer who placed more than two orders, whatever the status.
orders (first 12 of 14 rows)
| id | customer_id | order_date | amount | status |
|---|---|---|---|---|
| 101 | 1 | 2025-01-03 | 120 | paid |
| 102 | 1 | 2025-01-20 | 80.5 | paid |
| 103 | 2 | 2025-01-22 | 200 | paid |
| 104 | 3 | 2025-02-02 | 45 | refunded |
| 105 | 3 | 2025-02-15 | 310 | paid |
| 106 | 4 | 2025-02-18 | 150 | pending |
| 107 | 5 | 2025-03-01 | 60 | paid |
| 108 | 6 | 2025-03-09 | 95 | paid |
| 109 | 6 | 2025-03-30 | 130 | paid |
| 110 | 6 | 2025-04-11 | 75 | paid |
| 111 | 7 | 2025-04-12 | 210 | paid |
| 112 | 1 | 2025-04-25 | 40 | paid |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
Aggregation
- Repeat buyersReturn the customer_id of every customer who placed more than two orders, whatever the status.
- Customers over a spending thresholdReturn the customer_id and the total paid amount of every customer whose paid orders add up to more than 300.
- Active in several monthsFor customers who ordered in at least two different months, return the customer_id and the number of different months they ordered in. Count every order, whatever the status.
Joins
- Customers whose every order is paid (bool_and)Return the names of customers who have at least one order and whose orders all have the status paid.
Interview classics
- Departments above averageReturn the dept_id and average salary of every department whose average salary is above the average salary of all employees. Ignore employees without a department when grouping.
Work mode
- Find duplicate accountsFrom Sam in Support: "Some people signed up more than once with the same email, typed differently (capitals, extra spaces). List each email that has more than one account, in lowercase with no spaces, and how many accounts it has. Most accounts first, then alphabetically." Return email and accounts.
- Signed up, never paidFrom Marta in Marketing: "Which people signed up but never made a paid payment? Treat emails as the same person whatever the capitals or spaces, and count a person as paid if any of their accounts has a paid payment. One row per email, in lowercase." Return email.
Case studies
- Payroll review, step 5: managers and their teamsClose the review with the span of control. For every manager with at least two direct reports, return the manager's name, the number of direct reports, the total salary of those reports and their average salary rounded to a whole number.
Read first if you are stuck
Other practice sets
Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.