Search This Blog

SQL: IS NOT NULL Condition

 

SQL: IS NOT NULL Condition

This SQL tutorial explains how to use the SQL IS NOT NULL condition with syntax and examples.

Description

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the IS NOT NULL condition in SQL is:

expression IS NOT NULL

Parameters or Arguments

expression
The expression to test for a NOT NULL value.

DDL/DML for Examples

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example - Using IS NOT NULL with the SELECT Statement

When testing for a non-NULL value, IS NOT NULL is the recommended comparison operator to use in SQL. Let's start by looking at an example that shows how to use the IS NOT NULL condition in a SELECT statement.

In this example, we have a table called products with the following data:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following SQL statement:


SELECT *
FROM products
WHERE category_id IS NOT NULL;

There will be 6 records selected. These are the results that you should see:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25

This example will return all records from the products table where the customer_id does not contain a NULL value.

Example - Using IS NOT NULL with the UPDATE Statement

Next, let's look at an example of how to use the IS NOT NULL condition in an UPDATE statement.

In this example, we have a table called customers with the following data:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

Enter the following UPDATE statement:


UPDATE customers
SET favorite_website = 'google.com'
WHERE favorite_website IS NOT NULL;

There will be 5 records updated. Select the data from the customers table again:

SELECT * FROM customers;

These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanegoogle.com
6000FergusonSamanthagoogle.com
7000ReynoldsAllengoogle.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

This example will update all favorite_website values in the customers table to 'google.com' where favorite_website contains a NULL value. As you can see, the favorite_website has been updated all in but 1 row.

Example - Using IS NOT NULL with the DELETE Statement

Next, let's look at an example of how to use the IS NULL condition in a DELETE statement.

In this example, we have a table called orders with the following data:

order_idcustomer_idorder_date
170002016/04/18
250002016/04/18
380002016/04/19
440002016/04/20
5NULL2016/05/01

Enter the following DELETE statement:


DELETE FROM orders
WHERE customer_id IS NOT NULL;

There will be 4 records deleted. Select the data from the orders table again:

SELECT * FROM orders;

These are the results that you should see:

order_idcustomer_idorder_date
5NULL2016/05/01

This example will delete all records from the orders table where the customer_id does not contain a NULL value.

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...