We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
AND, OR, and NOT
Combine multiple conditions with logical operators to create more precise filters.
AND - Both Conditions Must Be True
SELECT name, department, salary
FROM employees
WHERE department = 'Engineering' AND salary > 90000;
This returns employees who are both in Engineering and earn over $90,000.
OR - Either Condition Can Be True
SELECT name, department, salary
FROM employees
WHERE department = 'Engineering' OR department = 'Marketing';
This returns employees in either Engineering or Marketing.
NOT - Reverses a Condition
SELECT name, department
FROM employees
WHERE NOT department = 'Sales';
This returns employees who are not in Sales (same as department <> 'Sales').
Combining Operators
You can combine AND, OR, and NOT. Use parentheses to control evaluation order:
-- Engineers earning over $95k OR anyone in Sales
SELECT name, department, salary
FROM employees
WHERE (department = 'Engineering' AND salary > 95000)
OR department = 'Sales';
Warning: Without parentheses,
ANDis evaluated beforeOR, which can give unexpected results. Always use parentheses when mixingANDandOR.
BETWEEN - Range Checks
Instead of salary >= 70000 AND salary <= 90000, use BETWEEN:
SELECT name, salary
FROM employees
WHERE salary BETWEEN 70000 AND 90000;
BETWEEN is inclusive - it includes both endpoints.
IN - Match Against a List
Instead of multiple OR conditions, use IN:
-- Instead of: department = 'Engineering' OR department = 'Sales'
SELECT name, department
FROM employees
WHERE department IN ('Engineering', 'Sales');
NOT IN and NOT BETWEEN
-- Employees NOT in Engineering or Sales
SELECT name, department
FROM employees
WHERE department NOT IN ('Engineering', 'Sales');
-- Salaries outside the 70k-90k range
SELECT name, salary
FROM employees
WHERE salary NOT BETWEEN 70000 AND 90000;
Try It Yourself
- Find active employees in Engineering earning over $95,000
- Find employees in Marketing or Sales
- Find employees with salaries between $70,000 and $100,000
- Find all active employees NOT in Engineering