Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
LEFT JOIN with NULL result rows
📖 Scenario: You are managing a small online bookstore database. You have two tables: books and sales. The books table lists all books available, and the sales table records sales transactions for some of these books. Some books may not have any sales yet.
🎯 Goal: Write a SQL query using LEFT JOIN to list all books along with their sales count. For books with no sales, the sales count should show as NULL.
📋 What You'll Learn
Create a table called books with columns book_id (integer) and title (text).
Create a table called sales with columns sale_id (integer) and book_id (integer).
Insert exactly these rows into books: (1, 'The Great Gatsby'), (2, '1984'), (3, 'To Kill a Mockingbird').
Insert exactly these rows into sales: (101, 1), (102, 1), (103, 3).
Write a LEFT JOIN query to join books with sales on book_id.
Select books.book_id, books.title, and the count of sales.sale_id as sales_count.
Group the results by books.book_id and books.title.
Ensure that books with no sales show sales_count as NULL (do not replace with zero).
💡 Why This Matters
🌍 Real World
Online stores often need to report all products with their sales, including those with no sales yet. LEFT JOIN helps show all products even if no sales exist.
💼 Career
Understanding LEFT JOIN and handling NULL results is essential for database querying in roles like data analyst, backend developer, and database administrator.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns book_id (integer) and title (text). Then insert these exact rows into books: (1, 'The Great Gatsby'), (2, '1984'), (3, 'To Kill a Mockingbird').
SQL
Hint
Use CREATE TABLE books (book_id INTEGER, title TEXT); and then INSERT INTO books (book_id, title) VALUES (...), (...), (...);
2
Create the sales table and insert data
Create a table called sales with columns sale_id (integer) and book_id (integer). Then insert these exact rows into sales: (101, 1), (102, 1), (103, 3).
SQL
Hint
Use CREATE TABLE sales (sale_id INTEGER, book_id INTEGER); and then INSERT INTO sales (sale_id, book_id) VALUES (...), (...), (...);
3
Write the LEFT JOIN query to count sales per book
Write a SQL query that uses LEFT JOIN to join books with sales on book_id. Select books.book_id, books.title, and the count of sales.sale_id as sales_count. Group the results by books.book_id and books.title.
SQL
Hint
Use LEFT JOIN to join books and sales on book_id. Use COUNT(sales.sale_id) to count sales per book. Group by books.book_id and books.title.
4
Modify the query to show NULL for books with no sales
Modify the previous query so that books with no sales show sales_count as NULL instead of zero. Do this by using NULLIF function to convert zero counts to NULL.
SQL
Hint
Use NULLIF(COUNT(sales.sale_id), 0) to convert zero counts to NULL.
Practice
(1/5)
1. What does a LEFT JOIN do in SQL?
easy
A. Returns all rows from the left table and matched rows from the right table, NULL if no match.
B. Returns only rows that have matching values in both tables.
C. Returns all rows from the right table and matched rows from the left table.
D. Deletes rows from the left table that have no match in the right table.
Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN keeps all rows from the left table regardless of matches in the right table.
Step 2: Identify NULLs for unmatched rows
If there is no matching row in the right table, the result shows NULL for right table columns.
Final Answer:
Returns all rows from the left table and matched rows from the right table, NULL if no match. -> Option A
Quick Check:
LEFT JOIN = all left rows + NULL for no match [OK]
Hint: LEFT JOIN keeps all left rows, unmatched right rows show NULL [OK]
Common Mistakes:
Confusing LEFT JOIN with INNER JOIN
Thinking unmatched rows are dropped
Assuming NULLs appear in left table columns
2. Which of the following is the correct syntax for a LEFT JOIN in SQL?
easy
A. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id;
B. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 LEFT JOIN table2 USING (id);
D. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
Solution
Step 1: Review standard LEFT JOIN syntax
The correct syntax is: SELECT columns FROM left_table LEFT JOIN right_table ON condition.
Step 2: Check each option
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; matches the correct syntax exactly. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has JOIN LEFT which is invalid. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; is invalid because JOIN requires an ON clause (syntax error). SELECT * FROM table1 LEFT JOIN table2 USING (id); is correct syntax with parentheses.
Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option D
Quick Check:
LEFT JOIN syntax = LEFT JOIN ... ON ... [OK]
Hint: Use LEFT JOIN ... ON ... for correct syntax [OK]
Common Mistakes:
Swapping JOIN and LEFT keywords
Using WHERE instead of ON for join condition
Omitting parentheses in USING clause
3. Given tables Employees and Departments with data:
Employees: id | name | dept_id 1 | Alice | 10 2 | Bob | 20 3 | Carol | NULL