NULL Handling questions and answers

Three-valued logic, IS NULL, COALESCE and NULL in aggregates. Practise null handling questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample null handling questions with worked answers

Question 1

In SQL's three-valued logic, what does the comparison NULL = NULL evaluate to?

  1. UNKNOWN
  2. TRUE
  3. FALSE
  4. A syntax error

Answer: A. UNKNOWN

Explanation: NULL stands for a missing or unknown value, so comparing two unknowns cannot be called true or false. Every ordinary comparison that involves NULL gives UNKNOWN, and WHERE keeps only rows whose condition is TRUE.

Question 2

A table Voter has 10 rows, and the city column is NULL in exactly 3 of them. The query SELECT COUNT(*) FROM Voter WHERE city = 'Pune' returns 4. What does SELECT COUNT(*) FROM Voter WHERE city <> 'Pune' return?

  1. 6
  2. 7
  3. 3
  4. 0

Answer: C. 3

Explanation: The 3 NULL rows satisfy neither city = 'Pune' nor city <> 'Pune', because both comparisons are UNKNOWN for them. That leaves 10 - 4 - 3 = 3 rows with a known city other than Pune.

Question 3

In SQL's three-valued logic, which of these expressions evaluates to FALSE (not to UNKNOWN)?

  1. NULL AND TRUE
  2. NULL OR FALSE
  3. NULL AND FALSE
  4. NOT (NULL = NULL)

Answer: C. NULL AND FALSE

Explanation: AND is FALSE as soon as one side is FALSE, whatever the unknown side might be, so NULL AND FALSE is FALSE. NULL AND TRUE and NULL OR FALSE depend on the unknown value and stay UNKNOWN, and NOT of UNKNOWN is UNKNOWN.

All 20 null handling questions with answers

NULL Handling: common questions

What are null handling questions?

Three-valued logic, IS NULL, COALESCE and NULL in aggregates. They appear in the sql section of many placement tests.

Where can I practise null handling questions with answers for free?

Right here on LogicWiz Exams. Every null handling question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise null handling?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of null handling questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep