SQL Exercises
Time to practise on the TAD Market database. Each exercise has a starter playground that already runs and shows part of what you need — replace the TODO parts, press ▶ Run, and compare your result with the expected answer. Every run starts from a fresh database, so experiment freely. Try for a few minutes before opening the solution.
| # | Exercise | Topics |
|---|---|---|
| 1 | Cheap and in stock | WHERE, AND, ORDER BY |
| 2 | Customers without e-mail | IS NULL, COALESCE |
| 3 | Orders per status | GROUP BY, COUNT |
| 4 | Customer order history | INNER JOIN, aliases |
| 5 | Never-ordering customers | LEFT JOIN / NOT EXISTS |
| 6 | Top categories by revenue | joins, SUM, HAVING |
| 7 | Price update with a backup | CREATE TABLE AS, UPDATE |
| 8 | Employee ranking | window functions |
| 9 | Management chain | recursive CTE |
1. Cheap and in stock
List the name and price of every product that costs less than 4 and has stock greater than 0, cheapest first.
Expected: 8 rows, starting with Whole Milk | 1.45.
-- TODO: add the stock condition and the sort order
SELECT name, price
FROM products
WHERE price < 4;name | price
-------------------+------
Orange Juice | 3.2
Whole Milk | 1.45
Greek Yogurt | 3.95
Apples | 2.99
Cherry Tomatoes | 2.75
Bananas | 1.89
Dark Chocolate Bar | 2.49
Tortilla Chips | 3.49
(8 rows)
Show solution
SELECT name, price
FROM products
WHERE price < 4 AND stock > 0
ORDER BY price;2. Customers without e-mail
Show the name and country of customers without an e-mail address, plus a column email that shows '(missing)' instead of NULL. Sort by name.
Expected: 4 rows — Felix Reyes, Le Petit Marche, Omar Murphy, Zara Ali.
-- TODO: keep only customers without e-mail and replace the NULL
SELECT name, country, email
FROM customers
ORDER BY name
LIMIT 3;name | country | email
-------------+---------+-------------------------
Amara Costa | Nigeria | amara.costa@example.com
Arjun Sharma | Norway | arjun.sharma@example.com
Bright Bytes | France | liam.schmidt@example.com
(3 rows)
Show solution
SELECT name, country, COALESCE(email, '(missing)') AS email
FROM customers
WHERE email IS NULL
ORDER BY name;3. Orders per status
How many orders does each status have? Show status and orders, largest group first.
Expected: delivered | 16 on top, cancelled | 7 at the bottom.
-- TODO: group and count
SELECT status
FROM orders
LIMIT 3;status
---------
cancelled
delivered
shipped
(3 rows)
Show solution
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
ORDER BY orders DESC;4. Customer order history
List all orders of the customer Kai Moreau: order id, order date, status and the first name of the employee who handled it, oldest first.
Expected: 4 rows (orders 18, 22, 36, 38).
-- TODO: join customers and employees, filter by the customer's name
SELECT o.order_id, o.order_date, o.status
FROM orders AS o
WHERE o.customer_id = 1;order_id | order_date | status
---------+------------+----------
26 | 2025-07-12 | delivered
(1 row)
Show solution
SELECT o.order_id, o.order_date, o.status, e.first_name AS employee
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN employees AS e ON e.employee_id = o.employee_id
WHERE c.name = 'Kai Moreau'
ORDER BY o.order_date;5. Never-ordering customers
Which customers have never placed an order? Show id, name and city, sorted by id.
Expected: 7 rows, from 4 | Pixel Studio | Madrid to 24 | Hugo Berg | Stockholm.
-- TODO: find the customers without any order
SELECT customer_id, name, city
FROM customers
ORDER BY customer_id
LIMIT 2;customer_id | name | city
------------+--------------+-------
1 | Corner Cafe | Berlin
2 | Bright Bytes | Paris
(2 rows)
Show solution
SELECT c.customer_id, c.name, c.city
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL
ORDER BY c.customer_id;
-- or:
-- WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id)6. Top categories by revenue
For orders that were not cancelled, compute the revenue (quantity × product price) per category name. Show only categories with revenue above 300, highest first, rounded to 2 decimals.
Expected: 5 rows, starting with Electronics | 2153.35.
-- TODO: join categories, exclude cancelled orders, filter groups with HAVING
SELECT p.category_id, SUM(oi.quantity * p.price) AS revenue
FROM order_items AS oi
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY p.category_id;category_id | revenue
------------+-------------------
1 | 489.85
2 | 372.65
3 | 69.7
4 | 88.71
5 | 117.21000000000001
6 | 2271.82
7 | 1127
8 | 647.5
(8 rows)
Show solution
SELECT c.name AS category,
ROUND(SUM(oi.quantity * p.price), 2) AS revenue
FROM order_items AS oi
JOIN orders AS o ON o.order_id = oi.order_id
JOIN products AS p ON p.product_id = oi.product_id
JOIN categories AS c ON c.category_id = p.category_id
WHERE o.status <> 'cancelled'
GROUP BY c.category_id
HAVING SUM(oi.quantity * p.price) > 300
ORDER BY revenue DESC;7. Price update with a backup
First save a backup copy of the products table as products_backup. Then raise all Bakery prices (category 2) by 10 %, rounded to 2 decimals. Finally show name, old price and new price of the bakery products by joining the backup.
Expected: Bagels | 4.9 | 5.39 among 4 rows.
-- TODO: 1) create the backup 2) update 3) compare
SELECT name, price FROM products WHERE category_id = 2;name | price
-----------------+------
Sourdough Loaf | 5.5
Butter Croissant | 6.25
Chocolate Cake | 22
Bagels | 4.9
(4 rows)
Show solution
CREATE TABLE products_backup AS SELECT * FROM products;
UPDATE products
SET price = ROUND(price * 1.1, 2)
WHERE category_id = 2;
SELECT p.name, b.price AS old_price, p.price AS new_price
FROM products AS p
JOIN products_backup AS b ON b.product_id = p.product_id
WHERE p.category_id = 2
ORDER BY p.name;8. Employee ranking
Rank all employees by salary (highest = 1) using RANK(), and show the difference between each salary and the company average, rounded to whole numbers.
Expected first row: Grace | 185000 | 1 | 105950.
-- TODO: add the rank and the difference from the average
SELECT first_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;first_name | salary
-----------+-------
Grace | 185000
Victor | 98000
Lin | 95000
(3 rows)
Show solution
SELECT first_name,
salary,
RANK() OVER (ORDER BY salary DESC) AS salary_rank,
ROUND(salary - AVG(salary) OVER ()) AS diff_from_avg
FROM employees
ORDER BY salary_rank;9. Management chain
Starting from Hana (employee 8), walk up the hierarchy and list every manager above her, with a level number (1 = direct manager).
Expected: 1 | Lin | Operations Manager and 2 | Grace | CEO.
-- TODO: turn this into a recursive CTE that keeps following manager_id
SELECT employee_id, first_name, manager_id
FROM employees
WHERE employee_id = 8;employee_id | first_name | manager_id
------------+------------+-----------
8 | Hana | 3
(1 row)
Show solution
WITH RECURSIVE chain AS (
SELECT e.employee_id, e.first_name, e.title, e.manager_id, 1 AS level
FROM employees AS e
WHERE e.employee_id = (SELECT manager_id FROM employees WHERE employee_id = 8)
UNION ALL
SELECT m.employee_id, m.first_name, m.title, m.manager_id, chain.level + 1
FROM employees AS m
JOIN chain ON m.employee_id = chain.manager_id
)
SELECT level, first_name, title
FROM chain
ORDER BY level;Test yourself
Which clause do you need to keep only groups whose SUM is above 300?
- WHERE
- HAVING
- ORDER BY
- LIMIT
Which pattern lists customers that have no orders?
- INNER JOIN orders
- LEFT JOIN orders … WHERE orders.order_id IS NULL
- CROSS JOIN orders
- GROUP BY customer_id