SQL IS NOT NULL
What is SQL IS NOT NULL?
The SQL IS NOT NULL operator is used to filter rows in a database table where a specified column's value is not NULL. It is the opposite of the IS NULL operator. The IS NOT NULL operator allows you to select rows where a particular column contains data, ensuring that the data exists and is not missing.
When you would use it
You would use the SQL IS NOT NULL operator when you need to filter data from a table based on whether a column's value is not NULL. This is useful when you want to retrieve records with data present in a specific column or attribute, ensuring that the data is not missing or undefined.
Syntax
The syntax for using the IS NOT NULL operator is as follows:
SELECT columns
FROM table_name
WHERE column_name IS NOT NULL;
columns
: The columns you want to retrieve in the query.table_name
: The name of the table containing the data.column_name
: The name of the column you want to filter based on whether it does not contain NULL values.
Parameter values
columns
: The columns you want to retrieve in your query.table_name
: The name of the table where the data is stored.column_name
: The name of the column you want to filter based on whether it does not contain NULL values.
Example query
Suppose we have a table named "customers" with columns "customer_id," "first_name," and "last_name." We want to retrieve customers who have both a first name and a last name specified (i.e., both "first_name" and "last_name" columns are not NULL):
SELECT customer_id, first_name, last_name
FROM customers
WHERE first_name IS NOT NULL AND last_name IS NOT NULL;
In the above query, we use the IS NOT NULL operator to filter customers who have both first names and last names specified.
Example table response
Assuming the "customers" table contains the following data:
| customer_id | first_name | last_name |
|------------ |----------- | ---------- |
| 1 | John | Smith |
| 2 | Alice | NULL |
| 3 | Bob | Brown |
| 4 | Carol | Johnson |
| 5 | David | NULL |
The query mentioned earlier would return the following result:
| customer_id | first_name | last_name |
|------------ |----------- | ---------- |
| 1 | John | Smith |
| 3 | Bob | Brown |
| 4 | Carol | Johnson |
This result includes customers with both first names and last names specified, excluding those with NULL values in either column.
Use cases
- Identifying records with complete and non-missing data.
- Retrieving records that have specific attributes or columns populated.
- Ensuring data quality and completeness in database queries.
SQL languages this is available for
The SQL IS NOT NULL operator is a standard SQL feature and is available in most relational database management systems (RDBMS) that support SQL. This includes popular RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. The specific syntax and behavior may vary slightly between database systems, but the fundamental functionality remains the same.