IN, BETWEEN, EXISTS: Mistakes
Module: Advanced Filtering
SELECT * FROM products WHERE price NOT IN (SELECT price FROM discounted WHERE price IS NULL);
SELECT * FROM products WHERE price NOT IN (SELECT price FROM discounted WHERE price IS NOT NULL);
NOT IN returns NULL if list contains NULL, causing no rows to match. Filter out NULLs or use NOT EXISTS.
Avoid NOT IN with nullable columns, use NOT EXISTS instead
High
NOT IN with NULL values returns no results
SELECT * FROM orders WHERE total BETWEEN 50 AND 10;
SELECT * FROM orders WHERE total BETWEEN 10 AND 50;
BETWEEN expects lower value first, higher value second. Reversed order returns no results.
Always put lower bound first in BETWEEN
High
BETWEEN requires lower bound first