đĢ The NOT operator in SQL is a logical operator used to reverse the result of a condition. If a condition evaluates to TRUE, NOT changes it to FALSE. Likewise, if a condition evaluates to FALSE, NOT changes it to TRUE. It is commonly used with the WHERE, HAVING, and other SQL clauses to exclude specific records.
đ What is the NOT Operator?
The NOT operator negates a condition. Instead of selecting rows that satisfy a condition, it selects rows that do not satisfy that condition.
Information
đ Basic Syntax
General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | City |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | Chennai |
| 102 | Bob | Mathematics | 85 | Coimbatore |
| 103 | Charlie | Physics | 78 | Madurai |
| 104 | David | Computer Science | 95 | Chennai |
| 105 | Eva | Physics | 88 | Coimbatore |
đ¯ Using NOT with a Simple Condition
Retrieve students who are not in the Computer Science department.
NOT Example
SELECT *
FROM Students
WHERE NOT Department = 'Computer Science';This query returns all students except those belonging to the Computer Science department.
đ NOT with Comparison Operators
The NOT operator can be combined with comparison operators.
| Expression | Meaning |
|---|---|
| NOT Marks > 90 | Marks are 90 or below. |
| NOT Department = 'Physics' | Department is not Physics. |
| NOT City = 'Chennai' | City is not Chennai. |
NOT with Comparison
SELECT *
FROM Students
WHERE NOT Marks > 90;đ NOT with BETWEEN
Use NOT BETWEEN to retrieve values outside a specified range.
NOT BETWEEN Example
SELECT *
FROM Students
WHERE Marks NOT BETWEEN 80 AND 90;This query returns students whose marks are less than 80 or greater than 90.
đ NOT with IN
The NOT IN operator excludes rows whose values appear in a given list.
NOT IN Example
SELECT *
FROM Students
WHERE Department NOT IN (
'Computer Science',
'Physics'
);đ¤ NOT with LIKE
The NOT LIKE operator excludes rows that match a specified pattern.
NOT LIKE Example
SELECT *
FROM Students
WHERE Name NOT LIKE 'A%';This query returns all students whose names do not begin with the letter A.
đĢ NOT with IS NULL
To retrieve rows that contain actual values instead of NULL, use IS NOT NULL.
IS NOT NULL Example
SELECT *
FROM Students
WHERE Email IS NOT NULL;Important
đ Combining NOT with AND and OR
The NOT operator can be combined with other logical operators to create more complex filtering conditions.
NOT with AND
SELECT *
FROM Students
WHERE NOT (
Department = 'Computer Science'
AND Marks >= 90
);NOT with OR
SELECT *
FROM Students
WHERE NOT (
City = 'Chennai'
OR City = 'Coimbatore'
);Tip
đ Common NOT Operators
| Operator | Purpose |
|---|---|
| NOT | Reverses a condition. |
| NOT BETWEEN | Excludes a range of values. |
| NOT IN | Excludes values in a list. |
| NOT LIKE | Excludes matching patterns. |
| IS NOT NULL | Returns rows containing non-NULL values. |
đŧ Real-World Example
A university wants to list students who are not from the Computer Science department and whose marks are not below 80.
Real-World Query
SELECT StudentID,
Name,
Department,
Marks
FROM Students
WHERE NOT Department = 'Computer Science'
AND NOT Marks < 80
ORDER BY Marks DESC;This query excludes Computer Science students and also excludes students whose marks are below 80.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ NOT reverses the result of a condition.
- đ It is commonly used with WHERE and HAVING.
- đ SQL provides specialized forms such as NOT IN, NOT BETWEEN, and NOT LIKE.
- đ Use IS NOT NULL to find non-NULL values.
- đ Parentheses improve readability when combining NOT with AND or OR.