Introduction
In this chapter, we will learn about the IS NOT NULL
operator in MySQL. The IS NOT NULL
operator is used to filter records that do not have NULL
values in a specified column. NULL
represents a missing or undefined value in a database. By using IS NOT NULL
, you can ensure that your queries return only rows where the column has a valid value. We will cover the syntax, examples, and important considerations for using the IS NOT NULL
operator.
Syntax
The basic syntax for the IS NOT NULL
operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IS NOT NULL;
column_name
: The column to check for non-NULL values.
Using IS NOT NULL Operator
Example with SELECT
SELECT first_name, last_name, email
FROM students
WHERE email IS NOT NULL;
This example retrieves rows where the email
column does not have NULL
values.
Example with UPDATE
UPDATE students
SET email = '[email protected]'
WHERE email IS NOT NULL;
This example updates the email
column to ‘[email protected]’ for rows where the email
column does not have NULL
values.
Example with DELETE
DELETE FROM students
WHERE email IS NOT NULL;
This example deletes rows where the email
column does not have NULL
values.
Full Example
Let’s go through a full example where we create a table, insert data into it, and use the IS NOT NULL
operator to filter records.
- Create a Database:
CREATE DATABASE school;
- Select the Database:
USE school;
- Create a Table:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100),
enrollment_date DATE
);
- Insert Data into the Table:
INSERT INTO students (first_name, last_name, email, enrollment_date) VALUES
('Rahul', 'Sharma', '[email protected]', '2023-07-01'),
('Priya', 'Singh', NULL, '2023-07-02'),
('Amit', 'Kumar', '[email protected]', '2023-07-03'),
('Neha', 'Verma', NULL, '2023-07-04'),
('Sahil', 'Mehta', '[email protected]', '2023-07-05');
- Use the IS NOT NULL Operator with SELECT:
SELECT first_name, last_name, email
FROM students
WHERE email IS NOT NULL;
Output
first_name | last_name | |
---|---|---|
Rahul | Sharma | [email protected] |
Amit | Kumar | [email protected] |
Sahil | Mehta | [email protected] |
- Use the IS NOT NULL Operator with UPDATE:
UPDATE students
SET email = '[email protected]'
WHERE email IS NOT NULL;
- Verify the Update:
SELECT first_name, last_name, email
FROM students
WHERE email = '[email protected]';
Output
first_name | last_name | |
---|---|---|
Rahul | Sharma | [email protected] |
Amit | Kumar | [email protected] |
Sahil | Mehta | [email protected] |
- Use the IS NOT NULL Operator with DELETE:
DELETE FROM students
WHERE email IS NOT NULL;
- Verify the Deletion:
SELECT first_name, last_name, email
FROM students
WHERE email IS NOT NULL;
Output
(empty result set)
Important Considerations
- NULL Values:
NULL
is not the same as an empty string or zero. It represents an unknown or missing value. - Comparison with NULL: You cannot use regular comparison operators like
=
or<>
to check forNULL
values. Always useIS NULL
orIS NOT NULL
for such checks. - Performance: Checking for
NULL
or non-NULL values can impact performance, especially on large datasets. Ensure appropriate indexing and optimize queries to improve performance.
Conclusion
The IS NOT NULL
operator is used for filtering data based on non-NULL values in MySQL queries. This chapter covered how to use the IS NOT NULL
operator with SELECT
, UPDATE
, and DELETE
statements, provided examples, and discussed important considerations.