AdvancedSQL · 26 min · 18 challenges

NULL Values: Advanced

On a Monday at Spotify, an analyst asks which artists in the catalog have never appeared on an editorial playlist, so the team can pitch them. The query returns 0 rows, and the team nearly reports that every artist is covered. One playlist row holds a NULL artist_id, and that single NULL erased every answer. This lesson traces where NULLs hide and how they spread.

NULLs in window functions

Daily Life
Interviews

A window function computes a value for each row from a set of related rows. Every row stays in the output, where GROUP BY would fold them into one. Window functions get lessons of their own later in the track, and this section needs only 3 pieces of them. An OVER (...) clause after a function makes it a window function. ORDER BY inside OVER lines the rows up, and an aggregate such as SUM then covers every row from the first one through the current one. PARTITION BY inside OVER splits the rows into separate groups the way GROUP BY does, without collapsing them.

The table store_sales holds 5 days of revenue for a single store. The register feed failed on days 2 and 4, so those rows hold NULL: the store traded on those days, and nobody knows for how much. Play the query and watch each running value build up row by row.

SELECT
day_num,
revenue,
SUM(revenue) OVER (
ORDER BY day_num
) AS running_total,
COUNT(revenue) OVER (
ORDER BY day_num
) AS days_counted
FROM store_sales
store_sales5 rows
day_num
revenue
1
100
2
NULL
3
80
4
NULL
5
120
Preparing the animation

The running total never turns NULL. On day 2 the frame holds 100 and a NULL, and SUM skips the NULL the way it does in a grouped query, so the total stays at 100, then sits flat again on day 4. A chart of running_total shows those flat stretches as 2 days with no sales, and nothing in the output marks them as unknown. days_counted is the column that tells the truth: COUNT(revenue) counts only known values, so any day where it fails to rise is a day with a gap.

Had day 1 been NULL, its running total would be NULL too. A frame holding nothing but NULLs gives SUM no known input, and SUM with no known input returns NULL until the first real value arrives.

Multiple Choice

sensor_readings holds reading_hour 1 with reading NULL, reading_hour 2 with reading 5 and reading_hour 3 with reading 7. What does running_total hold for hours 1 and 2?

SELECT
reading_hour,
reading,
SUM(reading) OVER (
ORDER BY reading_hour
) AS running_total
FROM sensor_readings

LAG cannot tell a gap from the edge

LAG(revenue) returns the revenue from the row before the current one, in the order OVER sets. Its optional third argument is a default for a row that has nothing before it. Before running the query, predict prev_or_zero for day 1 and for day 3.

SELECT
day_num,
revenue,
LAG(revenue, 1) OVER (
ORDER BY day_num
) AS prev_revenue,
LAG(revenue, 1, 0) OVER (
ORDER BY day_num
) AS prev_or_zero,
revenue - LAG(revenue, 1) OVER (
ORDER BY day_num
) AS change
FROM store_sales
ORDER BY day_num
Result
day_numrevenueprev_revenueprev_or_zerochange
1100NULL0NULL
2NULL100100NULL
380NULLNULLNULL
4NULL8080NULL
5120NULLNULLNULL
5 rows

Day 1 gets 0: no row comes before it, so the default applies. Day 3 gets NULL: a row does come before it, and that row's revenue is NULL. The default fills a missing row and leaves a missing value alone. In prev_revenue both cases print the same NULL, so anyone reading the output can't tell which one they're looking at. The change column fares worse. All 5 of its values are NULL, because every subtraction has a NULL on one side, the edge row included. With 2 gaps in 5 days, not a single day-over-day change survives.

Carrying the last known value forward

A common repair fills each gap with the most recent known value, so day 2 shows 100 and day 4 shows 80. It takes 2 window passes. The first labels rows with a running COUNT(revenue), which rises only on rows holding a value, so each NULL row shares a label with the known row before it. The second takes MAX(revenue) over each label with PARTITION BY. Each partition holds a single known value, and MAX skips the NULLs beside it, so it hands that value to every row in the partition.

WITH grouped AS (
SELECT
day_num,
revenue,
COUNT(revenue) OVER (
ORDER BY day_num
) AS fill_group
FROM store_sales
)
SELECT
day_num,
revenue,
fill_group,
MAX(revenue) OVER (
PARTITION BY fill_group
) AS filled_revenue
FROM grouped
ORDER BY day_num
Result
day_numrevenuefill_groupfilled_revenue
11001100
2NULL1100
380280
4NULL280
51203120
5 rows

fill_group reads 1, 1, 2, 2, 3, and each pair shares its known value. Swap COUNT(revenue) for COUNT(*) and every row gets a group of its own, so the NULL rows stay NULL. A series that opens with NULLs puts those rows in group 0, which holds no known value, so they stay NULL as well: forward fill has nothing to carry until the first value arrives. Whether carrying a value forward is honest depends on the data. A thermostat reading holds until the next reading, while yesterday's revenue says nothing about today's.

Loading the challenge
TIP
A NULL in an ORDER BY key needs a position too. Where NULLs land by default depends on the engine, and some put them first in a descending sort, so a revenue ranking can open with the unknown days. Write NULLS FIRST or NULLS LAST whenever the position matters. PARTITION BY on a nullable column sends every NULL key into a single shared partition.

NULLs in UNION operations

Daily Life
Interviews

Merging one kind of record from 2 systems usually means stacking 2 queries with UNION or UNION ALL. UNION ALL keeps every row. UNION also removes duplicate rows, and to find them it compares rows column by column under a rule that departs from = in one place: a NULL beside a NULL counts as a match. In a WHERE clause, NULL = NULL is NULL and the row is dropped. In a UNION, 2 rows that are both NULL in a column are duplicates there, and only 1 of them survives.

A shop takes orders on the web and in an app. Both systems record coupon_code, NULL when the customer used none. Each tab lists the coupon codes across both channels. Predict the row count before you open each one.

Keeps all 6 rows, including the 3 NULLs from orders placed without a coupon.
SELECT
coupon_code
FROM web_orders
UNION ALL
SELECT
coupon_code
FROM app_orders
ORDER BY coupon_code
Result
coupon_code
NULL
NULL
NULL
SPRING
SPRING
WELCOME
6 rows
The UNION result has 1 NULL row standing in for 3 orders across 2 channels, and nothing in it says which. That's fine for a list of codes in use and wrong for anything that counts: counting coupon-free orders from that result gives 1. The third tab shows the usual repair, a column recording where each row came from. Rows from different channels now differ in that column, so UNION can merge NULLs only when they share a source.

Padding a column a source never had

A third channel takes orders by phone and records no coupon at all, so phone_orders holds only order_id. Every branch of a UNION must return the same number of columns, which means the phone branch has to supply something in the coupon position. A bare NULL has no type of its own and borrows one from the other branches. Write CAST(NULL AS VARCHAR) instead. The type is then stated where the next reader can see it, and the query still has a type for the column when every branch pads it and no branch has a real column to lend.

SELECT
order_id,
coupon_code,
'web' AS channel
FROM web_orders
UNION ALL
SELECT
order_id,
CAST(NULL AS VARCHAR),
'phone'
FROM phone_orders
ORDER BY order_id
Result
order_idcoupon_codechannel
1SPRINGweb
2NULLweb
3SPRINGweb
4NULLweb
7NULLphone
8NULLphone
6 rows

Orders 2 and 4 hold NULL because the customer used no coupon. Orders 7 and 8 hold NULL because the phone system never asks. The merged column now carries 2 meanings under a single marker, and any rate built on it blends them. Over these 6 rows the coupon rate comes out at 2 in 6, a third, while the web channel, the only one that records coupons, saw a coupon on 2 of its 4 orders. The channel column lets a later query keep the meanings apart by filtering to channel = 'web' before computing the rate.

Columns match by position
UNION pairs the first column of each branch, then the second, and so on. The output takes its column names from the first branch, and aliases in later branches are ignored. A padded NULL placed in the wrong slot shifts values into the wrong columns, and when the types agree the query runs without a single error.

INTERSECT and EXCEPT compare rows under the same rule as UNION, with a NULL on each side counting as a match. The last section of this lesson depends on that.

Multiple Choice

Order 7 comes from phone_orders. After this query runs, what does its row hold in coupon_code and in channel?

SELECT
order_id,
coupon_code,
'web' AS channel
FROM web_orders
UNION ALL
SELECT
order_id,
'phone' AS channel,
CAST(NULL AS VARCHAR) AS coupon_code
FROM phone_orders

DISTINCT and NULL

Daily Life
Interviews

SELECT DISTINCT keeps one row for each different combination of values, and it judges sameness the way UNION does when it removes duplicates. In employee_metrics, 2 of the 6 employees have no department recorded. Play the query and count the NULL rows that come out.

SELECT DISTINCT
department
FROM employee_metrics
employee_metrics6 rows
name
department
Ann
Sales
Ben
NULL
Cara
Eng
Dev
Sales
Eve
NULL
Finn
Eng
Preparing the animation

The query returns 3 rows: Sales, Eng and a single NULL, which stands for both Ben and Eve. Now count the departments with COUNT(DISTINCT department) and the answer is 2. Aggregates discard NULL inputs before doing anything else, and the DISTINCT inside COUNT follows the same order, so the NULL value that SELECT DISTINCT kept never reaches the count.

SELECT
COUNT(*) AS all_rows,
COUNT(department) AS known_department,
COUNT(DISTINCT department) AS distinct_departments,
(
SELECT
COUNT(*)
FROM (
SELECT DISTINCT
department
FROM employee_metrics
) AS d
) AS distinct_rows
FROM employee_metrics
Result
all_rowsknown_departmentdistinct_departmentsdistinct_rows
6423
1 row

The 4 numbers answer 4 different questions. A pipeline check that compares distinct_rows from one step with distinct_departments from another reports a mismatch of 1 whenever the column holds a NULL, though no row is wrong. Decide which question the check asks, then use the matching count on both sides. GROUP BY department follows the SELECT DISTINCT rule and returns a single group for Ben and Eve, with a NULL key and a count of 2.

With more than one column, DISTINCT compares whole rows. ('Sales', NULL) and ('Sales', NULL) are duplicates, while ('Sales', NULL) and ('Sales', 'Berlin') are kept apart, because a NULL differs from every known value.

Defaults that merge groups

Replacing NULL with a label before deduplicating, as in COALESCE(department, 'Unknown'), makes the NULL group readable and lets COUNT(DISTINCT) include it. It also merges the NULL rows with any real value equal to the label. A source that already writes 'Unknown', or an empty string when the default is '', collapses into the same group, and the distinct count drops by 1 with no warning. Pick a label the data can't contain, or check for it first.

Multiple Choice

staff has 4 rows whose departments are 'Sales', 'Unknown', NULL and NULL. What does this query return?

SELECT
COUNT(DISTINCT COALESCE(department, 'Unknown')) AS departments
FROM staff

Comparing values that may be NULL

SQL exposes the deduplication rule as an operator you can put in your own conditions. a IS DISTINCT FROM b is TRUE when the values differ, and it treats NULL as an ordinary value: a NULL against a known value is distinct, and a NULL against a NULL is not. It always returns TRUE or FALSE. IS NOT DISTINCT FROM is its negation.

aba <> ba IS DISTINCT FROM b
11FALSEFALSE
12TRUETRUE
1NULLNULLTRUE
NULLNULLNULLFALSE

The difference matters most in change detection. tier_changes holds each customer's loyalty tier from yesterday's snapshot and from today's. Compare the 2 filters.

Keeps only rows where both tiers are known and different.
SELECT
customer_id,
old_tier,
new_tier
FROM tier_changes
WHERE old_tier <> new_tier
ORDER BY customer_id
Result
customer_idold_tiernew_tier
2SilverGold
1 row

With <>, customer 3's first tier and customer 4's lost tier both evaluate to NULL and drop out, so a change feed built on it never reports a customer joining or leaving the program. IS DISTINCT FROM returns customers 2, 3 and 4 and still skips customer 5, whose tier was unknown on both days.

Loading the challenge

NULL propagation

Daily Life
Interviews

