CodeBlanks Logo Icon CodeBlanks

Multi-column DISTINCT

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

query.sql
-- count cities
SELECT
FROM customers

Sample solution

query.sql
SELECT COUNT(DISTINCT city) AS sehir_sayisi
FROM customers

Expected 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

Question
SELECT COUNT(____ city) FROM customers;
Answer
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.

All lessons in Beginner