Select Null in SQL
SELECT NULL in SQL
The NULL value in SQL represents missing or unknown data. You can use NULL in SELECT statements to explicitly select a NULL value, or to check for NULL in conditions.
1. Selecting a NULL literal
You can select a NULL value directly (often for testing or as a placeholder):
SELECT NULL;Result: Returns one row with NULL value.
You can also select NULL alongside other columns:
SELECT id, name, NULL AS some_columnFROM employees;Here, some_column will be NULL for every row.
2. Checking for NULL in WHERE
Since NULL means unknown, you cannot use = NULL. Instead, use:
SELECT * FROM employeesWHERE column_name IS NULL;Or to find non-null values:
SELECT * FROM employeesWHERE column_name IS NOT NULL;3. Using NULL with functions
You can use functions like COALESCE or IFNULL to replace NULL with a default value:
SELECT name, COALESCE(phone, 'No Phone') AS phone_numberFROM employees;Summary:
SELECT NULLreturns a null literal.Use
IS NULLorIS NOT NULLto check for nulls in conditions.Use
COALESCEto handle null values gracefully.
Want examples of working with NULL in real queries?