πŸ“ What You’ll Learn

  • What the NOT operator does
  • Syntax and structure
  • How to negate conditions
  • Real-world examples
  • Common usage notes

❓ What Is the NOT Operator?

The NOT operator is used in SQL to negate a conditionβ€”it returns rows where the condition is false.

Think of it as flipping the logic:

NOT TRUE β†’ FALSE
NOT FALSE β†’ TRUE

🧱 Basic Syntax

SELECT column1, column2
FROM table_name
WHERE NOT condition;

You can also use NOT with logical operators like IN, BETWEEN, LIKE, etc.

πŸ”„ Example: Using NOT

Get all users who are not from the USA

SELECT name
FROM users
WHERE NOT country = 'USA';

βœ… This returns users from countries other than the USA.

πŸ§ͺ More Examples

Products not in stock

SELECT product_name
FROM products
WHERE NOT stock > 0;

Employees not in Sales or HR

SELECT name, department
FROM employees
WHERE department NOT IN ('Sales', 'HR');

Customers not from cities starting with 'S'

SELECT name
FROM customers
WHERE city NOT LIKE 'S%';

⚠️ Usage Notes

  • NOT flips the result of the condition that follows it
  • Use parentheses to group logic when combining with AND or OR
  • Be mindful with NULLsβ€”NOT NULL means the value must exist

πŸ“˜ Recap

  • The NOT operator inverts conditions
  • Useful for filtering everything except a certain condition
  • Combine with other operators like IN, LIKE, BETWEEN
  • Always check your logic to avoid unexpected results