ML Engineer MasterClass (April) | 6 seats left

BETWEEN

BETWEEN

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to filter numeric and date ranges using BETWEEN
  • Understand how BETWEEN simplifies range conditions compared to multiple comparisons
  • Write SQL queries to find Netflix series within a specific release year or rating range

🎬 Scenario: Finding Mid-Range Netflix Releases

Your manager at Netflix, Alex, is analyzing mid-era releases and wants to focus on shows released between 2015 and 2020.

Alex asks:

Can you retrieve all Netflix series that were released between 2015 and 2020?

Your task is to query the netflix_series table and provide the requested data.

series_idtitlegenrerelease_yearseasonsratingtotal_views_millions
1Stranger ThingsSci-Fi201648.7140
2Squid GameThriller202118200
3The WitcherFantasy201937.990
4Money HeistCrime201758.2180
5DarkSci-Fi201738.885

1. Filtering Data with BETWEEN

The BETWEEN operator filters results within a specified range, including both the lower and upper values.

Syntax

SQL
SELECT column1, column2  
FROM table_name  
WHERE column_name BETWEEN value1 AND value2;
  • BETWEEN value1 AND value2 includes both value1 and value2 in the result.
  • It’s equivalent to: WHERE column_name >= value1 AND column_name <= value2;

2. Using BETWEEN for Release Year Filtering

Instead of writing:

SQL
SELECT title, release_year  
FROM netflix_series  
WHERE release_year >= 2015 AND release_year <= 2020;

You can write:

SQL
SELECT title, release_year  
FROM netflix_series  
WHERE release_year BETWEEN 2015 AND 2020;
titlerelease_year
Narcos2015
Stranger Things2016
The Crown2016
Money Heist2017
Dark2017
The Witcher2019
Bridgerton2020

What’s Happening?

  • BETWEEN 2015 AND 2020 filters results to only include release years from 2015 to 2020.
  • The same query using >= and <= would be longer and harder to read.

3. Using BETWEEN for Ratings

You can also apply BETWEEN to numeric values like ratings.

Example Query: Finding Shows with Ratings Between 8.0 and 9.0

SQL
SELECT title, rating  
FROM netflix_series  
WHERE rating BETWEEN 8.0 AND 9.0;
titlerating
“Stranger Things”8.7
“Squid Game”8
“Money Heist”8.2
“Dark”8.8
“Narcos”8.8
“The Crown”8.6
“Black Mirror”8.8

What’s Happening?

  • BETWEEN 8.0 AND 9.0 filters results to only include shows with ratings between 8.0 and 9.0.
  • The query still includes 8.0 and 9.0.

✍️ SQL Exercises

Exercise 1: Finding Shows Released Between 2015 and 2020

Alex wants a report showing all series released between 2015 and 2020, sorted by release_year in ascending order, then title in ascending order.

Filter condition:

  • release_year must be between 2015 and 2020.
  • Sort by release_year in ascending order, then title in ascending order.
titlerelease_year
Narcos2015
Stranger Things2016
The Crown2016
Money Heist2017
Dark2017
The Witcher2019
Bridgerton2020

Write an SQL query to return the requested data.


Exercise 2: Finding Shows with Ratings Between 8.0 and 9.0

Alex wants to focus on highly-rated Netflix shows that have ratings between 8.0 and 9.0.

Filter condition:

  • rating must be between 8.0 and 9.0.
  • Sort by rating in ascending order, then title in ascending order.
titlerating
“Squid Game”8
“Money Heist”8.2
“The Crown”8.6
“Stranger Things”8.7
“Black Mirror”8.8
“Dark”8.8
“Narcos”8.8

Write an SQL query to return the requested data.