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, wants a quick snapshot of the top 3 most-watched series. Instead of returning the entire dataset, they only need a short list.
Alex asks:
Can you get me the top 3 most-viewed Netflix series? We don’t need the full list, just the biggest hits.
Your task is to use LIMIT or OFFSET to retrieve only the highest-performing series from the netflix_series table.
| 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.0 | 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 |
| 6 | Bridgerton | Drama | 2020 | 2 | 7.3 | 100 |
| 7 | Breaking Bad | Crime | 2008 | 5 | 9.5 | 120 |
| 8 | Narcos | Crime | 2015 | 3 | 8.8 | 75 |
| 9 | The Crown | Drama | 2016 | 5 | 8.6 | 95 |
| 10 | Black Mirror | Sci-Fi | 2011 | 6 | 8.8 | 110 |
The LIMIT keyword restricts the number of rows returned by a query. It is supported in MySQL and PostgreSQL.
1SELECT column1, column2
2FROM table_name
3ORDER BY column_name DESC
4LIMIT n;LIMIT n returns only the first n rows of the result set.ORDER BY to return the top or bottom results.To return the top 3 Netflix series by viewership, we use LIMIT 3.
1SELECT title, total_views_millions
2FROM netflix_series
3ORDER BY total_views_millions DESC
4LIMIT 3;| title | total_views_millions |
|---|---|
| Squid Game | 200 |
| Money Heist | 180 |
| Stranger Things | 140 |
What’s Happening?
The OFFSET clause allows pagination, meaning you can skip a set number of rows before returning results.
1SELECT title, total_views_millions
2FROM netflix_series
3ORDER BY total_views_millions DESC
4LIMIT 3 OFFSET 3;| title | total_views_millions |
|---|---|
| Breaking Bad | 120 |
| Black Mirror | 110 |
| Bridgerton | 100 |
What’s Happening?
OFFSET 3 skips the first 3 rows.LIMIT 3 returns the next 3 rows, effectively showing the 4th to 6th most-watched series.Alex, your manager at Netflix, wants to know which shows have received the highest audience ratings. Write an SQL query to retrieve the top 5 highest-rated Netflix series, sorted by rating in descending order.
Expected Output Example:
| title | genre | rating |
|---|---|---|
| Breaking Bad | Crime | 9.5 |
| Dark | Sci-Fi | 8.8 |
| Black Mirror | Sci-Fi | 8.8 |
| Narcos | Crime | 8.8 |
| Stranger Things | Sci-Fi | 8.7 |
Write an SQL query to return the requested data.
Netflix wants to see the most recent signups to track new users. Write an SQL query to retrieve the 5 most recent users who signed up, sorted by signup_date in descending order.
Expected Output Example:
| name | signup_date |
|---|---|
| David Kim | 2023-01-05T00:00:00 |
| Emma Garcia | 2022-07-18T00:00:00 |
| Bob Smith | 2021-02-22T00:00:00 |
| Alice Johnson | 2020-05-10T00:00:00 |
| Charlie Lee | 2019-09-13T00:00:00 |
Write an SQL query to return the requested data.