1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. A subquery used with NOT IN returns a NULL among its…

Added 30 Nov -0001

A subquery used with NOT IN returns a NULL among its values. What happens to the outer query's result?

  1. It returns the same rows as NOT EXISTS would, since NULLs are ignored by NOT IN
  2. It raises an error, because NOT IN refuses subqueries that can produce NULL
  3. It returns only the rows whose key is itself NULL, treating NULL as a match
  4. It returns no rows at all, because every comparison with NULL yields unknown Correct answer

Correct answer

D. It returns no rows at all, because every comparison with NULL yields unknown

16 views

MCQSEXAM

Explanation

Three-valued logic makes x NOT IN (1, 2, NULL) equivalent to x <> 1 AND x <> 2 AND x <> NULL, and that last term is unknown for every x, so the filter never passes. NOT EXISTS is immune because it tests row presence rather than value equality. Add WHERE col IS NOT NULL inside the subquery, or switch to NOT EXISTS, to keep the intended semantics.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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