runtime
19 / 88

lesson 19 Joins & sets · intermediate

Keep unmatched rows

Keep rows even when the other table has no match.

the idea

A LEFT JOIN keeps every row of the left table. When no right-side row matches, the right-side columns are NULL. COUNT(right.key) then counts matches, giving zero for unmatched rows, and WHERE right.key IS NULL keeps only the unmatched ones.

Dataset: Retail

products6 columns · 5,304 rows
stock_codetext · unique iddescriptiontextcategory_idnumber · links to categorieslist_pricedecimalfirst_sold_atdate & timeis_merchandisenumber
10002INFLATABLE POLITICAL GLOBE170.852009-12-01 09:08:001
10002RROBOT PENCIL SHARPNER85.172009-12-02 14:43:001
10080GROOVY CACTUS INFLATABLE170.392009-12-02 16:02:001
10109BENDY COLOUR PENCILS80.422009-12-03 12:31:001
10120DOGGY RUBBER170.212009-12-01 16:17:001
10123CHEARTS WRAPPING TAPE80.652009-12-01 14:28:001
10123GARMY CAMO WRAPPING TAPE80.652009-12-01 14:28:001
10124ASPOTS ON RED BOOKCOVER TAPE80.422010-02-11 18:13:001
10124CNULLNULLNULL2010-05-11 12:51:001
10124GARMY CAMO BOOKCOVER TAPE80.422010-05-13 16:49:001
stock_codetext · unique id
10002
descriptiontext
INFLATABLE POLITICAL GLOBE
category_idnumber · links to categories
17
list_pricedecimal
0.85
first_sold_atdate & time
2009-12-01 09:08:00
is_merchandisenumber
1
stock_codetext · unique id
10002R
descriptiontext
ROBOT PENCIL SHARPNER
category_idnumber · links to categories
8
list_pricedecimal
5.17
first_sold_atdate & time
2009-12-02 14:43:00
is_merchandisenumber
1
stock_codetext · unique id
10080
descriptiontext
GROOVY CACTUS INFLATABLE
category_idnumber · links to categories
17
list_pricedecimal
0.39
first_sold_atdate & time
2009-12-02 16:02:00
is_merchandisenumber
1
stock_codetext · unique id
10109
descriptiontext
BENDY COLOUR PENCILS
category_idnumber · links to categories
8
list_pricedecimal
0.42
first_sold_atdate & time
2009-12-03 12:31:00
is_merchandisenumber
1
stock_codetext · unique id
10120
descriptiontext
DOGGY RUBBER
category_idnumber · links to categories
17
list_pricedecimal
0.21
first_sold_atdate & time
2009-12-01 16:17:00
is_merchandisenumber
1
stock_codetext · unique id
10123C
descriptiontext
HEARTS WRAPPING TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.65
first_sold_atdate & time
2009-12-01 14:28:00
is_merchandisenumber
1
stock_codetext · unique id
10123G
descriptiontext
ARMY CAMO WRAPPING TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.65
first_sold_atdate & time
2009-12-01 14:28:00
is_merchandisenumber
1
stock_codetext · unique id
10124A
descriptiontext
SPOTS ON RED BOOKCOVER TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.42
first_sold_atdate & time
2010-02-11 18:13:00
is_merchandisenumber
1
stock_codetext · unique id
10124C
descriptiontext
NULL
category_idnumber · links to categories
NULL
list_pricedecimal
NULL
first_sold_atdate & time
2010-05-11 12:51:00
is_merchandisenumber
1
stock_codetext · unique id
10124G
descriptiontext
ARMY CAMO BOOKCOVER TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.42
first_sold_atdate & time
2010-05-13 16:49:00
is_merchandisenumber
1

first 10 of 5,304 rows · your queries use all of them

