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:
You’ve just been hired as a Data Analyst at Netflix! Your first task is to extract insights on the platform’s most successful series.
Your manager, Alex, has asked:
We need a report on our top-performing series, including their ratings, genres, and total viewership. Can you retrieve this data?
To get started, take a look at the netflix_series table, which contains key details:
| 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 SELECT statement is the foundation of SQL. It’s how we retrieve data from a database.
SELECT * FROM table_name;SELECT * FROM netflix_series;| series_id | title | genre | release_year | seasons | rating | total_views_millions |
|---|---|---|---|---|---|---|
| 101 | Stranger Things | Sci-Fi | 2016 | 4 | 8.7 | 140 |
| 102 | Squid Game | Thriller | 2021 | 1 | 8.0 | 200 |
| 103 | The Witcher | Fantasy | 2019 | 3 | 7.9 | 90 |
| 104 | Money Heist | Crime | 2017 | 5 | 8.2 | 180 |
| … | … | … | … | … | … | … |
What’s Happening?
Instead of retrieving all columns, you can specify which columns you need.
SELECT column1, column2 FROM table_name;SELECT title, genre, rating FROM netflix_series;| title | genre | rating |
|---|---|---|
| Stranger Things | Sci-Fi | 8.7 |
| Squid Game | Thriller | 8.0 |
| The Witcher | Fantasy | 7.9 |
| Money Heist | Crime | 8.2 |
| … | … | … |
Why is this better than SELECT *?
Your manager wants to create a report where column names are more readable.
SQL allows you to rename columns using AS.
SELECT column_name AS new_name FROM table_name;1SELECT title AS "Show Name",
2 genre AS "Category",
3 rating AS "IMDb Score"
4FROM netflix_series;| Show Name | Category | IMDb Score |
|---|---|---|
| Stranger Things | Sci-Fi | 8.7 |
| Squid Game | Thriller | 8.0 |
| The Witcher | Fantasy | 7.9 |
| Money Heist | Crime | 8.2 |
| … | … | … |
Why Use Aliases?
Your manager wants a list of Netflix series along with their release year and total viewership. However, the column names should be more readable for a company report.
Rename the columns as follows:
title → “Series Name”release_year → “Years Released”total_views_millions → “Total Views (Millions)”Write an SQL query to retrieve the requested data.
Expected Output:
| Series Name | Years Released | Total Views (Millions) |
|---|---|---|
| Stranger Things | 2016 | 140 |
| Squid Game | 2021 | 200 |
| The Witcher | 2019 | 90 |
| Money Heist | 2017 | 180 |
| Dark | 2017 | 85 |
| Bridgerton | 2020 | 100 |
| Breaking Bad | 2008 | 120 |
| Narcos | 2015 | 75 |
| The Crown | 2016 | 95 |
| Black Mirror | 2011 | 110 |
Write an SQL query to return the requested data.