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.

web_orders
order_idcoupon_code
1SPRING
2NULL
3SPRING
4NULL
app_orders
order_idcoupon_code
5NULL
6WELCOME

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
Result
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
Result
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
Result
coupon_codechannel
NULLapp
WELCOMEapp
NULLweb
SPRINGweb

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
web_orders
order_idcoupon_code
1SPRING
2NULL
3SPRING
4NULL
phone_orders
order_id
7
8
Result
order_idcoupon_codechannel
1SPRINGweb
2NULLweb
3SPRINGweb
4NULLweb
7NULLphone
8NULLphone

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

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.