Introduction
NULL means we don't know the value. When we compare NULL with anything, the result is also unknown, so it behaves differently than normal values.
expression IS NULL expression IS NOT NULL
SELECT * FROM employees WHERE manager_id IS NULL;
SELECT * FROM orders WHERE delivery_date IS NOT NULL;
SELECT * FROM products WHERE price = NULL;
CREATE TABLE test_null ( id INT, value INT ); INSERT INTO test_null VALUES (1, 10), (2, NULL), (3, 20); SELECT id, value, value IS NULL AS is_null, value = NULL AS equals_null FROM test_null;
NULL represent in a database?col is NULL in SQL?IS NULL because = NULL does not work.col = NULL and col == NULL are invalid for NULL checks; col <> NULL also does not work.users with a column age that contains some NULL values, what will this query return?SELECT * FROM users WHERE age <> 30;
age <> 30 return UNKNOWN for NULL values, so those rows are excluded.status is not NULL:SELECT * FROM orders WHERE status <> NULL;
<> returns UNKNOWN, so no rows match.status IS NOT NULL to find rows where status is not NULL.products where discount is either NULL or greater than 10. Which query correctly returns these rows?IS NULL, not = NULL.Pass the test and this pattern moves unknown