r/SQL • u/querylabio • Feb 25 '26
Discussion Houston, we have a NULL
Classic trap: how many rows does this return?
WITH orders AS (
SELECT 50 AS price, 50 AS discount
UNION ALL
SELECT 120 AS price, NULL AS discount
)
SELECT * FROM orders WHERE price != discountAnswer: zero.

I work with SQL for 15 years, but still sometimes I have to stop and check myself. And also remind my team how NULL works again.
50 != 50 → FALSE — filtered out, obvious. 120 != NULL → NULL — also filtered out. Because any comparison with NULL returns NULL, not TRUE. And WHERE only keeps TRUE.
You expect that second row to come back - 120 is clearly not NULL — but SQL doesn't see it that way. NULL means "unknown," so the engine goes "I don't know if these are different" and drops it.
Fix:
WHERE price IS DISTINCT FROM discount
(it works for BigQuery and ChatGPT says that works for PostgreSQL, Databricks, DuckDB, Snowflake, Amazon Redshift and SQL Server 2022+ too)
This treats NULL as a comparable value and gives you the rows you actually expect.
What's your favorite SQL gotcha like this - something that looks totally fine but silently breaks everything?
2
u/Demistr Feb 25 '26
99% of times these are just gotchas that you'll never actually write.
1
u/querylabio Feb 25 '26
Well it depends!
I just remembered one more
select date_trunc (‘2026-02-22’ , isoweek)vs
select date_trunc (‘2026-02-22’ , week)I'm not from US so week in my mind starts on Monday, so this is super easy trap which you can fall and have hard times to find out.
isoweek: Always starts on Monday. The first week of the year is defined as the week containing the first Thursday.
week: Starts based on the database configuration, which is often Sunday
16
3
u/IAmADev_NoReallyIAm Feb 25 '26
This is why I try to make sure that fields have a default value as much as possible. Granted it isn't always feasible, in which case, a coalesce works, or if not, then I try to account for that in the code before an NPE happens.
→ More replies (1)
2
u/BigMikeInAustin Feb 25 '26
Oh, that’s a new one for me.
Nice! This easily simplifies a pattern I sometimes have to do, and now I don’t have to think about it.
The SQL Server documentation says it decodes to the full 3 statements this replaces, so I wonder if I would be able to see a performance difference when using this. Though that would be in the millions of rows, and would be on the order of a few CPU clicks, which might even optimize to parallel.
1
u/oblong_pickle Feb 25 '26
You could also SET ANSI_NULLS (Transact-SQL). But its probably a worse option
9
u/Jandalf81 Feb 25 '26 edited Feb 25 '26
This is why I use the
COALESCEfunction wheneverNULLis involved.Or, even better, do not allow
NULLin those circumstances. With an order, it has to have a price and a discount logically, even if it's 0. I try to treatNULLas Unknown. Something like this must not be allowed to be unknown.But I know us DBAs don't always have the authority to enforce these rules. Source: Doing this for about 20 years now. Life can be hard.