Sub topic 7
Filtering Rows with `WHERE`
This chapter assumes you know that a relational table contains rows and columns, and that you can run SQL in an Oracle Database session. You should also recognize numbers, strings, and dates as basic data types.
A WHERE clause filters rows. Oracle evaluates a condition for each row and returns only the rows for which the condition is true.
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = 50;This query reads rows from employees, tests department_id = 50, and returns only employees in department 50.
How a `WHERE` clause works
A condition in a WHERE clause is called a predicate. A predicate compares values or tests a rule. Common comparison operators are:
| Operator | Meaning | Example |
|---|---|---|
| `=` | equal to | `department_id = 50` |
| `<>` | not equal to | `job_id <> 'IT_PROG'` |
| `>` | greater than | `salary > 5000` |
| `<=` | less than or equal to | `salary <= 10000` |
String values use single quotation marks. The comparison is usually case-sensitive, so 'IT_PROG' and 'it_prog' are different values.
You can combine predicates with AND, OR, and NOT.
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE department_id = 50
AND salary >= 3000;Both conditions must be true. OR requires at least one condition to be true.
SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id = 'IT_PROG'
OR job_id = 'ST_CLERK';Use parentheses when a condition contains both AND and OR. Without parentheses, Oracle evaluates NOT first, then AND, and then OR.
SELECT employee_id, last_name, department_id
FROM employees
WHERE department_id = 50
OR (department_id = 60 AND last_name LIKE 'S%');The parentheses make the intended grouping clear.
Useful filtering operators
IN compares a value with a list of values:
SELECT employee_id, last_name, department_id
FROM employees
WHERE department_id IN (50, 60, 80);BETWEEN includes both boundary values:
SELECT employee_id, last_name, salary
FROM employees
WHERE salary BETWEEN 4000 AND 8000;For text patterns, use LIKE. The % wildcard matches any number of characters, and _ matches exactly one character.
SELECT employee_id, last_name
FROM employees
WHERE last_name LIKE 'S%';This returns last names beginning with S.
`NULL` in a filter
NULL means that a value is missing or unknown. It is not equal to zero, an empty string, or another NULL. Therefore, this condition does not correctly find missing commissions:
WHERE commission_pct = NULLUse IS NULL or IS NOT NULL instead.
SELECT employee_id, last_name, commission_pct
FROM employees
WHERE commission_pct IS NULL;A predicate involving NULL can produce an unknown result. WHERE returns only rows where the predicate is true, so rows with an unknown result are excluded.
You can replace a NULL value for display or comparison with NVL, which returns its second argument when its first argument is NULL.
SELECT employee_id, last_name, NVL(commission_pct, 0) AS commission
FROM employees
WHERE commission_pct IS NULL
OR commission_pct > 0.2;Filtering and query order
Oracle logically obtains rows from FROM, applies WHERE, and then produces the selected columns. ORDER BY sorts the rows that remain. This means a filter reduces the input before the final result is sorted.
Exercise
Write a query that returns employee_id, last_name, and salary for employees in departments 50 or 80 whose salary is at least 3000.
Show answer
SELECT employee_id, last_name, salary FROM employees WHERE department_id IN (50, 80) AND salary >= 3000;