DISTINCT on Multiple Columns
Uniqueness across combinations.
DISTINCT on Multiple Columns is a free SQL Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Multiple Columns?
You already know that SELECT DISTINCT removes duplicate rows. But what does "duplicate" mean when your query returns several columns?
A row is only considered a duplicate when every selected column has the same value as another row. If even one column differs, both rows are kept.
Sample Table: Orders
Let's create a small orders table to experiment with. It stores which customer bought from which category and in which country.
CREATE TABLE orders (
id INTEGER,
customer VARCHAR(50),
category VARCHAR(50),
country VARCHAR(50)
);
INSERT INTO orders VALUES
(1, 'Alice', 'Electronics', 'US'),
(2, 'Alice', 'Books', 'US'),
(3, 'Bob', 'Electronics', 'UK'),
(4, 'Alice', 'Electronics', 'US'),
(5, 'Bob', 'Electronics', 'UK'),
(6, 'Carol', 'Clothing', 'CA');DISTINCT on One Column
A quick reminder: using DISTINCT on a single column returns each unique value for that column only.
Here we get every unique customer name — just three rows, even though there are six orders.
SELECT DISTINCT customer
FROM orders;DISTINCT on Two Columns
Now add a second column. The keyword DISTINCT still appears only once, right after SELECT. The database treats each unique (customer, category) pair as one row.
Alice appears twice — once for Electronics and once for Books — because those are two different combinations.
SELECT DISTINCT customer, category
FROM orders;How Uniqueness Is Checked
The database compares complete row snapshots. For DISTINCT to eliminate a row, all columns in the SELECT list must match an already-seen row exactly.
In our orders table, rows 3 and 5 are both (Bob, Electronics, UK) — a perfect match on all three columns — so one of them is eliminated.
SELECT DISTINCT customer, category, country
FROM orders;
-- Row (Bob, Electronics, UK) appears only once
-- even though it occurs on rows 3 and 5Partial Match Is Not Eliminated
Alice has two rows with category = Electronics, but they both have country = US. With all three columns selected, those two original rows are identical, so only one survives.
Change the selection and the uniqueness test changes too.
-- Select only customer + country
-- (Alice, US) appears twice in the table but DISTINCT keeps it once
SELECT DISTINCT customer, country
FROM orders;
-- Now add category back — uniqueness recalculated
SELECT DISTINCT customer, category, country
FROM orders;Real-World Use Case: Unique Pairs
A common pattern is finding every unique combination of two attributes — for example, which product categories are sold in which countries, regardless of how many times each sale occurred.
SELECT DISTINCT category, country
FROM orders
ORDER BY category, country;Combining DISTINCT with WHERE
You can filter rows first with WHERE, and then DISTINCT removes duplicates from the filtered result. The two clauses work independently — WHERE narrows the data, DISTINCT deduplicates it.
SELECT DISTINCT customer, category
FROM orders
WHERE country = 'US';DISTINCT vs GROUP BY
An equivalent way to get unique combinations is GROUP BY. Both approaches produce the same result for plain deduplication. The difference matters when you want aggregates: GROUP BY lets you add COUNT(*), SUM(), etc., while DISTINCT does not.
-- Using DISTINCT
SELECT DISTINCT customer, category
FROM orders;
-- Equivalent using GROUP BY
SELECT customer, category
FROM orders
GROUP BY customer, category;Counting Unique Combinations
Wrapping a multi-column DISTINCT inside a subquery lets you count how many unique combinations exist. This is a handy way to measure diversity in your data.
SELECT COUNT(*) AS unique_customer_category_pairs
FROM (
SELECT DISTINCT customer, category
FROM orders
) AS deduped;Common Mistake: DISTINCT Applies to All Columns
A frequent misconception is that DISTINCT applies only to the first column listed. It does not. DISTINCT always considers the entire row formed by all selected columns.
You cannot use DISTINCT on only some columns while keeping duplicates in others — for that, you need window functions like ROW_NUMBER().
-- This does NOT give distinct customers with any category
-- It gives distinct (customer, category) pairs
SELECT DISTINCT customer, category
FROM orders;
-- If you want one row per customer (any category), use:
SELECT customer, MIN(category) AS sample_category
FROM orders
GROUP BY customer;Quick Check
Test your understanding of DISTINCT on multiple columns.
Lesson Recap
Great work! Here is what you learned about using DISTINCT on multiple columns:
- DISTINCT applies to the whole row — all selected columns are checked together.
- A row is eliminated only when every column matches an already-seen row exactly.
- Adding or removing columns from the SELECT list changes which rows are considered duplicates.
- You can combine DISTINCT with WHERE and ORDER BY freely.
- GROUP BY is an equivalent alternative and is required when you also need aggregates.
Mastering multi-column DISTINCT lets you efficiently explore unique combinations in any dataset.
Frequently asked questions
Is the “DISTINCT on Multiple Columns” lesson free?
Yes — the full text of “DISTINCT on Multiple Columns” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.
What will I learn in “DISTINCT on Multiple Columns”?
Uniqueness across combinations. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Academy?
No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “DISTINCT on Multiple Columns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Academy lesson?
Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SELECT DISTINCT Basics
- DISTINCT on Multiple Columns
- PostgreSQL DISTINCT ON
- Counting Unique Values