Customers who never had a refund
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
Return the names of customers who placed at least one order and never had an order with the status refunded.
customers (8 rows)
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Anna | NL | 2024-01-05 |
| 2 | Bruno | BR | 2024-02-11 |
| 3 | Chloe | US | 2024-03-02 |
| 4 | Dev | IN | 2024-03-19 |
| 5 | Elif | TR | 2024-05-27 |
| 6 | Farah | US | 2024-06-14 |
| 7 | Gus | NL | 2024-08-01 |
| 8 | Hana | JP | 2024-09-09 |
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.
What this exercise teaches
NOT EXISTS is the safest way to say no row matches: it ignores NULLs and never duplicates rows. A plain JOIN with status <> 'refunded' would wrongly keep customers who have both paid and refunded orders.
A hint if you get stuck
EXISTS for at least one order, NOT EXISTS for the refunded ones. Both subqueries refer to c.id from the outer query.
Background you need
Common mistakes on this exercise
- Keeps customers who have a refund next to other orders.
- Includes customers who never ordered.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Joins
- Orders with customer names
- Customers who never ordered
- Revenue per country
- Everyone with a department
- Employee and manager
- Revenue per product
- Revenue per category
- Products never sold
- Headcount per department
- Orders per customer
- Most recent order per customer
- Products per order
- Customers from the same country
- Employees who manage nobody
- Categories per customer
- Payroll per department
- Direct reports per manager
- Stack two lists with UNION ALL
- Keep both sides with a full join
- Products on each order as one text (string_agg)
- Customers whose every order is paid (bool_and)
- Revenue inflated by a join (fan-out)
- Not equal, and the rows with NULL
- Overlapping bookings
- Compare two tables (EXCEPT)