SQL Beginner Challenge 9: Keep All Rows with LEFT JOIN

Difficulty: Beginner
Estimated time: 15–20 minutes
SQL concepts: LEFT JOIN, preserving rows, missing relationships
Goal: Understand how to keep all rows from one table—even when related data is missing.

The Scenario

After joining products with suppliers using an INNER JOIN, the product manager notices something:

“Some products don’t have a supplier yet. I still want to see all products, even if the supplier is missing.”

This is where LEFT JOIN comes in.

Database Tables

products

column_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price
supplier_idINTEGERReferences suppliers.supplier_id (can be NULL)

suppliers

column_nametypedescription
supplier_idINTEGERUnique supplier ID
supplier_nameTEXTSupplier name

Sample Data

products

product_idnamecategorypricesupplier_id
101Wireless MouseAccessories24.991
102Mechanical KeyboardAccessories89.001
10327-inch MonitorDisplays229.992
104USB-C HubAccessories34.50NULL
105Laptop StandWorkspace39.992

suppliers

supplier_idsupplier_name
1TechSource
2DisplayWorks

(Note: There is no supplier with ID NULL.)

Your Task

Write a SQL query that returns:

  • Product name
  • supplier_name (if available)

…and includes all products, even those without a supplier.

Expected Output

namesupplier_name
Wireless MouseTechSource
Mechanical KeyboardTechSource
27-inch MonitorDisplayWorks
USB-C HubNULL
Laptop StandDisplayWorks

Constraints

  • Use LEFT JOIN
  • Join products to suppliers using supplier_id
  • Keep all rows from products
  • Do not use SELECT *

Hint (Optional)

A LEFT JOIN:

  • Keeps all rows from the left table
  • Fills missing matches with NULL values

SELECT
p.name,
s.supplier_name
FROM products p
LEFT JOIN suppliers s
ON p.supplier_id = s.supplier_id;

Explanation

  • LEFT JOIN keeps every row from products.
  • When there is no matching supplier, supplier_name becomes NULL.
  • This is essential for spotting missing or incomplete relationships in data.

Common Mistakes

  1. Using INNER JOIN instead of LEFT JOIN
    • This would hide products without suppliers.
  2. Filtering on the joined table in WHERE
    WHERE s.supplier_name IS NOT NULL -- ❌ turns it back into an INNER JOIN
  3. Confusing LEFT vs RIGHT joins
    • Focus on which table you want to fully keep.

Optional Extension (Mini Bonus)

Try:

  1. Replace NULL supplier names with "Unknown" using COALESCE.
  2. Show only products without a supplier.
  3. Add product price and sort by it.

Next Challenge

Beginner Challenge #10: Find Missing Matches with LEFT JOIN + WHERE

🔗 View reference solution on GitHub
(After you’ve tried the challenge)

Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.