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, AND is evaluated before OR, which can give unexpected results. Always use parentheses when mixing AND and OR.

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

  1. Find active employees in Engineering earning over $95,000
  2. Find employees in Marketing or Sales
  3. Find employees with salaries between $70,000 and $100,000
  4. Find all active employees NOT in Engineering
Take Quiz Next Lesson
SQL Editor Ctrl+Enter to run
Results
Run a query to see results here
Tables: