- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL IS NULL
The IS NULL operator in SQL is used to filter rows where the column value is NULL. A NULL value represents missing or undefined data.
Basic Syntax of IS NULL
SELECT column1, column2, ...
FROM table_name
WHERE column_name IS NULL;
column_name: The column to check forNULLvalues.
Key Features of IS NULL
1. Check for NULL Values
The IS NULL operator retrieves rows where the column value is NULL.
SELECT name
FROM employees
WHERE department IS NULL;
- Retrieves names of employees where the department is NULL.
2. Check for Non-NULL Values
Use IS NOT NULL to filter rows where the column does not contain NULL values.
SELECT name
FROM employees
WHERE department IS NOT NULL;
- Retrieves names of employees where the department has a defined value.
3. Using IS NULL with Multiple Conditions
Combine IS NULL with other conditions using AND or OR.
SELECT name
FROM employees
WHERE department IS NULL
OR salary > 5000;
- Retrieves employees with no department or a salary greater than 5000.
Example Queries
1. Find Rows with NULL Values
SELECT order_id, customer_id
FROM orders
WHERE customer_id IS NULL;
- Retrieves orders where the customer_id is NULL.
2. Exclude Rows with NULL Values
SELECT product_id, product_name
FROM products
WHERE price IS NOT NULL;
- Retrieves products where the price is defined.
3. Filter Rows Based on NULL and Non-NULL Conditions
SELECT id, email
FROM users
WHERE email IS NULL
AND phone_number IS NOT NULL;
- Retrieves users whose email is missing but the phone number is defined.
Important Notes
NULL is Not the Same as 0 or '' (Empty String):
- A
NULLvalue indicates missing or unknown data. - Use
IS NULLinstead of equality operators like=.
NULL in Arithmetic Operations:
- Any arithmetic operation involving
NULLresults in NULL.
Summary
The IS NULL operator is used to filter rows where a column value is NULL (missing or undefined). Use IS NOT NULL to filter rows with defined values. Proper handling of NULL is essential to ensure accurate query results.