TopicsCheat sheetLessons
Lurndo › Topics › Comparisons

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
Result
titleminutes
Titanic194
Pulp Fiction154
Dune155
Oppenheimer180

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'
Result
idfilm_idmember_idrented_onreturned_onfine
258152024-10-20NULL0
261112024-10-212024-10-220

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
← PreviousFiltering rows with WHERENext →Combining conditions
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands