Beginner

382. COUNT DISTINCT

Learn how to count unique values in a column.

📘 Lesson 382 of 728 ⏱ 9 min 🎯 Beginner

COUNT(DISTINCT column) counts the unique non-NULL values contained in a column.

Counting unique customers, products, accounts, or regions is a common business requirement.

Count the number of unique customers who placed orders.

SQL
SELECT
    COUNT(DISTINCT CustomerID) AS UniqueCustomers
FROM Orders;

Analysts use COUNT DISTINCT for unique customer counts, active users, products sold, branches, and other business metrics.

  • COUNT DISTINCT counts unique values.
  • Duplicate values are counted once.
  • NULL values are generally excluded.
  • It is useful for customer and entity analysis.
  • It can be combined with GROUP BY.
What is COUNT(DISTINCT) used for?

It counts the number of unique non-NULL values in a column.

What is the difference between COUNT(*) and COUNT(DISTINCT CustomerID)?

COUNT(*) counts rows, while COUNT(DISTINCT CustomerID) counts unique customer IDs.

Have you finished this lesson?

Introduction to SQL – All Lessons