Content
NULL represents missing or unknown data. Understanding NULL behavior is crucial for accurate queries.
Check for NULL
SELECT * FROM customers WHERE phone IS NULL;
Check for NOT NULL
SELECT * FROM customers WHERE email IS NOT NULL;
NULL in Comparisons
-- This WON'T work!
SELECT * FROM customers WHERE phone = NULL;
-- Use IS NULL instead
SELECT * FROM customers WHERE phone IS NULL;
COALESCE - Replace NULL
SELECT name, COALESCE(phone, 'No phone') as phone FROM customers;
IFNULL (MySQL)
SELECT name, IFNULL(phone, 'N/A') as phone FROM customers;
NULLIF
SELECT NULLIF(discount, 0) as discount FROM products;
COUNT and NULL
SELECT COUNT(*) as total, COUNT(phone) as with_phone FROM customers;
Handle NULL with AI2sql
AI2sql correctly handles NULL in generated queries.