order_items7 columns · 125,613 rows
order_item_idnumber · unique idorder_idnumber · links to ordersline_numbernumberstock_codetext · links to productsdescriptiontextquantitynumberunit_pricedecimal
11121329DINOSAURS WRITING SET280.98
21221252SET OF MEADOW FLOWER STICKERS301.69
31321100CHARLIE AND LOLA CHARLOTTE BAG301.15
41421033JUMBO BAG CHARLIE AND LOLA TOYS302
51520711JUMBO BAG TOYS601.3
61621410COUNTRY COTTAGE DOORSTOP GREEN322.5
71721411GINGHAM HEART DOORSTOP RED322.5
81884031ACHARLIE+LOLA RED HOT WATER BOTTLE563
91984031BCHARLIE LOLA BLUE HOT WATER BOTTLE563
1011084032ACHARLIE+LOLA PINK HOT WATER BOTTLE601.9
order_item_idnumber · unique id
1
order_idnumber · links to orders
1
line_numbernumber
1
stock_codetext · links to products
21329
descriptiontext
DINOSAURS WRITING SET
quantitynumber
28
unit_pricedecimal
0.98
order_item_idnumber · unique id
2
order_idnumber · links to orders
1
line_numbernumber
2
stock_codetext · links to products
21252
descriptiontext
SET OF MEADOW FLOWER STICKERS
quantitynumber
30
unit_pricedecimal
1.69
order_item_idnumber · unique id
3
order_idnumber · links to orders
1
line_numbernumber
3
stock_codetext · links to products
21100
descriptiontext
CHARLIE AND LOLA CHARLOTTE BAG
quantitynumber
30
unit_pricedecimal
1.15
order_item_idnumber · unique id
4
order_idnumber · links to orders
1
line_numbernumber
4
stock_codetext · links to products
21033
descriptiontext
JUMBO BAG CHARLIE AND LOLA TOYS
quantitynumber
30
unit_pricedecimal
2
order_item_idnumber · unique id
5
order_idnumber · links to orders
1
line_numbernumber
5
stock_codetext · links to products
20711
descriptiontext
JUMBO BAG TOYS
quantitynumber
60
unit_pricedecimal
1.3
order_item_idnumber · unique id
6
order_idnumber · links to orders
1
line_numbernumber
6
stock_codetext · links to products
21410
descriptiontext
COUNTRY COTTAGE DOORSTOP GREEN
quantitynumber
32
unit_pricedecimal
2.5
order_item_idnumber · unique id
7
order_idnumber · links to orders
1
line_numbernumber
7
stock_codetext · links to products
21411
descriptiontext
GINGHAM HEART DOORSTOP RED
quantitynumber
32
unit_pricedecimal
2.5
order_item_idnumber · unique id
8
order_idnumber · links to orders
1
line_numbernumber
8
stock_codetext · links to products
84031A
descriptiontext
CHARLIE+LOLA RED HOT WATER BOTTLE
quantitynumber
56
unit_pricedecimal
3
order_item_idnumber · unique id
9
order_idnumber · links to orders
1
line_numbernumber
9
stock_codetext · links to products
84031B
descriptiontext
CHARLIE LOLA BLUE HOT WATER BOTTLE
quantitynumber
56
unit_pricedecimal
3
order_item_idnumber · unique id
10
order_idnumber · links to orders
1
line_numbernumber
10
stock_codetext · links to products
84032A
descriptiontext
CHARLIE+LOLA PINK HOT WATER BOTTLE
quantitynumber
60
unit_pricedecimal
1.9

first 10 of 125,613 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. Find products never ordered. Run this. LEFT JOIN keeps every product; lines with no match show NULL for order_item_id.

    step-1.sql
    SELECT p.stock_code, p.description, i.order_item_id
    FROM products p
    LEFT JOIN order_items i ON i.stock_code = p.stock_code
    WHERE i.order_item_id IS NULL
    LIMIT 10;
  2. Count products per category safely. Run this. COUNT(p.stock_code) counts matched products; an empty category shows 0, not 1.the pattern

    example.sql
    SELECT c.category_id, c.category_name, COUNT(p.stock_code) AS products
    FROM categories c
    LEFT JOIN products p ON p.category_id = c.category_id
    GROUP BY c.category_id, c.category_name;
  3. Compare inner join to full category list. Run this. Inner join only counts categories that have at least one product.

    step-3.sql
    SELECT
      (
        SELECT COUNT(*)
        FROM categories
      ) AS categories,
      (
        SELECT COUNT(DISTINCT c.category_id)
        FROM categories c
        JOIN products p ON p.category_id = c.category_id
      ) AS categories_with_products;

common mistake

COUNT(*) after a LEFT JOIN counts the unmatched row itself, so a product with no lines would show 1 instead of 0.

challenge 1

Find every merchandise product (is_merchandise = 1).

Return stock_code, description and lines_sold, where lines_sold counts its invoice lines and is 0 for products never ordered.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Keep unmatched rows — Joins & sets · Runtime