Almost every operator and function returns NULL when any of its inputs is NULL. NULL + 5, NULL * 0, NULL > 3 and ABS(NULL) all come out NULL, because the engine cannot compute an answer from a value it does not have. Each operator follows that rule on its own, and the damage comes from chaining them. A derived column built from 4 inputs is NULL whenever any one of them is, and every column derived from it inherits the NULL, step after step down a pipeline.

order_lines holds the 4 lines of an order. Line 2 came through a checkout version that writes NULL instead of 0 when no discount applies. Trace line 2 through the expression before running the query.

SELECT
line_id,
unit_price,
quantity,
discount,
unit_price * quantity - discount AS line_total
FROM order_lines
ORDER BY line_id
Result
line_idunit_pricequantitydiscountline_total
1203555
2152NULLNULL
3401040
41251050
4 rows

unit_price * quantity for line 2 is a known 30. Subtracting NULL turns it into NULL, and the 30 is gone from the row. One stage later, SUM skips that NULL like any other, so the order's revenue loses line 2 completely and the query raises no error. Each tab below sums the 4 lines a different way.

Sums the raw expression, and SUM skips line 2's NULL total.
SELECT
SUM(unit_price * quantity - discount) AS revenue
FROM order_lines
Result
revenue
145
1 row
145
revenue with COALESCE on the total
175
revenue with COALESCE on the discount

Wrapping the total repairs nothing. By the time COALESCE sees line 2, the subtraction has already thrown away the price and quantity, so COALESCE swaps NULL for 0, and 0 adds to the sum exactly what a skipped row adds. A repair has to sit where the NULL enters, on the input whose NULL has a known meaning. Here a missing discount means no discount, so 0 is correct. A NULL quantity would be different: nobody knows how many items shipped, and defaulting it to 0 would write a guess into the revenue.

Complete the SQL

> Build the revenue query for order_lines, where line 2's discount is NULL because no discount applied. Put COALESCE where the NULL enters, so the 4 lines sum to 175.

1
2
3
4
5

Where the spread stops

Some expressions absorb a NULL instead of passing it on. Knowing which ones lets you trace a NULL forward and name every output column it reaches. In the table, x is NULL.

ExpressionResultWhy
`x + 1`NULLArithmetic needs both values
`x > 0`NULLA comparison with an unknown side is unknown
`ABS(x)`NULLMost functions return NULL for NULL input
`x IS NULL`TRUETests for NULL, so returns TRUE or FALSE
`COALESCE(x, 0)`0Replaces NULL with the first known value
`FALSE AND x > 0`FALSEFALSE settles an AND by itself
`TRUE OR x > 0`TRUETRUE settles an OR by itself
`CASE WHEN x > 0 THEN 'pos' ELSE 'other' END`'other'An unknown condition takes the ELSE branch
`SUM(x)` across rowsRow skippedAggregates ignore NULL inputs

The CASE row hides the NULL completely. A label such as CASE WHEN discount > 0 THEN 'discounted' ELSE 'full price' END files line 2 under full price. The NULL never appears in the output, so nothing downstream can tell that the label came from a guess. When a NULL input should stay visible, give it a branch of its own, WHEN discount IS NULL THEN 'unknown', placed first.

TIP
Propagation is also a tool. revenue / NULLIF(order_count, 0) turns a zero divisor into NULL, so a day with no orders gives a NULL average instead of failing the query with a division error. The NULL then flows into every column built from it and marks each one as having no answer, which reports the day more truthfully than a 0 would.
Multiple Choice

readings has a row where temp_c is NULL and alert_on is TRUE. What does status hold for that row?

SELECT
CASE
WHEN alert_on
AND temp_c > 40 THEN 'alarm'
WHEN NOT (
temp_c > 40
) THEN 'normal'
ELSE 'check sensor'
END AS status
FROM readings

Three-valued logic (NOT IN)

Daily Life
Interviews

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.

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
0 rows
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.

