Search This Blog

SQL: NOT Condition

 

SQL: NOT Condition

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

Description

The SQL NOT condition (sometimes called the NOT Operator) is used to negate a condition in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the NOT condition in SQL is:

NOT condition

Parameters or Arguments

condition
This is the condition to negate. The opposite of the condition must be met for the record to be included in the result set.

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 NOT with the IN Condition

Let's start by looking at how to use NOT with the IN condition. When we use the NOT operator with the IN condition, we create a NOT IN condition. This will test to see if an expression is not in a list.

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 product_name NOT IN ('Pear', 'Banana', 'Bread');

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

product_idproduct_namecategory_id
3Orange50
4Apple50
6Sliced Ham25
7KleenexNULL

This example would return all rows from the products table where the product_name is not Pear, Banana or Bread. Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.

It is equivalent to the following SQL statement:


SELECT *
FROM products
WHERE product_name <> 'Pear'
AND product_name <> 'Banana'
AND product_name <> 'Bread';

Example - Using NOT with the IS NULL Condition

When you combine the NOT operator with the IS NULL condition, you create an IS NOT NULL condition that allows you to test for a non-NULL value. This is the recommended comparison operator to use in SQL when testing for non-NULL values. Let's look at an example that shows how to use the IS NOT NULL condition in a query.

Using the same products as the previous example:

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 NOT with the LIKE Condition

Next, let's look at an example of how to use the NOT operator with the LIKE condition.

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

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Let's look for all records in the suppliers table where the supplier_name does not contain the letter 'o'. Enter the following SQL statement:


SELECT *
FROM suppliers
WHERE supplier_name NOT LIKE '%o%';

There will be 1 record selected. These are the results that you should see:

supplier_idsupplier_namecitystate
400Kimberly-ClarkIrvingTexas

In this example, there is only one record in the suppliers table where the supplier_name does not contain the letter 'o'.

Example - Using NOT with the BETWEEN Condition

The NOT operator can also be combined with the BETWEEN condition to create a NOT BETWEEN condition. Let's explore an example that shows how to use the NOT BETWEEN condition in a query.

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 SQL statement:


SELECT *
FROM customers
WHERE customer_id NOT BETWEEN 5000 AND 8000;

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

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
9000JohnsonDerekgoogle.com

This would return all rows where the customer_id was NOT between 5000 and 8000, inclusive. It would be equivalent to the following SELECT statement:


SELECT *
FROM customers
WHERE customer_id < 5000
OR customer_id > 8000;

Example - Using NOT with the EXISTS Condition

Finally, the NOT condition can be combined with the EXISTS condition to create a NOT EXISTS condition. Let's look at an example that shows how to use the NOT EXISTS condition in SQL.

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

And 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 SQL statement:


SELECT *
FROM customers
WHERE NOT EXISTS
  (SELECT * 
   FROM orders
   WHERE customers.customer_id = orders.customer_id);

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

customer_idlast_namefirst_namefavorite_website
6000FergusonSamanthabigactivities.com
9000JohnsonDerekgoogle.com

This example would return all records from the customers table where there are no records in the orders table for the given customer_id.

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...