NOT reverses a condition — it returns rows where the condition is false.
Syntax
WHERE NOT condition
Sample Table: employees
| name | department |
|---|---|
| Aman | Engineering |
| Riya | Marketing |
| Neha | Design |
Example
SELECT name FROM employees
WHERE NOT department = 'Engineering';
Equivalent, and usually more readable:
SELECT name FROM employees
WHERE department <> 'Engineering';
Output: Riya, Neha.
NOT with IN, BETWEEN, LIKE, IS NULL
WHERE department NOT IN ('Engineering', 'Design')
WHERE salary NOT BETWEEN 50000 AND 70000
WHERE name NOT LIKE 'A%'
WHERE manager_id IS NOT NULL
These forms are usually clearer than wrapping the whole expression in NOT (...).
Practical Use Case
Excluding a specific set of rows is often easier to express with NOT than by listing every value you do want — e.g. "every department except Engineering" is one condition instead of enumerating the rest.
Common Mistakes
- NOT and NULL don't mix the way people expect.
WHERE NOT department = 'Engineering'will silently exclude rows wheredepartmentisNULL—NULLnever satisfies=or its negation. If you need NULLs included, handle them explicitly. - Writing deeply nested
NOT (A AND NOT B)logic instead of simplifying it first — hard to read and easy to get wrong
Interview Relevance
Q: "Does WHERE NOT department = 'Engineering' return rows where department is NULL?" No — and explaining why (three-valued logic: NULL comparisons return UNKNOWN, not TRUE) is a strong signal in an interview.
Practice Question
Write a query for all employees NOT in the "Design" or "Marketing" departments, using NOT IN.