1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. Why can WHERE x NOT IN (SELECT y FROM t) return no rows…

Added 30 Nov -0001

Why can WHERE x NOT IN (SELECT y FROM t) return no rows when the subquery yields a NULL?

  1. Because NOT IN converts the NULL to zero and then compares numerically, matching every zero value
  2. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe Correct answer
  3. Because the subquery is executed only when it contains no NULL values, and otherwise the whole subquery is skipped
  4. Because NOT IN is not valid with a subquery at all, so the parser rejects the statement outright

Correct answer

B. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe

17 views

MCQSEXAM

Explanation

NOT IN and NULL: the predicate expands to a chain of ANDed comparisons, and one UNKNOWN makes the whole expression UNKNOWN, which filters the row out. A single NULL in the subquery can therefore silently empty the result, a classic production bug. NOT EXISTS with a correlated condition, or filtering the subquery with WHERE y IS NOT NULL, restores the intended semantics.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs