ML Engineer MasterClass (April) | 6 seats left

WHERE

WHERE

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to filter data using WHERE
  • Understand comparison operators (=, >, <, >=, <=, !=)
  • Write SQL queries to extract specific insights from Netflix’s hit series data

🎬 Scenario: Identifying High-Rated Netflix Series

Your manager at Netflix, Alex, is preparing a report on top-rated shows and wants to filter out lower-performing series.

Alex asks:

Can you retrieve only the shows that have a rating above 8.5? We need to focus on our highest-quality content.

Your job 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 WHERE

The WHERE clause filters rows based on specific conditions. Instead of retrieving every record in a table, we can narrow results based on ratings, release years, or genres.

Syntax

SQL
SELECT column1, column2
FROM table_name
WHERE condition;

Example Query: Retrieving Top-Rated Series

SQL
SELECT title, rating  
FROM netflix_series  
WHERE rating > 8.5;

Output Example

titlerating
“Stranger Things”8.7
“Dark”8.8
“Breaking Bad”9.5
“Narcos”8.8
“The Crown”8.6
“Black Mirror”8.8

What’s Happening?

  • WHERE rating > 8.5 filters the results so that only shows with a rating greater than 8.5 appear.
  • The dataset excludes any series with a rating of 8.5 or below.

2. Using Comparison Operators

We can use different operators to refine our filters:

OperatorDescriptionExample Query
=EqualsWHERE genre = 'Sci-Fi'
!=Not equalWHERE rating != 8.5
>Greater thanWHERE rating > 8
<Less thanWHERE release_year < 2020
>=Greater than or equal toWHERE seasons >= 3
<=Less than or equal toWHERE total_views_millions <= 150

Example Query: Finding Recent Releases

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

Output Example

titlerelease_year
Squid Game2021
Bridgerton2020

3. Filtering with Multiple Conditions

The AND and OR operators allow us to combine multiple conditions.

  • AND: Only returns rows that match all conditions
  • OR: Returns rows that match at least one condition

Example Query: Finding High-Rated Shows Released After 2018

SQL
SELECT title, rating, release_year 
FROM netflix_series 
WHERE rating > 7.5 AND release_year > 2017;

Output Example

titleratingrelease_year
“Squid Game”82021
“The Witcher”7.92019

What’s happening?

  • AND ensures that both conditions must be true for a row to appear.
  • The result excludes any older or lower-rated shows.

Example Query: Finding Either High-Rated or Recent Shows

SQL
SELECT title, rating, release_year 
FROM netflix_series 
WHERE rating > 8.5 OR release_year > 2020;

Output Example

titleratingrelease_year
“Squid Game”82021
“Stranger Things”8.72016
“Dark”8.82017
“Breaking Bad”9.52008
“Narcos”8.82015
“The Crown”8.62016
“Black Mirror”8.82011

✍️ SQL Exercises

Exercise 1: Filtering High-Rated Netflix Series

Alex wants a filtered report showing Netflix series that were released in or after 2017 and have a rating of at least 8.0.

Filter conditions:

  • release_year >= 2017
  • rating >= 8.0

Expected Output Example:

titlerelease_yearrating
Squid Game20218.0
Dark20178.8
Money Heist20178.2

Write an SQL query to return the requested data.


Netflix executives want to analyze popular shows in the “Crime” and “Sci-Fi” genres. Your task is to retrieve all Netflix series that belong to either of these genres. Order the table in the ascending order of rating.

Filter conditions:

  • genre must be ‘Crime’ or ‘Sci-Fi’.

Expected Output Example:

titlegenrerating
Money HeistCrime8.2
Stranger ThingsSci-Fi8.7
NarcosCrime8.8
DarkSci-Fi8.8
Black MirrorSci-Fi8.8
Breaking BadCrime9.5

Write an SQL query to return the requested data.