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 ranked list of the most popular series. Alex asks:
I want to see our hit series ranked by total viewership. Can you sort the results from most to least watched?
Your task is to use the ORDER BY clause to produce a properly sorted list of Netflix’s top shows.
| 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 ORDER BY clause sorts query results in a specific order, either ascending (ASC) or descending (DESC).
SELECT column1, column2
FROM table_name
ORDER BY column_name ASC | DESC;To list the highest-rated Netflix series first, use ORDER BY rating DESC.
SELECT title, rating
FROM netflix_series
ORDER BY rating DESC;| title | rating |
|---|---|
| Breaking Bad | 9.5 |
| Dark | 8.8 |
| Narcos | 8.8 |
| Black Mirror | 8.8 |
| Stranger Things | 8.7 |
| The Crown | 8.6 |
| Money Heist | 8.2 |
| Squid Game | 8 |
| The Witcher | 7.9 |
| Bridgerton | 7.3 |
What’s Happening?
To rank the most-watched Netflix shows, sort by total_views_millions in descending order.
SELECT title, total_views_millions
FROM netflix_series
ORDER BY total_views_millions DESC;| title | total_views_millions |
|---|---|
| Squid Game | 200 |
| Money Heist | 180 |
| Stranger Things | 140 |
| The Witcher | 90 |
| … | … |
What’s Happening?
You can sort by multiple columns to break ties. For example, if two series share the same rating, you can sort them by total viewership as a secondary criterion.
SELECT title, rating, total_views_millions
FROM netflix_series
ORDER BY rating DESC, total_views_millions DESC;| title | rating | total_views_millions |
|---|---|---|
| Breaking Bad | 9.5 | 120 |
| Black Mirror | 8.8 | 110 |
| Dark | 8.8 | 85 |
| Narcos | 8.8 | 75 |
| Stranger Things | 8.7 | 140 |
| The Crown | 8.6 | 95 |
| … | … | … |
What’s Happening?
Alex wants a report showing shows released after 2017, sorted by release year (newest first). If two shows have the same release year, they should be sorted by rating (highest first).
Expected Output:
| title | release_year | rating |
|---|---|---|
| Squid Game | 2021 | 8.0 |
| Bridgerton | 2020 | 7.3 |
| The Witcher | 2019 | 7.9 |
Write an SQL query to return the requested data.