Join ML Engineer Interview MasterClass (April Cohort) led by FAANG Data Scientists | Just 6 seats remaining...
ML Engineer MasterClass (April) | 6 seats left
By the end of this lesson, you will:
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_id | title | genre | release_year | seasons | rating | total_views_millions |
|---|---|---|---|---|---|---|
| 1 | Stranger Things | Sci-Fi | 2016 | 4 | 8.7 | 140 |
| 2 | Squid Game | Thriller | 2021 | 1 | 8 | 200 |
| 3 | The Witcher | Fantasy | 2019 | 3 | 7.9 | 90 |
| 4 | Money Heist | Crime | 2017 | 5 | 8.2 | 180 |
| 5 | Dark | Sci-Fi | 2017 | 3 | 8.8 | 85 |
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.
SELECT column1, column2
FROM table_name
WHERE condition;SELECT title, rating
FROM netflix_series
WHERE rating > 8.5;| title | rating |
|---|---|
| “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.We can use different operators to refine our filters:
| Operator | Description | Example Query |
|---|---|---|
= | Equals | WHERE genre = 'Sci-Fi' |
!= | Not equal | WHERE rating != 8.5 |
> | Greater than | WHERE rating > 8 |
< | Less than | WHERE release_year < 2020 |
>= | Greater than or equal to | WHERE seasons >= 3 |
<= | Less than or equal to | WHERE total_views_millions <= 150 |
SELECT title, release_year
FROM netflix_series
WHERE release_year >= 2020;| title | release_year |
|---|---|
| Squid Game | 2021 |
| Bridgerton | 2020 |
The AND and OR operators allow us to combine multiple conditions.
AND: Only returns rows that match all conditionsOR: Returns rows that match at least one conditionSELECT title, rating, release_year
FROM netflix_series
WHERE rating > 7.5 AND release_year > 2017;| title | rating | release_year |
|---|---|---|
| “Squid Game” | 8 | 2021 |
| “The Witcher” | 7.9 | 2019 |
What’s happening?
AND ensures that both conditions must be true for a row to appear.SELECT title, rating, release_year
FROM netflix_series
WHERE rating > 8.5 OR release_year > 2020;| title | rating | release_year |
|---|---|---|
| “Squid Game” | 8 | 2021 |
| “Stranger Things” | 8.7 | 2016 |
| “Dark” | 8.8 | 2017 |
| “Breaking Bad” | 9.5 | 2008 |
| “Narcos” | 8.8 | 2015 |
| “The Crown” | 8.6 | 2016 |
| “Black Mirror” | 8.8 | 2011 |
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 >= 2017rating >= 8.0Expected Output Example:
| title | release_year | rating |
|---|---|---|
| Squid Game | 2021 | 8.0 |
| Dark | 2017 | 8.8 |
| Money Heist | 2017 | 8.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:
| title | genre | rating |
|---|---|---|
| Money Heist | Crime | 8.2 |
| Stranger Things | Sci-Fi | 8.7 |
| Narcos | Crime | 8.8 |
| Dark | Sci-Fi | 8.8 |
| Black Mirror | Sci-Fi | 8.8 |
| Breaking Bad | Crime | 9.5 |
Write an SQL query to return the requested data.