NULLs in UNION operations
Concepts covered: Set Combination
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.
| order_id | coupon_code |
|---|---|
| 1 | SPRING |
| 2 | NULL |
| 3 | SPRING |
| 4 | NULL |
| order_id | coupon_code |
|---|---|
| 5 | NULL |
| 6 | WELCOME |
UNION ALL
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| coupon_code |
|---|
| NULL |
| NULL |
| NULL |
| SPRING |
| SPRING |
| WELCOME |
UNION
Removes duplicates, so 3 rows remain: SPRING, WELCOME and a single NULL.
SELECT
coupon_code
FROM web_orders
UNION
SELECT
coupon_code
FROM app_orders
ORDER BY coupon_code| coupon_code |
|---|
| NULL |
| SPRING |
| WELCOME |
With source
Labels each row with its channel, so NULLs merge only inside a channel and 1 NULL row per channel survives.
SELECT
coupon_code,
'web' AS channel
FROM web_orders
UNION
SELECT
coupon_code,
'app' AS channel
FROM app_orders
ORDER BY channel, coupon_code| coupon_code | channel |
|---|---|
| NULL | app |
| WELCOME | app |
| NULL | web |
| SPRING | web |
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| order_id | coupon_code |
|---|---|
| 1 | SPRING |
| 2 | NULL |
| 3 | SPRING |
| 4 | NULL |
| order_id |
|---|
| 7 |
| 8 |
| order_id | coupon_code | channel |
|---|---|---|
| 1 | SPRING | web |
| 2 | NULL | web |
| 3 | SPRING | web |
| 4 | NULL | web |
| 7 | NULL | phone |
| 8 | NULL | phone |
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.
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.
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- coupon_code holds NULL and channel holds 'phone', since the aliases line them up
- The query fails, because the second branch's aliases disagree with the first's
- The query fails, because 'phone' cannot be stored in the coupon_code column
- coupon_code holds 'phone' and channel holds NULL, since columns match by position
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.