Three-valued logic (NOT IN)

Concepts covered: Three-Valued Logic

The filtering lessons showed the symptom: a NOT IN subquery that returns a single NULL makes the whole filter return 0 rows. This section takes the expression apart to see why, then looks at a second case that catches teams after they've fixed the first.

x NOT IN (1, 3, NULL) is shorthand for x <> 1 AND x <> 3 AND x <> NULL. The last comparison is NULL for every x, and an AND containing a NULL can come out FALSE or NULL but never TRUE. WHERE keeps only TRUE rows, so nothing survives. The trace runs that chain for 4 artist ids.

artist_id<> 1<> 3<> NULLAND of all 3
1FALSETRUENULLFALSE
2TRUETRUENULLNULL
3TRUEFALSENULLFALSE
4TRUETRUENULLNULL

IN hides the same NULL. x IN (1, 3, NULL) expands to an OR chain, which is TRUE for ids 1 and 3 and NULL for ids 2 and 4. WHERE drops NULL just as it drops FALSE, so the IN filter returns the right rows and looks healthy. Putting NOT in front swaps TRUE with FALSE and leaves NULL where it was, which is how a correct IN query turns into an empty NOT IN query.

Four ways to find artists on no playlist

artists lists 4 artists, and playlist_tracks records which artist appears on which playlist. One track row on playlist 11 lost its artist id. Each tab asks which artists appear on no playlist.

artists
artist_idartist_name
1Nova
2Kite
3Juno
4Ravel
playlist_tracks
playlist_idartist_id
101
103
11NULL
111

NOT IN

Compares each artist with a list that contains a NULL, so no row can be TRUE.

SELECT
  artist_id,
  artist_name
FROM artists
WHERE artist_id NOT IN (
  SELECT
    artist_id
  FROM playlist_tracks
)
ORDER BY artist_id
Result
artist_idartist_name

Filtered

Removes NULLs inside the subquery first, so the comparison chain can be TRUE again.

SELECT
  artist_id,
  artist_name
FROM artists
WHERE artist_id NOT IN (
  SELECT
    artist_id
  FROM playlist_tracks
  WHERE artist_id IS NOT NULL
)
ORDER BY artist_id
Result
artist_idartist_name
2Kite
4Ravel

NOT EXISTS

Looks for a matching track row per artist, and a NULL id matches nothing, so it blocks no artist.

SELECT
  a.artist_id,
  a.artist_name
FROM artists AS a
WHERE NOT EXISTS (
  SELECT
    1
  FROM playlist_tracks AS p
  WHERE p.artist_id = a.artist_id
)
ORDER BY a.artist_id
Result
artist_idartist_name
2Kite
4Ravel

EXCEPT

Subtracts one set of ids from another, and the NULL on the right finds no NULL on the left to remove.

SELECT
  artist_id
FROM artists
EXCEPT
SELECT
  artist_id
FROM playlist_tracks
ORDER BY artist_id
Result
artist_id
2
4

The last 3 tabs agree here on Kite and Ravel, ids 2 and 4. They stop agreeing when the NULL sits on the left of the filter instead of inside the list.

When the probe value is NULL

streams logs plays, and stream 102 arrived without an artist id. A data quality check looks for orphan streams, ones whose artist is missing from artists. That table has no NULL ids, so the list is clean. The probe is not: for stream 102, NULL NOT IN (1, 2, 3, 4) compares NULL with each id, every comparison is NULL, and the row is dropped. NOT EXISTS runs its subquery for stream 102, finds no artist row where artist_id = NULL holds, and keeps the stream. EXCEPT keeps a NULL row whenever the other side holds no NULL.

streams holds (100, 1), (101, 7), (102, NULL) and (103, 2) as stream_id and artist_id, and artists holds ids 1 to 4. Which streams does this query return?

SELECT
  stream_id
FROM streams
WHERE artist_id NOT IN (
  SELECT
    artist_id
  FROM artists
)

Which behavior is right depends on the question. For orphan streams, 102 belongs in the result: a play nobody can attribute is the problem the check exists to find. Write down what a NULL probe value should mean for each anti-filter, then pick the form that does that.

About this lesson

This section is part of the NULL Values: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.