Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to SQL Notes
Topic #205

SQL NOT Operator

NOT reverses a condition — it returns rows where the condition is false.

Syntax

WHERE NOT condition

Sample Table: employees

namedepartment
AmanEngineering
RiyaMarketing
NehaDesign

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 where department is NULL — NULL never 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.

Related SQL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →