4. WHERE: keeping only the rows you want
WHERE filters rows. The database checks the condition against every row
and keeps only the ones where it's true.
Compare values
The comparison operators are what you'd expect: =, <> (not equal),
<, >, <=, >=. Note SQL uses a single =, never ==.
SELECT title, rental_rate
FROM film
WHERE rental_rate > 4.00;
Combine conditions with AND / OR
AND means both must be true; OR means at least one. When you mix
them, always use parentheses — it removes any doubt about what you
meant:
SELECT title, rating, rental_rate
FROM film
WHERE rating = 'R' AND (rental_rate < 1.00 OR rental_rate > 4.00);
NOT flips a condition: WHERE NOT (rating = 'R').
Handy shortcuts
BETWEEN checks a range, and it includes both ends:
SELECT title, length
FROM film
WHERE length BETWEEN 60 AND 90; -- 60 and 90 both count
IN checks against a list — cleaner than a pile of ORs:
SELECT title, rating
FROM film
WHERE rating IN ('G', 'PG');
Match text patterns with LIKE
LIKE matches text against a pattern. % stands for any run of
characters, _ for exactly one:
SELECT title
FROM film
WHERE title LIKE 'ACADEMY%'; -- starts with ACADEMY
LIKE is case-sensitive. Postgres adds ILIKE for case-insensitive
matching — WHERE title ILIKE 'academy%' finds the same film.
Try it
Find every film that is rated 'PG' and is longer than 120 minutes.
Return the title and length, sorted by title.
SELECT title, length
FROM film
-- add your conditions and sorting
SELECT title, length
FROM film
WHERE rating = 'PG' AND length > 120
ORDER BY title;