Sub topic 5
Keeping Only Matching Rows with `WHERE`
Assume you already know that a relational table contains rows and columns, and that you can run SQL in PostgreSQL, MySQL, or SQLite. This chapter uses an employees table with columns such as first_name, last_name, department, salary, hire_date, and manager_id.
A WHERE clause filters rows. The database tests its condition for every row and returns only rows for which the condition is true.
SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'Sales';Text values use single quotes. The query returns employees in the Sales department. It does not change the stored table.
Comparing values
SQL provides comparison operators for numbers, text, and dates.
| Operator | Meaning |
|---|---|
| `=` | equal to |
| `<>` | not equal to |
| `<` and `<=` | less than, or less than or equal to |
| `>` and `>=` | greater than, or greater than or equal to |
For example, this query keeps employees whose salary is at least 50,000:
SELECT first_name, salary
FROM employees
WHERE salary >= 50000;You can combine comparisons with AND. Every condition joined by AND must be true.
SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'Sales'
AND salary >= 50000;Combining alternatives
Use OR when either condition can be true. Use parentheses when you combine AND and OR. Without parentheses, AND is evaluated more tightly than OR, which can produce an unintended result.
SELECT first_name, department, salary
FROM employees
WHERE (department = 'Sales' OR department = 'Marketing')
AND salary > 50000;This means “Sales or Marketing, and salary above 50,000.” The parentheses make that meaning clear.
NOT reverses a condition:
SELECT first_name, department
FROM employees
WHERE NOT department = 'Sales';You can often write the same condition as department <> 'Sales'.
Ranges, lists, and patterns
BETWEEN checks an inclusive range, so both boundary values are included. IN checks whether a value matches one item in a list. LIKE matches text patterns. The % wildcard represents any sequence of characters.
SELECT first_name, last_name
FROM employees
WHERE department IN ('Sales', 'Marketing', 'Support')
AND hire_date BETWEEN '2020-01-01' AND '2022-12-31'
AND last_name LIKE 'Mc%';This query finds employees in one of three departments, hired within the date range, whose last name starts with Mc.
Handling missing values
NULL means that a value is missing or unknown. It is not equal to zero, an empty string, or another NULL. Use IS NULL or IS NOT NULL to test it.
SELECT first_name, last_name
FROM employees
WHERE manager_id IS NULL;Writing manager_id = NULL does not find missing managers because comparisons with NULL produce an unknown result, not true.
Exercise
Write a query that returns the names and salaries of employees in either Sales or Support whose salary is below 60000.
Show answer
SELECT firstname, lastname, salary FROM employees WHERE department IN ('Sales', 'Support') AND salary < 60000;