The SELECT statement by itself returns all rows from a table. However, in real-world scenarios, you usually only need a subset of data that meets specific criteria. This is where the WHERE clause comes in.
The WHERE clause is used to filter records. It ensures that only those rows that satisfy a specified condition are included in the result set.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The condition is an expression that evaluates to true, false, or unknown (if NULL values are involves). Only rows where the condition evaluates to true are returned.
SQL provides a variety of operators to compare values in the WHERE clause:
| Operator | Description | Example |
|---|---|---|
= | Equal to | WHERE last_name = 'SMITH' |
<> or != | Not equal to | WHERE store_id <> 1 |
> | Greater than | WHERE rental_rate > 2.99 |
< | Less than | WHERE length < 60 |
>= | Greater than or equal to | WHERE replacement_cost >= 20.00 |
<= | Less than or equal to | WHERE amount <= 5.00 |
To find films with a rental rate of $4.99 from the film table:
SELECT title, rental_rate, replacement_cost
FROM film
WHERE rental_rate = 4.99;
SQL includes powerful operators for range, set, and pattern matching.
Filters values within a certain range (inclusive).
-- Find payments between $5.00 and $10.00
SELECT payment_id, amount, payment_date
FROM payment
WHERE amount BETWEEN 5.00 AND 10.00;
Matches any value in a specified list.
-- Find customers from specific stores
SELECT first_name, last_name, store_id
FROM customer
WHERE store_id IN (1, 2);
Searches for a specified pattern in a column using wildcards:
% represents zero, one, or multiple characters._ represents a single character.-- Find films starting with 'A'
SELECT title
FROM film
WHERE title LIKE 'A%';
-- Find films where the second letter is 'I'
SELECT title
FROM film
WHERE title LIKE '_I%';
As we learned in the lesson on NULLs, you cannot use = or <> to check for NULL. You must use IS NULL or IS NOT NULL.
-- Incorrect
-- WHERE return_date = NULL
-- Correct
SELECT rental_id, rental_date, return_date
FROM rental
WHERE return_date IS NULL;
Key Takeaways from this Lesson:
WHERE clause filters rows before they are returned to the result set.'SMITH').LIKE for pattern matching and IN for matching against lists.= with NULL; always use IS NULL.In the next lesson, we will explore how to Combine Multiple Conditions to create even more powerful filters.