Learn SQL/Intermediate/NULL Handling/SQL NULL in Comparisons

SQL NULL in Comparisons

Every comparison operator — =, <>, <, >, <=, >= — returns UNKNOWN whenever either side is NULL, with zero exceptions.

What & Why

This rule has no exceptions: any standard comparison operator involving NULL on either side evaluates to UNKNOWN, not TRUE or FALSE. This applies identically to =, <>, <, >, <=, and >=.

See How It Works

BUSINESS QUESTION

Product wants to inspect the TRUE, FALSE, or UNKNOWN result of nps_score >= 8 on every feedback row.

iduser_idfeature_idratingnps_scorecommentcreated_at
1101159Clear and useful2024-01-25 12:15:00+00
210224NULLExport worked well2024-02-20 15:40:00+00
3103147Helpful setup flow2024-03-18 09:05:00+00
410433NULLNeeds clearer timing2024-04-08 17:30:00+00
Watch the comparison return TRUE, FALSE, or UNKNOWNExample 1 of 4
nps_score >= 8
idnps_scorecomparison_result
19TRUE
2NULL?
37?
4NULL?

The known score 9 satisfies nps_score >= 8.

EXAMPLE QUERY
SELECT
  id,
  nps_score,
  nps_score >= 8 AS comparison_result
FROM product.feedback
ORDER BY id;
Now You Try

Practice this concept

Product inspects the TRUE, FALSE, or UNKNOWN result of nps_score >= 8 for every feedback row.

Available schema
product

Prefix tables with product.table_name.

idnps_scorecomparison_result
product.feedback
ColumnType
idinteger
user_idinteger
feature_idinteger
ratinginteger
nps_scoreinteger
commenttext
created_attimestamp with time zone
moderation_buckettext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario