Lesson 19 of 60 – NOT Operator
32%

NOT Operator in SQL

The NOT operator is used to reverse a condition in SQL. It returns records that do not satisfy the specified condition.

Note: NOT is commonly used with WHERE conditions and can be combined with operators such as IN, BETWEEN, LIKE, and EXISTS.

1. What is NOT?

The NOT operator reverses the result of a condition.

SELECT *
FROM Students
WHERE NOT course = 'Python';

This query returns students whose course is not Python.

2. Basic NOT Syntax

SELECT column_name
FROM table_name
WHERE NOT condition;

The NOT operator is placed before the condition that you want to reverse.

3. NOT with Equal (=)

SELECT *
FROM Students
WHERE NOT age = 20;

This returns students whose age is not 20.

4. NOT with Text

SELECT *
FROM Students
WHERE NOT course = 'Java';

This returns students whose course is not Java.

5. NOT with Greater Than

SELECT *
FROM Students
WHERE NOT age > 20;

This returns records where the condition age > 20 is false, subject to SQL's treatment of NULL values.

For ordinary non-NULL age values, this corresponds to ages less than or equal to 20.

6. NOT with Less Than

SELECT *
FROM Students
WHERE NOT age < 18;

For non-NULL age values, this returns records where age is greater than or equal to 18.

7. NOT with IN

NOT can be used with the IN operator.

SELECT *
FROM Students
WHERE course NOT IN ('Python', 'Java');

This returns students whose course is neither Python nor Java.

8. NOT IN Syntax

SELECT column_name
FROM table_name
WHERE column_name NOT IN (value1, value2, value3);

NOT IN is a convenient way to exclude several specific values.

9. NOT with BETWEEN

You can use NOT with BETWEEN to exclude a range.

SELECT *
FROM Students
WHERE age NOT BETWEEN 18 AND 25;

This returns values outside the range 18 through 25, for non-NULL age values.

10. NOT BETWEEN Example

Suppose the ages are:

16
18
20
22
25
28
30

Query:

SELECT *
FROM Students
WHERE age NOT BETWEEN 18 AND 25;

The matching ages are 16, 28, and 30.

11. NOT with LIKE

NOT can be used with LIKE to exclude a pattern.

SELECT *
FROM Students
WHERE name NOT LIKE 'A%';

This returns names that do not start with A.

12. NOT LIKE Example

SELECT *
FROM Students
WHERE email NOT LIKE '%@gmail.com';

This excludes email addresses ending with @gmail.com, subject to the database's pattern-matching and collation rules.

13. NOT with IS NULL

You can use NOT with an IS NULL condition.

SELECT *
FROM Students
WHERE NOT (mobile IS NULL);

This finds records where the mobile value is not NULL.

A more common and simpler form is:

SELECT *
FROM Students
WHERE mobile IS NOT NULL;

14. IS NOT NULL

IS NOT NULL is specifically used to find values that are not NULL.

SELECT *
FROM Students
WHERE mobile IS NOT NULL;

15. NOT with AND

NOT can be combined with AND.

SELECT *
FROM Students
WHERE NOT course = 'Python'
AND age > 18;

This finds students who are not in Python and are older than 18.

16. NOT with OR

SELECT *
FROM Students
WHERE NOT (course = 'Python' OR course = 'Java');

This excludes students whose course is Python or Java.

17. NOT with Parentheses

Parentheses are useful when NOT applies to a group of conditions.

SELECT *
FROM Students
WHERE NOT (
    course = 'Python'
    AND age < 20
);

The grouped condition is evaluated first, and NOT reverses its result.

18. NOT with Multiple Conditions

SELECT *
FROM Students
WHERE NOT (
    city = 'Patna'
    AND course = 'Python'
);

This excludes records where both city is Patna and course is Python.

19. NOT vs <>

For a simple equality condition, these expressions are often equivalent for non-NULL values:

WHERE NOT course = 'Python'
WHERE course <> 'Python'

The second form is often shorter when checking that one value is not equal to another.

20. NOT vs !=

In MySQL, != can also be used for not equal.

SELECT *
FROM Students
WHERE course != 'Python';

For ordinary non-NULL values, this is equivalent to checking that the course is not Python.

21. NOT IN vs Multiple <>

Instead of writing multiple not-equal conditions:

WHERE course <> 'Python'
AND course <> 'Java'

You can use:

WHERE course NOT IN ('Python', 'Java');

NOT IN is usually easier to read when excluding multiple values.

22. NOT with LIKE and Wildcards

SELECT *
FROM Students
WHERE name NOT LIKE 'R%';

The % wildcard represents zero or more characters. This query excludes names beginning with R.

23. NOT with IN and AND

SELECT *
FROM Students
WHERE course NOT IN ('Python', 'Java')
AND age >= 18;

This returns students who are at least 18 and are not enrolled in Python or Java.

24. NOT with BETWEEN and AND

SELECT *
FROM Students
WHERE age NOT BETWEEN 18 AND 25
AND fee > 5000;

This returns students whose age is outside the range 18–25 and whose fee is greater than 5000.

25. NOT with ORDER BY

SELECT name, course, age
FROM Students
WHERE course NOT IN ('Python', 'Java')
ORDER BY age DESC;

The records are first filtered and then sorted by age in descending order.

26. NOT with LIMIT

SELECT *
FROM Students
WHERE course NOT IN ('Python', 'Java')
LIMIT 5;

In MySQL, this returns up to five matching records.

27. NOT in UPDATE

NOT can also be used when updating selected records.

UPDATE Students
SET status = 'Inactive'
WHERE NOT course = 'Python';

This updates records whose course is not Python.

Warning: Always check the WHERE condition carefully before executing UPDATE.

28. NOT in DELETE

NOT can also be used with DELETE.

DELETE FROM Students
WHERE course NOT IN ('Python', 'Java');

This targets students whose course is neither Python nor Java.

Warning: DELETE permanently removes matching records. Always verify the WHERE condition before executing it.

29. Practical Example

Suppose an institute wants students who are not enrolled in Python or Java and who are older than 18.

SELECT name, age, course
FROM Students
WHERE course NOT IN ('Python', 'Java')
AND age > 18;

This combines NOT IN with AND.

30. Complete NOT Example

SELECT name, age, course, city, fee
FROM Students
WHERE NOT (
    course IN ('Python', 'Java')
)
AND age >= 18
AND city NOT IN ('Patna', 'Delhi')
AND fee > 5000
ORDER BY fee DESC;

This query:

  • Excludes Python and Java students.
  • Includes students aged 18 or above.
  • Excludes students from Patna and Delhi.
  • Requires a fee greater than 5000.
  • Sorts the result by fee from highest to lowest.

📌 Key Points

  • NOT reverses the result of a condition.
  • NOT IN excludes specified values.
  • NOT BETWEEN excludes a specified range.
  • NOT LIKE excludes matching patterns.
  • IS NOT NULL finds values that are not NULL.
  • NOT can be combined with AND and OR.
  • Parentheses help group complex NOT conditions.
  • NOT can be used with SELECT, UPDATE, and DELETE.
  • For simple not-equal comparisons, <> or != can also be used.
  • Always check UPDATE and DELETE conditions carefully before execution.

🧠 Quick Quiz

Question: Which SQL operator is used to reverse a condition?