Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

DISTINCT

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

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_idnameemailcountrysignup_datesubscription_planis_active
1Alice Johnsonalice@example.comUSA2020-05-10T00:00:00Premiumtrue
2Bob Smithbob@example.comCanada2021-02-22T00:00:00Standardtrue
3Charlie Leecharlie@example.comUK2019-09-13T00:00:00Basicfalse
4David Kimdavid@example.comGermany2023-01-05T00:00:00Premiumtrue
5Emma Garciaemma@example.comMexico2022-07-18T00:00:00Standardtrue

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_plancountry
BasicUSA
PremiumCanada
StandardUK
PremiumGermany
StandardMexico

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.

Upgrade to Pro

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_idseries_id
12
14
15
22
24
27
31
39
43
46
58
510

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?