← Learning Hub
Advanced

Project: Sales Report and Interview Questions

Time to answer real business questions about our shop. Try each one yourself first, then compare with the answer.

This project answers real business questions from the shop database using everything in the course. Read each query, predict the result, then run it. Changing the questions slightly and writing your own queries is the best way to become confident with SQL.

1. Revenue by category (excluding cancelled orders)

Example
SELECT p.category, SUM(oi.qty * oi.price) AS revenue
FROM order_items oi
JOIN orders o   ON o.id = oi.order_id
JOIN products p ON p.id = oi.product_id
WHERE o.status <> 'cancelled'
GROUP BY p.category
ORDER BY revenue DESC;

-- Result:
-- category    | revenue
-- ------------+--------
-- Electronics | 4297
-- Fashion     | 2796
-- Books       | 2394
-- Home        | 1946
▶ Try it yourself

2. Best customer

Example
SELECT c.name, SUM(oi.qty * oi.price) AS spent
FROM customers c
JOIN orders o       ON o.customer_id = c.id AND o.status <> 'cancelled'
JOIN order_items oi ON oi.order_id = o.id
GROUP BY c.id, c.name
ORDER BY spent DESC
LIMIT 1;

-- Result:
-- name       | spent
-- -----------+------
-- Ravi Kumar | 4297
▶ Try it yourself

3. Products to restock (out of stock, or selling and low)

Example
SELECT name, stock
FROM products
WHERE stock = 0
   OR (stock < 50 AND id IN (SELECT product_id FROM order_items))
ORDER BY stock;

-- Result:
-- name             | stock
-- -----------------+------
-- Smart Watch      | 0
-- Table Lamp       | 25
-- Wireless Earbuds | 35
-- Python Book      | 40
▶ Try it yourself

4. Second highest price (a classic interview question)

Example
SELECT MAX(price) AS second_highest
FROM products
WHERE price < (SELECT MAX(price) FROM products);

-- Result:
-- second_highest
-- --------------
-- 1499
▶ Try it yourself

5. Monthly sales

Example
SELECT strftime('%Y-%m', o.order_date) AS month,
       COUNT(DISTINCT o.id) AS orders,
       SUM(oi.qty * oi.price) AS revenue
FROM orders o JOIN order_items oi ON oi.order_id = o.id
WHERE o.status <> 'cancelled'
GROUP BY month
ORDER BY month;

-- Result:
-- month   | orders | revenue
-- --------+--------+--------
-- 2026-10 | 3      | 5091
-- 2026-11 | 4      | 6342
▶ Try it yourself

More to practise

  • Find duplicate rows: GROUP BY the columns HAVING COUNT(*) > 1.
  • The Nth highest value with DENSE_RANK().
  • Customers who ordered in every month of the year.
  • Average order value per city.
Next steps: install PostgreSQL or MySQL on your computer, load a public dataset, and connect to it from Python, Java or Node.js.
Key points
  • Break big questions into small queries.
  • Use CTEs to build answers step by step.
  • Check results make sense before sharing them.

📝 Test yourself

1. Which clause removes cancelled orders before adding up revenue?
2. How do you find duplicate emails?
3. Fill in the function that counts different orders only once:
COUNT( o.id)