Multi-column DISTINCT
- SQL course
- Beginner
- Lesson 67/115
- 4 min
Use DISTINCT across multiple columns.
DISTINCT COUNT
With COUNT(DISTINCT column) you count unique values. Duplicates are counted once.
SELECT COUNT(DISTINCT city) AS sehir_sayisi FROM customers;This query counts how many different cities there are.
COUNT(DISTINCT city) AS sehir_sayisi is selected from customers.
DISTINCT COUNT — details
COUNT(DISTINCT) counts non-NULL unique values. In MySQL DISTINCT alone is not enough for counting; it pairs with COUNT.
Practice DISTINCT COUNT
Count unique cities in customers aliased as city_count.
Starter code
-- count cities
SELECT
FROM customersSample solution
SELECT COUNT(DISTINCT city) AS sehir_sayisi
FROM customersExpected output
sehir_sayisi 3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Multi-column DISTINCT: match the terms
| Term | Meaning |
|---|---|
COUNT |
count |
DISTINCT |
deduplicate |
SUM |
sum |
AVG |
average |
2 Count distinct
SELECT COUNT(____ city) FROM customers;
SELECT COUNT(DISTINCT city) FROM customers;
Explanation: DISTINCT is the right answer.
3 What does COUNT(DISTINCT city) count?
- Total rows
- The number of distinct cities Correct answer
- Duplicate rows
- Empty rows
Explanation: COUNT(DISTINCT) counts unique values.