TopicsCheat sheetLessons
Lurndo › Topics › BETWEEN

Learn SQL: BETWEEN

From … to with BETWEEN

Vic is planning a nineties night. "From 1990 up to and including 1999. The ones from 1990 too, mind!" Borders matter, and `BETWEEN` includes them.

BETWEEN includes the borders

year BETWEEN 1990 AND 1999 means year >= 1990 AND year <= 1999. Both borders count.

Look at the two highlighted rows: The Matrix (1999) and Home Alone (1990) sit exactly on the border, so they belong in the list.

SELECT title, year FROM films WHERE year BETWEEN 1990 AND 1999
Result
titleyear
The Matrix1999
Alles moet weg1996
Jurassic Park1993
Titanic1997
Toy Story1995
Pulp Fiction1994
The Lion King1994
Home Alone1990

Dates too, and NOT BETWEEN

BETWEEN also works for dates stored as text, and with NOT BETWEEN you find everything outside the range. Always put the smallest value first.

This query shows the rentals from the first week of October.

SELECT * FROM rentals WHERE rented_on BETWEEN '2024-10-01' AND '2024-10-07'
Result
idfilm_idmember_idrented_onreturned_onfine
1132024-10-012024-10-030
2672024-10-012024-10-020
31912024-10-022024-10-084.5
42242024-10-032024-10-040
5792024-10-032024-10-050
612122024-10-042024-10-126
71062024-10-052024-10-060
821152024-10-052024-10-070

Try it yourself

Which members were born between 1980 and 1995, borders included? Show name and birth year.

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
← PreviousA list with INNext →Searching text with LIKE
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands