NOT — Exclude Matching Conditions

The NOT keyword in SQL is used to exclude rows that match a certain condition.
It reverses the logic of whatever condition it’s applied to.

Syntax

SELECT column1, column2, ... FROM table_name
WHERE NOT condition;

Example 1: Employees not in IT department

SELECT name, department FROM employees
WHERE NOT department = 'IT';

This returns all employees who are not in the IT department.

Example 2: Employees not female

SELECT name, gender FROM employees
WHERE NOT gender = 'female';

This shows all employees who are not marked as female.


Tip

You can also combine NOT with IN, LIKE, BETWEEN, etc. (more on those in future chapters).