Part 2: Mastering the DISTINCT Command

Part 2: Mastering the DISTINCT Command | SQL Challenges & Capstone
SQL Filtering & Search Commands Practice · Part 2 · Lecture Notes

Mastering the DISTINCT Command

How to identify unique values, remove duplicates, and explore business categories β€” the first-look query every analyst runs on a new dataset.

Part 2 of 633% Complete
DISTINCT
Unique Values
Removing Duplicates
Multi-Column
8 Practice Questions

Learning Objectives

  • Use DISTINCT to remove duplicate values and return only unique records from a column
  • Apply DISTINCT to explore categories, statuses, and classifications in business tables
  • Understand how DISTINCT works across multiple columns to return unique row combinations
  • Use DISTINCT as a first-look analytical tool before building targeted WHERE filters
Click any section header to open or close it

DISTINCT removes duplicate values from query results and returns only unique records. Without DISTINCT, a query returns every row β€” including repeated values. With DISTINCT, each unique value appears only once in the output, no matter how many times it exists in the table.

In industry, analysts use DISTINCT to identify unique customers, review available product categories, summarize business classifications, remove duplicate records, and explore business segments before building more complex queries.

The Basic Structure

DISTINCT goes immediately after SELECT and before the column name. It applies to the entire row of selected columns β€” if you list two columns, it returns unique combinations of both.

Basic DISTINCT Syntax
SELECT DISTINCT column_name FROM table_name;

DISTINCT is the fastest way to answer the question: “what unique values exist in this column?” Instead of scanning through hundreds of repeated values, DISTINCT collapses them into a clean list of one-per-value output.

A First-Look Analytical Pattern

Analysts frequently run DISTINCT on category, status, and country columns at the start of an analysis to understand what business segments exist in the data before writing more targeted queries.

Unique Countries from Users
SELECT DISTINCT country FROM users;
Unique Transaction Types
SELECT DISTINCT transaction_type FROM transactions;

Product categories, order statuses, subscription tiers, and ticket classifications are all examples of columns where DISTINCT immediately reveals the full set of business values. This is typically one of the first queries an analyst runs when working with a new table or a new business domain.

Why This Matters Before Writing Filters

Knowing the exact values in a status or category column means you can write WHERE filters correctly. If you assume a status is called “active” but the data stores it as “Active”, your filter returns nothing β€” DISTINCT prevents that mistake.

Unique Product Categories
SELECT DISTINCT category FROM products;

When you list multiple columns after SELECT DISTINCT, the database returns unique combinations of all those columns together. Two rows are considered duplicates only if every selected column matches β€” not just one.

How Multi-Column DISTINCT Works

SELECT DISTINCT country, user_type returns one row per unique country + user_type pair. If Canada has both Retail and Business users, both combinations appear. DISTINCT does not mean “unique per column” β€” it means “unique per complete row of selected columns.”

Unique Country and User Type Combinations
SELECT DISTINCT country, user_type FROM users;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Retrieve unique countries from the users table
AI Prompt
Write a MySQL query to show unique countries from the users table.
SQL Answer
SELECT DISTINCT country FROM users;
Practice 2 Retrieve unique transaction types from the transactions table
AI Prompt
Write a MySQL query to show unique transaction types from the transactions table.
SQL Answer
SELECT DISTINCT transaction_type FROM transactions;
Practice 3 Retrieve unique product categories from the products table
AI Prompt
Write a MySQL query to show unique product categories from the products table.
SQL Answer
SELECT DISTINCT category FROM products;
Practice 4 Retrieve unique order statuses from the orders table
AI Prompt
Write a MySQL query to show unique order statuses from the orders table.
SQL Answer
SELECT DISTINCT order_status FROM orders;
Practice 5 Retrieve unique ticket statuses from the support_tickets table
AI Prompt
Write a MySQL query to show unique ticket statuses from the support_tickets table.
SQL Answer
SELECT DISTINCT ticket_status FROM support_tickets;
Practice 6 Retrieve unique subscription statuses from the subscriptions table
AI Prompt
Write a MySQL query to show unique subscription statuses from the subscriptions table.
SQL Answer
SELECT DISTINCT status FROM subscriptions;
Practice 7 Retrieve unique device types from the sessions table
AI Prompt
Write a MySQL query to show unique device types from the sessions table.
SQL Answer
SELECT DISTINCT device_type FROM sessions;
Practice 8 Retrieve unique combinations of country and user_type from the users table
AI Prompt
Write a MySQL query to show unique combinations of country and user_type from the users table.
SQL Answer
SELECT DISTINCT country, user_type FROM users;

Knowledge Check

Five questions β€” select the best answer for each.

1. What does DISTINCT do in a SQL query?
AIt sorts results from A to Z
BIt removes duplicate values and returns only unique records
CIt counts the number of rows in each group
DIt limits the output to a fixed number of rows
2. Where does DISTINCT appear in a SELECT statement?
AAfter FROM
BAfter WHERE
CImmediately after SELECT
DAt the end of the query
3. An analyst runs SELECT DISTINCT country FROM users. The users table has 8 rows with Canada appearing 5 times, United States 2 times, and United Kingdom 1 time. How many rows does the result contain?
A8
B5
C3
D1
4. What does SELECT DISTINCT country, user_type FROM users return?
AOnly the most common country value
BOne row per user
CUnique combinations of country and user_type together
DOne row per unique country, ignoring user_type
5. Why do analysts use DISTINCT on status and category columns at the start of an analysis?
ATo sort the values alphabetically
BTo see all unique business values before writing filter conditions
CTo delete duplicate rows from the table
DTo count how many records exist per status

Part 2 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

DISTINCT removes duplicate values so analysts can identify unique business information, categories, and classifications. Place DISTINCT immediately after SELECT. On multiple columns, it returns unique combinations of all listed columns together.

DISTINCTUnique ValuesDuplicatesCategoriesMulti-ColumnBusiness SegmentsFirst-Look Analysis