SQL practice · intermediate

Solved SQL exercises on JOIN and aggregation

Ten exercises on JOIN, GROUP BY and HAVING, each with the solution, the output it really produces and the mistake people make most often.

Updated: October 18, 202610 exercises with verified results
In this guide

How to use this guide

These SQL exercises practise what people find hardest once they move beyond simple queries: joining tables without losing or duplicating rows, and summarising data by group. They all use the fictional StoreSQL database, and every result shown comes from running the solution against it.

Try to solve each task in the Editor SQL before opening the solution. You can also download the SQLite database. The tables are customers (id, name, email, city, active), products (id, name, category, price, stock), orders (id, customer_id, date, status, total), order_items (order_id, product_id, quantity, unit_price) and categories (id, name). Amounts are rounded to two decimal places and use a full stop as the decimal separator, as SQL does.

1. Count the orders in each status

Beginner · GROUP BY

Your challenge

Show how many orders there are in each status, from the status with the most orders to the one with the fewest.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 1
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
ORDER BY orders DESC, status;

Verified result (3 rows):

statusorders
shipped9
pending6
cancelled3

Each group is one status and COUNT(*) counts the rows in that group. The second key in the ORDER BY fixes the order when two statuses tie.

Common mistake: Selecting another column without grouping by it, such as total, raises an error in PostgreSQL and returns an arbitrary value in SQLite.

2. Add up the amount per customer, excluding cancelled orders

Intermediate · JOIN and WHERE

Your challenge

Show each customer's name with the total of their non-cancelled orders, from highest to lowest.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 2
SELECT c.name, ROUND(SUM(p.total), 2) AS amount
FROM customers AS c
JOIN orders AS p ON p.customer_id = c.id
WHERE p.status <> 'cancelled'
GROUP BY c.id, c.name
ORDER BY amount DESC, c.name;

Verified result (10 rows):

nameamount
Sara Green1112
Amy Wilson377.4
Hugh Miller268
Liam Martin253.4
Megan Reed215.9
Laura Ward148.9
Emily Fox98
David Clark69.9
Nia Smith61.5
Jack Wood54

The WHERE removes cancelled orders before adding up. We group by c.id and by name so that two customers with the same name are not merged.

Common mistake: Grouping by name only. If two customers share a name, their amounts are added into one row.

3. Find the customers who have never placed an order

Intermediate · LEFT JOIN

Your challenge

List the customers with no orders, sorted by name.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 3
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS p ON p.customer_id = c.id
WHERE p.id IS NULL
ORDER BY c.name;

Verified result (2 rows):

name
Miles Lee
Peter Hill

LEFT JOIN keeps every customer. Those with no order get missing values in the columns from orders, and p.id IS NULL picks them out.

Common mistake: Write p.id = NULL. A comparison with NULL is never true, so the query would return no rows.

4. Count orders per customer, including those with zero

Intermediate · LEFT JOIN and COUNT

Your challenge

Show every customer with their number of orders, from most to fewest.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 4
SELECT c.name, COUNT(p.id) AS orders
FROM customers AS c
LEFT JOIN orders AS p ON p.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY orders DESC, c.name;

Verified result (12 rows):

nameorders
Amy Wilson3
Megan Reed3
Hugh Miller2
Laura Ward2
Liam Martin2
Sara Green2
David Clark1
Emily Fox1
Jack Wood1
Nia Smith1
Miles Lee0
Peter Hill0

COUNT(p.id) counts only the orders that exist. With COUNT(*), a customer with no orders would show 1, because the customer row still exists.

Common mistake: Use COUNT(*) with a LEFT JOIN: it counts the customer row, not the order.

5. Find the cities with at least two active customers

Intermediate · HAVING

Your challenge

Show the cities with two or more active customers, and how many there are.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 5
SELECT city, COUNT(*) AS customers
FROM customers
WHERE active = 1
GROUP BY city
HAVING COUNT(*) >= 2
ORDER BY customers DESC, city;

Verified result (4 rows):

citycustomers
London3
Bristol2
Leeds2
Manchester2

WHERE keeps only the active customers and HAVING then filters the groups by their count. Cities with only one active customer drop out.

