Learn SQL: Comparisons
Greater, smaller, not equal
"I rent out long films for two days," says Vic. "But which ones are those, actually?" Equal to isn't enough here: you need greater than.
Six ways to compare
Besides =, SQL has > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to) and <> (not equal to). Instead of <>, most databases also accept !=.
This query shows the films that run longer than 150 minutes.
SELECT title, minutes FROM films WHERE minutes > 150| title | minutes |
|---|---|
| Titanic | 194 |
| Pulp Fiction | 154 |
| Dune | 155 |
| Oppenheimer | 180 |
Text and dates too
Comparing works with text and dates too. Dates are stored here as text in the form '2024-10-15': year, month, day. That way the alphabetical order matches the order in time.
Always put a date between quotes. This query shows the rentals from 20 October onwards.
SELECT * FROM rentals WHERE rented_on >= '2024-10-20'| id | film_id | member_id | rented_on | returned_on | fine |
|---|---|---|---|---|---|
| 25 | 8 | 15 | 2024-10-20 | NULL | 0 |
| 26 | 1 | 11 | 2024-10-21 | 2024-10-22 | 0 |
Try it yourself
Which films cost more than 2.50 euros a day? Show title and price_per_day.
Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson