SQL practice

SQL JOIN practice questions

14 problems, ordered from easiest. They run in your browser, and 3 are free.

Start with Employee department names

The idea in a minute

A join puts two tables side by side by matching a column. An INNER JOIN keeps only the rows that match on both sides. A LEFT JOIN keeps every row from the left table and fills NULL where nothing matched.

The mistakes are almost always about row counts: a join that repeats rows, or one that quietly drops them. The problems here ask you to check the count after a join as well as write it.

Example: Customers with and without orders

CREATE TABLE customers AS SELECT * FROM (VALUES (1,'Asha'),(2,'Ravi'),(3,'Meera')) t(customer_id, name);
CREATE TABLE orders AS SELECT * FROM (VALUES (10,1,450),(11,1,150),(12,2,300)) t(order_id, customer_id, amount);
SELECT c.name, COUNT(o.order_id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.name
ORDER BY c.name

What it prints

name   orders
Asha   2
Meera  0
Ravi   1

What to watch for

  • Check how many rows you have before and after. A join on a column with repeats multiplies rows.
  • A filter on the right table in WHERE can turn a LEFT JOIN into an INNER JOIN. Put that condition in ON instead.
  • Use table aliases so it is clear which table each column comes from.

Joins problems

  1. Employee department namesbeginner · Free
  2. Employees with optional departmentbeginner · Free
  3. Customers who never orderedbeginner · Free
  4. Customers with more than 2 ordersintermediate · Locked
  5. Join row counts with duplicates and NULLsintermediate · Locked
  6. Fix a join that doubles revenueintermediate · Locked
  7. Salary grade from a band tableintermediate · Locked
  8. Usage by plan with point-in-time contractsadvanced · Locked
  9. First-touch attributionadvanced · Locked
  10. Last-touch attributionadvanced · Locked
  11. Cancellation rate excluding banned usersadvanced · Locked
  12. Source versus target reconciliationadvanced · Locked
  13. User purchase platformadvanced · Locked
  14. Advertiser status transitionsadvanced · Locked

Want a guided order across every topic? Open the practice sheets or the SQL hub.