π 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
orOR
- Be mindful with
NULL
sβ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