SQL LEFT JOIN Quiz

This SQL quiz is about LEFT JOIN and the NULLs it brings with it. You will count matches without losing unmatched rows, find rows that have no partner and see how COUNT, SUM and table order behave with outer joins.

Medium 10 questions ~6 minutes Pass at 80% SQLite

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 10

What does this print?

CREATE TABLE customers (id INTEGER, name TEXT);
CREATE TABLE orders (id INTEGER, customer_id INTEGER);
INSERT INTO customers VALUES (1, 'Ann'), (2, 'Bob'), (3, 'Cy');
INSERT INTO orders VALUES (10, 1), (11, 1), (12, 3);
SELECT c.name, COUNT(o.id)
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.name;
Show the answer

C. Ann|2 Bob|0 Cy|1 The left join keeps Bob with NULL order columns. COUNT(o.id) skips NULLs, so Bob's count is 0, which is exactly what you want for "orders per customer".

What this quiz covers

  • Keeping unmatched rows from the left table
  • COUNT(*) vs COUNT(column) after a LEFT JOIN
  • Finding rows with no match using IS NULL
  • NULL sums and COALESCE
  • RIGHT and FULL OUTER JOIN in SQLite, MySQL and PostgreSQL

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.