AND, OR, and NOT
Objectives
By the end of this lesson, you should be able to:
- Combine multiple conditions with
ANDandOR - Negate a condition with
NOT - Explain how PostgreSQL evaluates
ANDandORtogether
💡 Why this matters: Real filtering conditions are rarely just one comparison. “Engineering employees earning over 80000” needs both conditions true at once, “Sales or Marketing” needs either one.
⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the
employeestable from the last lesson.
AND: Every Condition Must Be True
SELECT first_name, department, salary FROM employees
WHERE department = 'Engineering' AND salary > 80000;
first_name | department | salary
------------+-------------+---------
Priya | Engineering | 89000.00
Taylor | Engineering | 95000.00
Both conditions have to be true for a row to appear, Erin is in Engineering but earns under 80000, so she’s excluded. AND can chain more than two conditions, every single one has to hold.
OR: At Least One Condition Must Be True
SELECT first_name, department FROM employees
WHERE department = 'Sales' OR department = 'Marketing';
first_name | department
------------+-------------
Jordan | Sales
Maya | Marketing
Sam | Sales
Alexis | Marketing
Only one of the two conditions needs to hold for a row to appear. This particular case, checking one column against several possible values, is common enough that it has its own shorthand, IN, covered in the next lesson.
NOT: Negating a Condition
SELECT first_name, department FROM employees WHERE NOT department = 'Engineering';
first_name | department
------------+-------------
Jordan | Sales
Maya | Marketing
Sam | Sales
Alexis | Marketing
NOT department = 'Engineering' and department != 'Engineering' (last lesson) return the same result here, NOT flips whatever condition follows it. NOT is more useful once conditions get more complex, NOT (department = 'Engineering' AND salary > 80000) is a lot clearer than trying to write that negation with comparison operators alone.
Combining AND and OR
AND binds tighter than OR, the same way multiplication binds tighter than addition in arithmetic. Use parentheses whenever a query mixes both, to make the actual grouping explicit rather than relying on the default:
SELECT first_name, department, salary FROM employees
WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 65000;
Without the parentheses, this would be read as department = 'Sales' OR (department = 'Marketing' AND salary > 65000), a genuinely different condition. When in doubt, add the parentheses, they cost nothing and remove any ambiguity, for you and for anyone reading the query later.
Try It
- Write a query for every employee in
Marketingearning more than 68000. - Write a query for every employee in either
EngineeringorSales. - Write a query using
NOTfor every employee not inMarketing. - Write a query for every employee in
EngineeringorSales, earning more than 70000, using parentheses to make the grouping explicit.
Recap
ANDrequires every condition to be true,ORrequires at least one.NOTnegates whatever condition follows it.ANDbinds tighter thanOR, use parentheses whenever a query mixes both, to make the grouping explicit.
Next lesson: BETWEEN and IN, shorthand for two very common filtering patterns.