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 analyzing customer trends and wants to know which unique subscription plans are currently active.
Alex asks:
Can you provide a list of all the different subscription plans that exist in our database?
Your task is to query the netflix_users table and provide the requested data.
| user_id | name | country | signup_date | subscription_plan | is_active | |
|---|---|---|---|---|---|---|
| 1 | Alice Johnson | alice@example.com | USA | 2020-05-10T00:00:00 | Premium | true |
| 2 | Bob Smith | bob@example.com | Canada | 2021-02-22T00:00:00 | Standard | true |
| 3 | Charlie Lee | charlie@example.com | UK | 2019-09-13T00:00:00 | Basic | false |
| 4 | David Kim | david@example.com | Germany | 2023-01-05T00:00:00 | Premium | true |
| 5 | Emma Garcia | emma@example.com | Mexico | 2022-07-18T00:00:00 | Standard | true |
DISTINCTBy default, when you SELECT a column, SQL returns all records, even if some values repeat. The DISTINCT keyword removes duplicate values, ensuring that each unique value appears only once.
SELECT DISTINCT column_name
FROM table_name;SELECT DISTINCT subscription_plan
FROM netflix_users;| subscription_plan |
|---|
| Basic |
| Standard |
| Premium |
What’s Happening?
DISTINCT keyword removes duplicate subscription plans from the result set.We can also apply DISTINCT to multiple columns, ensuring unique combinations of values are returned.
SELECT DISTINCT subscription_plan, country
FROM netflix_users;| subscription_plan | country |
|---|---|
| Basic | USA |
| Premium | Canada |
| Standard | UK |
| Premium | Germany |
| Standard | Mexico |
What’s Happening?
subscription_plan and country appears only once.Netflix wants to identify all unique countries where users have signed up.
Filter condition:
country values from the netflix_users table.Expected Output:
| country |
|---|
| USA |
| Canada |
| UK |
| Germany |
| Mexico |
Write an SQL query to return the requested data.
Netflix’s data team wants to know the different combinations of users and series they have watched to understand viewer engagement. Sort the table based on the ascending order of user_id and series_id
Filter condition:
user_id and series_id from the watch_history table.Expected Output:
| user_id | series_id |
|---|---|
| 1 | 2 |
| 1 | 4 |
| 1 | 5 |
| 2 | 2 |
| 2 | 4 |
| 2 | 7 |
| 3 | 1 |
| 3 | 9 |
| 4 | 3 |
| 4 | 6 |
| 5 | 8 |
| 5 | 10 |
Write an SQL query to return the requested data.