NOT (Reverse a Condition)
Flip a condition with NOT, combine it with IN / BETWEEN / LIKE, and see how it behaves with NULL.
Sometimes it is easier to describe what you don't want. "Everyone not from India." "Teams not in this list." NOT takes any condition and reverses it.
Definition
NOT is a unary logical operator that reverses the result of the condition after it: TRUE becomes FALSE and FALSE becomes TRUE. UNKNOWN stays UNKNOWN.
"Unary" means it works on one condition (unlike AND / OR, which join two). The last sentence is the important one: NOT cannot turn a NULL comparison into TRUE, so NULL rows are still excluded.
Syntax
sql
SELECT column_list
FROM table_name
WHERE NOT condition;NOT also combines with other operators:
| Form | Meaning |
|---|---|
NOT country = 'India' | same as country <> 'India' |
NOT (A AND B) | reverse a whole group of conditions |
NOT IN (...) | value is not in the list |
NOT BETWEEN a AND b | value is outside the range |
NOT LIKE 'pattern' | text does not match the pattern |
IS NOT NULL | value is present |
Example — everyone NOT from India
sql
SELECT *
FROM players
WHERE NOT country = 'India';| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
| 5 | David Warner | DC | Australia | 700 |
NOT flips the result: SQL first checks country = 'India', then keeps the rows where that was not true.
Example — NOT on a group in brackets
"Remove the Indian players who scored under 400" (i.e. remove MS Dhoni and Jasprit Bumrah):
sql
SELECT name, team, country, runs
FROM players
WHERE NOT (country = 'India' AND runs < 400);| name | team | country | runs |
|---|---|---|---|
| Virat Kohli | RCB | India | 750 |
| Rohit Sharma | MI | India | 500 |
| Jos Buttler | RR | England | 850 |
| David Warner | DC | Australia | 700 |
The brackets are evaluated first, then NOT reverses the combined answer. Without brackets, NOT would apply only to country = 'India' (NOT binds tighter than AND).
Note — De Morgan's law:
NOT (A AND B)=NOT A OR NOT B, andNOT (A OR B)=NOT A AND NOT B. So the query above equalscountry <> 'India' OR runs >= 400.
Example — NOT and NULL
In player_profiles, three players have a NULL nickname.
sql
SELECT name, nickname
FROM player_profiles
WHERE NOT nickname = 'King';| name | nickname |
|---|---|
| Rohit Sharma | Hitman |
| MS Dhoni | Thala |
Only 2 rows, not 5. For NULL nicknames nickname = 'King' is UNKNOWN, and NOT UNKNOWN is still UNKNOWN → dropped. Add OR nickname IS NULL to include them.
Common mistakes
- ❌
WHERE country NOT = 'India'→ invalid. ✅WHERE NOT country = 'India'orcountry <> 'India'. - ❌
WHERE NOT country = 'India' AND runs < 400expecting NOT to cover both → NOT applies only to the first condition. ✅ add brackets:NOT (country = 'India' AND runs < 400). - ❌ Expecting NOT to bring back NULL rows. ✅ handle NULL explicitly with
IS NULL.
Interview tip
Q: Rewrite WHERE NOT (team = 'MI' OR team = 'CSK') without NOT.
A: By De Morgan's law: WHERE team <> 'MI' AND team <> 'CSK' — which is also what team NOT IN ('MI', 'CSK') means.
Key points to remember
- NOT reverses a condition: TRUE → FALSE, FALSE → TRUE.
- NOT UNKNOWN is still UNKNOWN, so NOT never brings back NULL rows.
- NOT combines with other operators: NOT IN, NOT BETWEEN, NOT LIKE, IS NOT NULL.
- NOT applies only to the condition right after it — use brackets to reverse a group.
- De Morgan: NOT (A AND B) = NOT A OR NOT B; NOT (A OR B) = NOT A AND NOT B.
- NOT country = 'India' gives the same rows as country <> 'India'.
Try it yourself
Everyone NOT from India
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NOT on a bracketed group
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NOT does not rescue NULLs
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: NOT with IN
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
