PRO SQL Modules
SQL Basics1h 20m • 4 lessons
Multiple Tables1h 40m • 5 lessons
Query Restructuring40m • 2 lessons
Data Transformation1h 40m • 5 lessons
Analytical SQL2h • 6 lessons
DISTINCT

Dan Lee
Lesson Objectives
By the end of this lesson, you will:
- Learn how to retrieve unique values using DISTINCT
- Understand when to use DISTINCT to remove duplicate rows
- Write SQL queries to extract distinct insights from Netflix’s user data
🎬 Scenario: Finding Unique Subscription Plans
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 |
Removing Duplicates with DISTINCT
By 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.
Syntax:
1SELECT DISTINCT column_name
2FROM table_name;
Example Query: Finding Unique Subscription Plans
1SELECT DISTINCT subscription_plan
2FROM netflix_users;
Expected Output:
subscription_plan |
---|
Basic |
Standard |
Premium |
What’s Happening?
- The DISTINCT keyword removes duplicate subscription plans from the result set.
- Instead of listing every user’s plan, we get a concise summary of all available options.
Using DISTINCT with Multiple Columns
We can also apply DISTINCT to multiple columns, ensuring unique combinations of values are returned.
Example Query: Finding Unique Subscription Plans by Country
1SELECT DISTINCT subscription_plan, country
2FROM netflix_users;
Expected Output:
subscription_plan | country |
---|---|
Basic | USA |
Premium | Canada |
Standard | UK |
Premium | Germany |
Standard | Mexico |
What’s Happening?
- The query ensures that each combination of subscription_plan and country appears only once.
✍️ SQL Exercises
Exercise 1: Identifying Countries with Netflix Users
Netflix wants to identify all unique countries where users have signed up.
Filter condition:
- Retrieve a distinct list of country values from the netflix_users table.
Expected Output:
country |
---|
USA |
Canada |
UK |
Germany |
Mexico |
Write an SQL query to return the requested data.
Exercise 2: Finding Unique Watch History Entries
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:
- Retrieve unique pairs of 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.