SQL NOT
Anonymous contributor
Published May 22, 2021Updated Aug 18, 2022
Contribute to Docs
The NOT operator is used to query for items in a table that return NOT TRUE for some condition(s).
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE column NOT condition;
The condition will usually involve comparison operators such as the following:
=: Equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to!=: Not equal
Example 1
To return items without abc in their sequence:
SELECT *FROM inventoryWHERE item_name NOT LIKE '%abc%';
Example 2
To return items not within the ('Cat', 'Dog', 'Fish') list:
SELECT *FROM inventoryWHERE item_name NOT IN ('Cat', 'Dog', 'Fish');
Example 3
To return all items from the inventory table that do not exist within the store table by item_id:
SELECT *FROM inventoryWHERE NOT EXISTS (SELECT *FROM storeWHERE inventory.item_id = store.item_id);
Example 4
To return items that do not have a price within 10 and 12:
SELECT *FROM inventoryWHERE price NOT BETWEEN 10 AND 12;
Example 5
To return items that do not contain NULL values in their sequence:
SELECT *FROM inventoryWHERE item_name IS NOT NULL;
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.18 hours
- Learn how to query SQL databases and design relational databases to efficiently store large quantities of data.
- Includes 5 Courses
- With Certificate
- Beginner Friendly.13 hours