🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – NOT

Invert a condition in your WHERE clause using NOT to exclude specific rows from results.

Sometimes it is easier to describe what you do NOT want than what you do. The NOT operator flips a condition from true to false and vice versa. NOT IN excludes a list of values, NOT LIKE skips pattern matches, and NOT NULL finds rows with actual values (instead of empty ones).

NOT is particularly valuable when the exclusion list is small but the inclusion set would be enormous. Rather than listing 47 valid categories, just exclude the 3 you do not want.

NOT with different operators

WHERE NOT city = 'Delhi'        -- Exclude Delhi
WHERE city NOT IN ('Delhi','Pune') -- Exclude multiple
WHERE grade NOT LIKE 'D%'       -- Exclude D grades
WHERE score IS NOT NULL         -- Non-empty scores

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • NOT inverts any Boolean condition
  • IS NOT NULL specifically checks for non-empty values
  • NOT IN is equivalent to using != with AND for each value
  • NOT can make queries slower — ensure columns are indexed

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.