Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Databases, SQL & BISQL languageJuniorCommonTheory

Why does NULL require special handling in SQL queries and tests?

Answer

NULL represents an unknown or absent value, so ordinary comparisons with it usually produce UNKNOWN rather than TRUE or FALSE. Use IS NULL or IS NOT NULL instead of equals, and remember that NULL affects joins, aggregates, NOT IN, sorting and uniqueness differently across database products. Test missing values explicitly at input, storage, query, calculation, export and UI boundaries because silent null handling can change both row counts and business totals.

Strong answer includes

  • understands SQL three-valued logic
  • uses IS NULL rather than equality
  • covers joins, aggregates and end-to-end null propagation

Practical example

NULL comparison: wrong and correct forms
Dialect · Portable SQLRuntime · SQLite sample DB

Runs against the resettable SQLite sample database used by the playground.

SQL
SQLite · sample DB · browser sandbox
Result
Run the query to see the result.

NULL is not an ordinary value, so equality does not test for absence. NOT EXISTS is also preferable to NOT IN when the subquery can contain NULL because three-valued logic can otherwise make every comparison UNKNOWN.

Expected result: IS NULL returns missing phones; the NOT EXISTS query returns customers without matching orders.

#sql#database#null#three-valued-logic

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recovery