Problem Statement
Explain how NULL affects comparisons, sorting, and aggregates. How do you handle it safely?
Explanation
NULL means unknown. Comparisons like col = NULL are unknown, so conditions fail. Use IS NULL and IS NOT NULL. Sort order places NULLs either first or last depending on the engine or explicit NULLS FIRST or LAST.
Aggregates skip NULLs except COUNT(*). Use COALESCE to substitute defaults, and design constraints to avoid surprise NULLs where business rules require values.
Code Solution
SolutionRead Only
SELECT COALESCE(phone,'N/A') AS phone FROM users ORDER BY phone NULLS LAST;
