Sub topic 7
Combining Conditions with AND, OR, and NOT
Assume you understand tables, rows, columns, and basic SELECT and WHERE clauses. This chapter uses an employees table with columns such as first_name, department, salary, and status.
Requiring several conditions with AND
AND keeps a row only when every condition is true. For example, this query finds active Sales employees earning at least 50,000:
SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Sales'
AND salary >= 50000
AND status = 'Active';A row must satisfy all three tests. An employee in Sales with a salary of 45,000 is excluded. An employee earning 60,000 in Marketing is also excluded.
Use AND when each requirement must apply at the same time.
Accepting alternatives with OR
OR keeps a row when at least one condition is true. This query finds employees in either Sales or Marketing:
SELECT first_name, department
FROM employees
WHERE department = 'Sales'
OR department = 'Marketing';A row in Sales satisfies the first condition. A row in Marketing satisfies the second. A row in Support satisfies neither and is excluded.
When you combine AND and OR, use parentheses to show the intended groups:
SELECT first_name, department, salary
FROM employees
WHERE (department = 'Sales' OR department = 'Marketing')
AND salary > 50000;The parentheses mean “Sales or Marketing first, then salary above 50,000.”
SQL evaluates AND before OR when parentheses are absent. Therefore, this condition:
WHERE department = 'Sales'
OR department = 'Marketing'
AND salary > 50000is interpreted as:
WHERE department = 'Sales'
OR (department = 'Marketing' AND salary > 50000)That version includes every Sales employee, regardless of salary. Parentheses prevent this common error.
Reversing a condition with NOT
NOT reverses a condition. This query excludes employees in the Support department:
SELECT first_name, department
FROM employees
WHERE NOT department = 'Support';For simple comparisons, <> often reads more naturally:
SELECT first_name, department
FROM employees
WHERE department <> 'Support';NOT is especially useful with grouped conditions:
SELECT first_name, department, status
FROM employees
WHERE NOT (department = 'Sales' OR status = 'Inactive');This excludes both Sales employees and all inactive employees. Equivalently, it requires an employee to be outside Sales and active:
SELECT first_name, department, status
FROM employees
WHERE department <> 'Sales'
AND status <> 'Inactive';This relationship follows De Morgan's laws: NOT (A OR B) means NOT A AND NOT B, while NOT (A AND B) means NOT A OR NOT B.
Predicting the result
Break a complex condition into named tests before writing it.
| Test | Meaning |
|---|---|
| `department = 'Sales'` | The employee works in Sales |
| `salary >= 50000` | The salary is at least 50,000 |
| `status = 'Active'` | The employee is active |
The following query requires an active employee who is either in Sales or earns at least 50,000:
SELECT first_name, department, salary
FROM employees
WHERE (department = 'Sales' OR salary >= 50000)
AND status = 'Active';The database evaluates the parenthesized alternative first, then applies the active-status requirement.
Exercise
Write a query that returns the names of active employees who work in Sales or Marketing and earn more than 60000.
Show answer
SELECT firstname, lastname FROM employees WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 60000 AND status = 'Active';