Practice Questions
Fifteen questions against a toy e-commerce schema. Load the seed, write your query, then open the solution and check it against the expected output.
Each question has a naive answer that looks right and is wrong. The solution says which one.
For the reference material behind these, see the SQL Cheatsheet.
The dataβ
customers β orders β order_items β products.
| table | columns |
|---|---|
customers | id, name, email, country, created_at |
products | id, name, category, price |
orders | id, customer_id β customers, status (pending/paid/refunded), metadata jsonb, created_at |
order_items | id, order_id β orders, product_id β products, quantity, unit_price |
Revenue = quantity * unit_price from order_items, paid orders only. Not products.price β that's today's price, unit_price is the price at sale. Total paid revenue is 8450.00; if a join fans out, your number exceeds it.
Two rows from each, to show the shape β the rest is in seed.sql.
customers β 6 rows, countries SG / MY / IN / DE. Emails are <firstname>@example.com.
| id | name | country | created_at | |
|---|---|---|---|---|
| 1 | Amara Okafor | amara@example.com | SG | 2026-08-02 |
| 4 | Divya Rao | divya@example.com | IN | 2026-09-02 |
products β 8 rows across furniture, electronics, pantry.
| id | name | category | price |
|---|---|---|---|
| 1 | Aeron Chair | furniture | 1200.00 |
| 5 | 27" Monitor | electronics | 400.00 |
orders β 11 rows dated JuneβSeptember 2026: 9 paid, 1 refunded, 1 pending.
| id | customer_id | status | metadata | created_at |
|---|---|---|---|---|
| 10 | 4 | paid | {"source":"web"} | 2026-09-03 |
| 5 | 1 | refunded | {"source":"web"} | 2026-08-01 |
order_items β 18 rows, 1β2 per order. unit_price happens to match products.price throughout.
| id | order_id | product_id | quantity | unit_price |
|---|---|---|---|---|
| 16 | 10 | 2 | 2 | 800.00 |
| 17 | 10 | 6 | 1 | 60.00 |
Three edge cases the questions hunt for: one customer never orders, one product appears only in the pending order, and orders 5 and 11 are not revenue.
To run the queries for real, load seed.sql into any Postgres 15+:
docker run --rm -e POSTGRES_PASSWORD=pg -p 5432:5432 -d postgres:16
psql postgres://postgres:pg@localhost:5432/postgres -f seed.sql
No Docker? Paste it into DB Fiddle on PostgreSQL 15 β everything runs except question 15's EXPLAIN.
Joinsβ
1. Customers who have never orderedβ
Return name, email. One row, any order.
Solution
select c.name, c.email
from customers c
left join orders o on o.customer_id = c.id
where o.id is null;
| name | |
|---|---|
| Farid Hassan | farid@example.com |
The anti-join pattern is LEFT JOIN β¦ WHERE right.pk IS NULL. NOT EXISTS is equally good and usually plans identically:
select name, email from customers c
where not exists (select 1 from orders o where o.customer_id = c.id);
Trap: where c.id not in (select customer_id from orders) returns zero rows the moment that subquery yields a single NULL β x NOT IN (1, NULL) evaluates to NULL, never true. orders.customer_id is NOT NULL here so it happens to work, but the habit breaks the first time you point it at a nullable column. Default to NOT EXISTS.
2. Products that have never soldβ
"Sold" means appearing in a paid order. Return id, name, category. One row.
Solution
select p.id, p.name, p.category
from products p
where not exists (
select 1 from order_items i
join orders o on o.id = i.order_id and o.status = 'paid'
where i.product_id = p.id
);
| id | name | category |
|---|---|---|
| 8 | Tea Sampler | pantry |
Trap: dropping the status condition asks a different question β "never appeared in any order" β and returns zero rows, because the Tea Sampler does sit in order 11. That order is pending, so nobody has paid for it. The status filter belongs inside the EXISTS; a product with only unpaid orders must still count as unsold.
3. Revenue per paid orderβ
Return order_id, customer, total, ordered by total descending. Nine rows.
Solution
select o.id as order_id, c.name as customer, sum(i.quantity * i.unit_price) as total
from orders o
join customers c on c.id = o.customer_id
join order_items i on i.order_id = o.id
where o.status = 'paid'
group by o.id, c.name
order by total desc;
| order_id | customer | total |
|---|---|---|
| 10 | Divya Rao | 1660.00 |
| 9 | Chen Wei | 1600.00 |
| 1 | Amara Okafor | 1380.00 |
| 3 | Ben Tan | 1200.00 |
| 6 | Divya Rao | 890.00 |
| 4 | Chen Wei | 800.00 |
| 7 | Ben Tan | 550.00 |
| 2 | Amara Okafor | 270.00 |
| 8 | Elena Fischer | 100.00 |
Nine rows summing to 8450.00. Orders 5 and 11 are absent.
Trap: joining order_items multiplies the order row by its line-item count. Anything you aggregate off the orders row itself β say sum(o.shipping_fee) β is now counted once per item, so a two-item order double-counts its shipping. Aggregate line items in a subquery before joining anything else onto the order.
4. Top 3 customers by paid revenueβ
Return customer, revenue, ordered by revenue descending. Three rows.
Solution
select c.name as customer, sum(i.quantity * i.unit_price) as revenue
from customers c
join orders o on o.customer_id = c.id and o.status = 'paid'
join order_items i on i.order_id = o.id
group by c.id, c.name
order by revenue desc
limit 3;
| customer | revenue |
|---|---|
| Divya Rao | 2550.00 |
| Chen Wei | 2400.00 |
| Ben Tan | 1750.00 |
Without the LIMIT you'd also see Amara Okafor 1650.00 and Elena Fischer 100.00 β five rows, not six, because Farid has nothing to join to.
Group by c.id, not just c.name β two customers can share a name and would silently merge into one row. Once c.id (the primary key) is in the GROUP BY, Postgres lets you select any other customers column without listing it.
5. Revenue per customer, including customers with noneβ
Return customer, revenue, ordered by revenue descending. Every customer appears, so six rows; non-buyers show 0.00, not NULL.
Solution
select c.name as customer, coalesce(sum(i.quantity * i.unit_price), 0) as revenue
from customers c
left join orders o on o.customer_id = c.id and o.status = 'paid'
left join order_items i on i.order_id = o.id
group by c.id, c.name
order by revenue desc;
| customer | revenue |
|---|---|
| Divya Rao | 2550.00 |
| Chen Wei | 2400.00 |
| Ben Tan | 1750.00 |
| Amara Okafor | 1650.00 |
| Elena Fischer | 100.00 |
| Farid Hassan | 0.00 |
Trap: move o.status = 'paid' into the WHERE clause and you silently convert the LEFT JOIN into an inner one β Farid's row has o.status IS NULL, which fails the predicate, and you're back to five rows. A filter on the right table of a LEFT JOIN belongs in ON; a filter on the left table belongs in WHERE.
The coalesce matters too: sum() over zero rows is NULL, not 0. Without it Farid shows a blank, and order by puts NULL first by default in descending order.
Aggregationβ
6. Order counts per status, in one rowβ
Return exactly one row with three columns: paid, refunded, pending.
Solution
select
count(*) filter (where status = 'paid') as paid,
count(*) filter (where status = 'refunded') as refunded,
count(*) filter (where status = 'pending') as pending
from orders;
| paid | refunded | pending |
|---|---|---|
| 9 | 1 | 1 |
FILTER reads better than sum(case when β¦ then 1 else 0 end) and beats three separate scans of the table for speed β one pass, three counters.
Trap: count(status = 'paid') counts 11, not 9. count(x) counts non-NULL inputs, and false is not NULL. The case version needs else null (or sum(β¦ else 0)) to be correct.
7. Countries with more than one paying customerβ
Return country, paying_customers. One row.
Solution
select c.country, count(distinct c.id) as paying_customers
from customers c
join orders o on o.customer_id = c.id and o.status = 'paid'
group by c.country
having count(distinct c.id) > 1;
| country | paying_customers |
|---|---|
| SG | 2 |
Without the HAVING you'd get four rows: DE 1, IN 1, MY 1, SG 2. Note MY shows 1, not 2 β Farid is Malaysian but never ordered, so the inner join drops him.
WHERE filters rows before grouping; HAVING filters groups after.
Trap: count(*) instead of count(distinct c.id) counts order rows, giving SG 4, MY 2, IN 2, DE 1 β and MY passes the > 1 test on the strength of one customer's two orders.