🚨 Get Your Free NFT Certificate Mint by Completing the Web3 Exam! START NOW

Code has been added to clipboard!

Using SQL NOT in Your Database

Reading time 2 min
Published Sep 17, 2019
Updated Jan 6, 2020

SQL NOT: Definition and Syntax

By using SQL NOT, you can negate a specified condition, which means the value must be the opposite of the defined condition to be selected. The syntax for defining an SQL NOT condition is simple:

NOT condition

Just like AND and OR, SQL NOT is usually used in the WHERE clause. It can be included in various statements, such as INSERT, SELECT, UPDATE or DELETE:

Example
SELECT column_demo1, column_demo2, ...
FROM table_demo
WHERE NOT condition;

Example of Using NOT in SQL

To help you get a better idea on how SQL WHERE NOT works, we'll provide a few code examples for you to try. Get familiar with the data table we'll be using, and move on to practice.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

A Demo Database

For our example, we will be using a Developers table in the database:

ID Name City Country
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA
6 Basma Zlata Miami USA

Using SQL NOT

In the code example below, you can see a line that says WHERE NOT Country='UK';. This means the SELECT statement will only select those fields in the Developers table which have another value defined in the Country column:

Example
SELECT * FROM Developers
WHERE NOT Country='UK';

Combining AND, OR and NOT in SQL

You can combine NOT with the AND and OR operators to achieve more specific results.

In this code example, we select the fields from the Developers table which have a country defined that is NOT France AND NOT India:

Example
SELECT * FROM Developers
WHERE NOT Country='France' AND NOT Country='India';

Now, we select the fields which have the country defined as USA AND the city specified as either Miami OR New York:

Example
SELECT * FROM Developers
WHERE Country='USA' AND (City='Miami' OR City='New York');