Multiple Choice

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
)
Choosing an anti-filter
  • ▸NOT EXISTS keeps probe rows with a NULL value and ignores NULLs in the other table. It's the safest default.
  • ▸NOT IN with IS NOT NULL in the subquery handles NULLs in the list, but drops probe rows whose own value is NULL.
  • ▸EXCEPT compares whole rows with NULL matching NULL, and returns each distinct row once.
  • ▸Plain NOT IN on a nullable column stays correct only until the first NULL lands in that column.
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.
Loading the challenge
PUTTING IT ALL TOGETHER

> A music streaming service builds its weekly artist payout table in 4 steps. Play logs from the web and mobile clients are merged and deduplicated. Each play is priced from a per-market rate table whose feed misses some days. The payout is plays times rate, summed per artist. Before money moves, a check lists plays that belong to no known artist. This week the mobile client shipped a bug that wrote NULL artist ids for a day, and the web client has never sent a market column at all.

The merge pads the web branch with CAST(NULL AS VARCHAR) for market and adds a client column, so a NULL market from the web reads as never collected.
A DISTINCT list of artist ids keeps a single NULL row for the broken plays, while a COUNT(DISTINCT artist_id) reconciliation skips it, so the check has to state which question it asks.
Forward fill over the rate table carries the last known rate across feed gaps, where the raw rate column would leave those days NULL.
Plays times rate is NULL for every unpriced play, and SUM drops those plays from the payout without an error, so the repair belongs on the rate, before the multiplication.
The orphan check uses NOT EXISTS against the artist table, so the NULL-artist plays surface for review instead of vanishing from both the paid list and the orphan list.
KEY TAKEAWAYS
Window aggregates skip NULLs, so a running total goes flat across a gap, and a running COUNT(column) shows where the gaps are.
LAG's default fills only a row with nothing before it. A NULL value in the previous row still comes back as NULL.
UNION, DISTINCT, GROUP BY, INTERSECT and EXCEPT treat 2 NULLs as the same value, while = returns NULL for them.
COUNT(DISTINCT col) skips the NULL value that SELECT DISTINCT keeps, so the 2 counts differ by 1 whenever a NULL exists.
IS DISTINCT FROM compares nullable columns and always returns TRUE or FALSE, which makes it the operator for change detection.
A NULL input turns every expression built on it into NULL until COALESCE, IS NULL, an aggregate or a settled AND or OR stops it. Repair it at the input.
NOT IN returns nothing when its list holds a NULL, and drops probe rows whose own value is NULL. NOT EXISTS keeps those rows.

Follow one NULL through windows, set operations, DISTINCT, expressions and NOT IN, and predict every row it changes.

TRUE, FALSE, and NULL walk into a bar

Category
SQL
Difficulty
advanced
Duration
26 minutes
Challenges
18 hands-on challenges

Topics covered: NULLs in window functions, NULLs in UNION operations, DISTINCT and NULL, NULL propagation, Three-valued logic (NOT IN)

Lesson Sections

  1. NULLs in window functions (concepts: Row Offsets)

    Had day 1 been NULL, its running total would be NULL too. A frame holding nothing but NULLs gives SUM no known input, and SUM with no known input returns NULL until the first real value arrives. LAG cannot tell a gap from the edge Carrying the last known value forward

  2. NULLs in UNION operations (concepts: Set Combination)

    The UNION result has 1 NULL row standing in for 3 orders across 2 channels, and nothing in it says which. That's fine for a list of codes in use and wrong for anything that counts: counting coupon-free orders from that result gives 1. The third tab shows the usual repair, a column recording where each row came from. Rows from different channels now differ in that column, so UNION can merge NULLs only when they share a source. Padding a column a source never had INTERSECT and EXCEPT compare rows

  3. DISTINCT and NULL (concepts: Deduplication)

    With more than one column, DISTINCT compares whole rows. ('Sales', NULL) and ('Sales', NULL) are duplicates, while ('Sales', NULL) and ('Sales', 'Berlin') are kept apart, because a NULL differs from every known value. Defaults that merge groups Comparing values that may be NULL

  4. NULL propagation (concepts: Null Propagation)

    Where the spread stops

  5. Three-valued logic (NOT IN) (concepts: Three-Valued Logic)

    Four ways to find artists on no playlist 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 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.