DISTINCT on one column
- SQL course
- Beginner
- Lesson 66/115
- 4 min
List unique values of a single column.
Multi-column DISTINCT
DISTINCT works with multiple columns. Rows are unique by the combination of all columns.
SELECT DISTINCT city, category FROM customers;This query returns rows unique by the city and category combination.
city, category is selected from customers (DISTINCT removes duplicates).
Multi-column DISTINCT — details
Each column combination appears once. Order does not matter; the same combination in a different order is also deduplicated.
Practice Multi-column DISTINCT
Select unique city and category combinations from customers.
Starter code
-- two-column distinct
SELECT
FROM customersSample solution
SELECT DISTINCT city, category
FROM customersExpected output
city category İstanbul altın İzmir gümüş
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Two-column distinct
SELECT DISTINCT ____
FROM customers;
SELECT DISTINCT city, category
FROM customers;
Explanation: city, category is the right answer.
2 What does DISTINCT city, category deduplicate by?
- By the combination of both columns Correct answer
- Only by city
- Only by category
- Random
Explanation: DISTINCT deduplicates by all columns combined.
3 DISTINCT on one column: match the terms
| Term | Meaning |
|---|---|
DISTINCT |
deduplicate |
comma |
separate columns |
SELECT |
columns |
FROM |
table |