Preamble
The SQL condition NOT (sometimes called the NOT operator) is used to override the condition in the WHERE sentence of the SELECT, INSERT, UPDATE or DELETE operator.
Syntax of NOT condition in SQL
NOT condition_id
Arguments or parameters
- To include an entry in the result set, the opposing condition must be chosen. condition – This is a condition for denial.
Use NOT with IN condition
Let’s begin by looking at how NOT can be used with the IN condition. The NOT IN condition is created when the NOT operator is combined with the IN condition. The expression will be checked to see if it is not on the list.
In this illustration, the product table contains the information below:
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
Type the SQL query in the box provided.
SELECT *
FROM prods
WHERE prod_name NOT IN ('Pear', 'Banana', 'Bread');
There will be four entries chosen. You should see the outcomes listed below.
| prod_id | prod_name | cat_id |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
The products table will be searched for all rows in this example where prod_name does not match Pear, Banana, or Bread. In some cases, it is more effective to list the values you do not want rather than the values you do.
The following SQL statement is equivalent to this.
SELECT *
FROM prods
WHERE prod_name <> 'Pear'
AND prod_name <> 'Banana'
AND prod_name <> 'Bread';
Using NOT with IS NULL condition
When you combine the NOT operator with the IS NULL condition, you create the IS NOT NULL condition which allows you to check a value other than NULL. This is a recommended comparison statement for use in SQL when checking values other than NULL.
Let’s look at an example which shows how to use the IS NOT NULL condition in a query.
Using the same products as in the previous example.
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
Enter the following SQL statement.
SELECT *
FROM prods
WHERE cat_id IS NOT NULL;
6 records will be selected. Here are the results that you should get.
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
In this example, all records from the products table will be returned where customer_id is not NULL.
Using NOT with LIKE condition
Next, let’s look at an example of using the NOT operator with the LIKE condition.
In this illustration, we have a table with the information below:
| suppl_id | suppl_name | city_id | state_id |
| 100 | Yandex | Moscow | Russian |
| 200 | Lansing | Michigan | |
| 300 | Oracle | Redwood City | California |
| 400 | Bing | Redmond | Washington |
| 500 | Yahoo | Sunnyvale | Washington |
| 600 | DuckDuckGo | Paoli | Pennsylvania |
| 700 | Qwant | Paris | France |
| 800 | Menlo Park | California | |
| 900 | Electronic Arts | San Francisco | California |
Let’s look at each entry in the Suppliers table where the letter “o” is absent from Supplier_name. Type the SQL query in the box provided.
SELECT *
FROM suppls
WHERE suppl_name NOT LIKE '%o%';
Four entries will be chosen. These are the results you want to achieve.
| suppl_id | suppl_name | city_id | state_id |
| 100 | Yandex | Moscow | Russian |
| 300 | Oracle | Redwood City | California |
| 400 | Bing | Redmond | Washington |
| 700 | Qwant | Paris | France |
In this example, there are four entries in the suppliers table where suppl_name does not contain liters ‘o’.
Using NOT with BETWEEN condition
The NOT operator can also be combined with the BETWEEN condition to create the NOT BETWEEN condition. Let us consider an example that shows how to use the NOT BETWEEN condition in a query.
In this example, we have a table with the following data:
| custom_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 5000 | Selena | Gomez | bing.com |
| 6000 | Mila | Kunis | yahoo.com |
| 7000 | Tom | Cruise | oracle.com |
| 8000 | Johnny | Depp | NULL |
| 9000 | Russell | Crowe | google.com |
Enter the following SQL statement.
SELECT *
FROM customs
WHERE custom_id NOT BETWEEN 5000 AND 8000;
Two records will be chosen. Here are the results that you should get.
| custom_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 9000 | Russell | Crowe | google.com |
This will return all lines where customer_id is not in the range between 5000 and 8000 inclusive. This would be equivalent to the next SELECT operator.
SELECT *
FROM customs
WHERE custom_id < 5000
OR custom_id > 800;
Using NOT with EXISTS condition
Finally, the NOT condition can be combined with the EXISTS condition to create the NOT EXISTS condition. Let’s look at an example which shows how to use the NOT EXISTS condition in SQL.
In this example we have a table with the following data:
| custom_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 5000 | Selena | Gomez | bing.com |
| 6000 | Mila | Kunis | yahoo.com |
| 7000 | Tom | Cruise | oracle.com |
| 8000 | Johnny | Depp | NULL |
| 9000 | Russell | Crowe | google.com |
also a table with the following information:
| ord_id | custom_id | ord_date |
| 1 | 7000 | 2019/06/18 |
| 2 | 5000 | 2019/06/18 |
| 3 | 8000 | 2019/06/19 |
| 4 | 4000 | 2019/06/20 |
| 5 | NULL | 2019/07/01 |
Enter the following SQL statement.
SELECT *
FROM customs
WHERE DOES NOT EXIST
(SELECT *
FROM ords
WHERE customs.custom_id = ords.custom_id);
Two entries will be chosen. The outcomes you should see are listed below.
| custom_id | f_name | l_name | fav_website |
| 6000 | Mila | Kunis | yahoo.com |
| 9000 | Russell | Crowe | google.com |
Since the order table does not contain any records for this custom_id, all records from the customer’s table are returned in this example.
AND OR and NOT Operators in sql
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
$11B at Stake — How Supply-Chain Delays Are Forcing Airlines to Fly Older Planes
- 7 November 2025
- Software Engineering
Introduction The aviation industry is entering 2025 at a critical inflection point. According to a joint report by the International Air Transport Association (IATA) and Oliver Wyman, global airlines could face over $11 billion in additional costs this year due to persistent supply-chain disruptions. The most visible consequence is the forced reliance on older, less … Continue reading “$11B at Stake — How Supply-Chain Delays Are Forcing Airlines to Fly Older Planes”
Enhancing Cost Estimation and Attribution in the Technology Sector: How Enteros Uses AI Performance Management to Drive Financial and Operational Efficiency
- 6 November 2025
- Database Performance Management
Introduction In the dynamic world of the technology sector, cost estimation and attribution have become as critical as innovation itself. As companies expand their cloud infrastructures, deploy AI-driven workloads, and manage vast databases across multi-cloud ecosystems, the challenge lies in maintaining both financial precision and performance optimization. Technology enterprises need a clear understanding of where … Continue reading “Enhancing Cost Estimation and Attribution in the Technology Sector: How Enteros Uses AI Performance Management to Drive Financial and Operational Efficiency”
Optimizing Retail Budgeting and Performance: How Enteros Combines AI SQL and AI Performance Management to Transform Database Efficiency
Introduction In the fast-paced retail sector, success depends on delivering seamless customer experiences, managing inventory efficiently, and controlling operational costs — all while keeping up with dynamic market demands. Retailers today rely on a digital ecosystem powered by databases, SaaS platforms, and AI technologies to manage everything from transactions and supply chains to personalized recommendations. … Continue reading “Optimizing Retail Budgeting and Performance: How Enteros Combines AI SQL and AI Performance Management to Transform Database Efficiency”
Revolutionizing the Fashion Sector: How Enteros Leverages Generative AI and AI Performance Management to Optimize SaaS Database Efficiency
- 5 November 2025
- Database Performance Management
Introduction The global fashion industry has always been a beacon of creativity, speed, and transformation. From runway collections to e-commerce platforms, the sector thrives on rapid innovation and data-driven decision-making. In today’s digital-first world, fashion enterprises—from luxury retailers to fast-fashion brands—are evolving into technology-driven organizations, heavily dependent on SaaS platforms, AI tools, and cloud databases … Continue reading “Revolutionizing the Fashion Sector: How Enteros Leverages Generative AI and AI Performance Management to Optimize SaaS Database Efficiency”