Tools

SQL AND / OR / NOT

Share:
Article Summary

Use AND, OR, and NOT to build complex filters in your WHERE clause. πŸ”Ή How They Work πŸ”Ή Examples 🧠 Quick Recap Operator Meaning Example AND Both conditions true WHERE dept = 'IT' AND salary > 60000 OR Either condition true WHERE dept = 'HR' OR salary > 70000 NOT Negates condition WHERE NOT dept […]

Use AND, OR, and NOT to build complex filters in your WHERE clause.

πŸ”Ή How They Work

  • AND β€” all conditions must be true
  • OR β€” at least one condition is true
  • NOT β€” reverses the condition (negation)

πŸ”Ή Examples

-- Both conditions must be true
SELECT * FROM employees 
WHERE department = 'IT' AND salary > 60000;

-- Either condition can be true
SELECT * FROM employees 
WHERE department = 'HR' OR salary > 70000;

-- Negate a condition
SELECT * FROM employees 
WHERE NOT department = 'Sales';

-- Combine all
SELECT * FROM employees 
WHERE (department = 'IT' OR department = 'HR') AND salary > 50000;

🧠 Quick Recap

OperatorMeaningExample
ANDBoth conditions trueWHERE dept = 'IT' AND salary > 60000
OREither condition trueWHERE dept = 'HR' OR salary > 70000
NOTNegates conditionWHERE NOT dept = 'Sales'

βœ… Use these to filter data precisely and flexibly!

Was this helpful?

Written by

W3buddy
W3buddy

Explore W3Buddy for in-depth guides, breaking tech news, and expert analysis on AI, cybersecurity, databases, web development, and emerging technologies.