Common mistake: Putting COUNT(*) >= 2 in the WHERE: the count does not exist yet when the rows are filtered.

6. Calculate revenue from shipped orders by category

Advanced · four tables

Your challenge

Show the revenue from the lines of shipped orders by category, from highest to lowest.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 6
SELECT cat.name AS category,
       ROUND(SUM(d.quantity * d.unit_price), 2) AS revenue
FROM order_items AS d
JOIN orders AS p ON p.id = d.order_id
JOIN products AS pr ON pr.id = d.product_id
JOIN categories AS cat ON cat.id = pr.category_id
WHERE p.status = 'shipped'
GROUP BY cat.id, cat.name
ORDER BY revenue DESC, category;

Verified result (5 rows):

categoryrevenue
Computers999
Audio237.8
Accessories220.8
Displays179
Storage178

Each order line is joined to its order, its product and the product's category. A line's revenue is the quantity times the price it was sold at, not the product's current price.

Common mistake: Adding up orders.total after joining to the lines: each order's total would be repeated once per line.

7. Find the three products with the most units sold

Intermediate · SUM and LIMIT

Your challenge

Show the three products with the most units sold across all orders.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 7
SELECT pr.name, SUM(d.quantity) AS units
FROM order_items AS d
JOIN products AS pr ON pr.id = d.product_id
GROUP BY pr.id, pr.name
ORDER BY units DESC, pr.name
LIMIT 3;

Verified result (3 rows):

nameunits
USB-C Cable6
Headphones4
Mouse4

The question asks about units, so we add up quantity. Breaking ties by name keeps the result the same from one run to the next.

Common mistake: Use COUNT(*): it counts lines, so a line with quantity 2 would count the same as one with quantity 1.

8. Calculate the average order value by status

Beginner · AVG

Your challenge

Show the average amount of the orders in each status, rounded to two decimal places.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Solution to exercise 8
SELECT status,
       COUNT(*) AS orders,
       ROUND(AVG(total), 2) AS average_ticket
FROM orders
GROUP BY status
ORDER BY average_ticket DESC;

Verified result (3 rows):

statusordersaverage_ticket
cancelled3424.3
shipped9202.08
pending6140.05

AVG computes each group's average. Showing the count as well helps you see whether an average rests on only a few orders.

Common mistake: Rounding before averaging: round the result of AVG, not each individual value.

9. Find the orders with a total above the average

Intermediate · subquery

Your challenge

Show the orders whose total is above the average total of all orders, from highest to lowest.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Exercise 9 solution
SELECT id, status, total
FROM orders
WHERE total > (SELECT AVG(total) FROM orders)
ORDER BY total DESC, id;

Verified result (4 rows):

idstatustotal
106shipped1088
116cancelled999
108cancelled249
111pending249

The subquery returns a single value, the overall average, which the WHERE compares with each order.

Common mistake: Write WHERE total > AVG(total): an aggregate function cannot be used directly in WHERE.

10. Find the date of each customer's first order

Intermediate · MIN and JOIN

Your challenge

Show each customer's name with the date of their first order, sorted by that date.

Before opening the solution: predict how many rows your query will return.

Show solution and result
Exercise 10 solution
SELECT c.name, MIN(p.date) AS first_order
FROM customers AS c
JOIN orders AS p ON p.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY first_order, c.name;

Verified result (10 rows):

namefirst_order
Amy Wilson2025-05-02
Liam Martin2025-05-04
Megan Reed2025-05-11
David Clark2025-05-15
Sara Green2025-05-18
Laura Ward2025-05-23
Hugh Miller2025-05-25
Nia Smith2025-05-28
Jack Wood2025-06-03
Emily Fox2025-06-06

MIN on a date in the format AAAA-MM-DD returns the earliest one, because that format sorts the same way as text and as a date.

Common mistake: Storing dates as DD/MM/AAAA: sorting them as text no longer matches chronological order.

Once you have solved them

If you struggle with joins, review INNER JOIN and LEFT JOIN; for summaries, GROUP BY and HAVING. For the most common mistakes, see LEFT JOIN losing rows and duplicates after a JOIN. The first eight solved exercises cover filtering and sorting, and the SQL cheat sheet sums up the syntax.