runtime
30 / 88

lesson 30 Dates, strings & subqueries · beginner

NOT IN and the NULL trap

Avoid unexpected missing results when NOT IN encounters NULL.

the idea

x NOT IN (list) is true only when x differs from every element. If the list contains NULL, “differs from NULL” is unknown, so NOT IN returns no rows at all. NOT EXISTS is not affected; with NOT IN, remove NULLs from the subquery.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

customers4 columns · 848 rows
customer_idnumber · unique idcountrytextregiontextfirst_order_atdate & time
12347IcelandEurope2010-10-31 14:20:00
12353BahrainRest of World2010-10-27 12:44:00
12359CyprusEurope2009-12-05 13:32:00
12373AustriaEurope2010-02-04 12:08:00
12374AustriaEurope2010-04-30 14:01:00
12384SwitzerlandEurope2011-08-10 11:23:00
customer_idnumber · unique id
12347
countrytext
Iceland
regiontext
Europe
first_order_atdate & time
2010-10-31 14:20:00
customer_idnumber · unique id
12353
countrytext
Bahrain
regiontext
Rest of World
first_order_atdate & time
2010-10-27 12:44:00
customer_idnumber · unique id
12359
countrytext
Cyprus
regiontext
Europe
first_order_atdate & time
2009-12-05 13:32:00
customer_idnumber · unique id
12373
countrytext
Austria
regiontext
Europe
first_order_atdate & time
2010-02-04 12:08:00
customer_idnumber · unique id
12374
countrytext
Austria
regiontext
Europe
first_order_atdate & time
2010-04-30 14:01:00
customer_idnumber · unique id
12384
countrytext
Switzerland
regiontext
Europe
first_order_atdate & time
2011-08-10 11:23:00

first 6 of 848 rows · your queries use all of them

orders6 columns · 6,722 rows
order_idnumber · unique idinvoice_notextcustomer_idnumber · links to customersordered_atdate & timecountrytextorder_typetext
1489438181022009-12-01 09:24:00United Kingdomsale
2489517163292009-12-01 11:34:00United Kingdomsale
3C489528167142009-12-01 11:50:00United Kingdomcancellation
4489529179842009-12-01 11:51:00United Kingdomsale
5489532133942009-12-01 11:58:00United Kingdomsale
6489540129132009-12-01 12:18:00United Kingdomsale
order_idnumber · unique id
1
invoice_notext
489438
customer_idnumber · links to customers
18102
ordered_atdate & time
2009-12-01 09:24:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
2
invoice_notext
489517
customer_idnumber · links to customers
16329
ordered_atdate & time
2009-12-01 11:34:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
3
invoice_notext
C489528
customer_idnumber · links to customers
16714
ordered_atdate & time
2009-12-01 11:50:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
4
invoice_notext
489529
customer_idnumber · links to customers
17984
ordered_atdate & time
2009-12-01 11:51:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
5
invoice_notext
489532
customer_idnumber · links to customers
13394
ordered_atdate & time
2009-12-01 11:58:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
6
invoice_notext
489540
customer_idnumber · links to customers
12913
ordered_atdate & time
2009-12-01 12:18:00
countrytext
United Kingdom
order_typetext
sale

first 6 of 6,722 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Count anonymous cancellations. Run this. Some cancellation orders have customer_id NULL in the source data.the pattern

    example.sql
    SELECT COUNT(*) AS anonymous_cancellations
    FROM orders
    WHERE order_type = 'cancellation' AND customer_id IS NULL;
  2. See NOT IN return nothing. Run this. When the subquery list includes NULL, NOT IN matches no customers.

    step-2.sql
    SELECT COUNT(*) AS customers
    FROM customers
    WHERE customer_id NOT IN (
      SELECT customer_id
      FROM orders
      WHERE order_type = 'cancellation'
    );
  3. Answer with NOT EXISTS instead. Run this. NOT EXISTS tests for related rows without the NULL trap that breaks NOT IN.

    step-3.sql
    SELECT COUNT(*) AS customers_without_sales
    FROM customers c
    WHERE NOT EXISTS (
      SELECT 1
      FROM orders o
      WHERE o.customer_id = c.customer_id AND o.order_type = 'sale'
    );

common mistake

customer_id NOT IN (SELECT customer_id FROM orders WHERE order_type = 'cancellation') returns nothing, because some cancellations are anonymous.

challenge 1

Return customer_id for customers who never placed a cancellation order.

Use NOT EXISTS.

what to return

Columns
customer_id
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
NOT IN and the NULL trap — Dates, strings & subqueries · Runtime