COUNT(DISTINCT)
COUNT(DISTINCT) is a SQL function in the Aggregate Functions category. Counts only unique non-NULL values in a column. The syntax is COUNT(DISTINCT column). It accepts DISTINCT column. It returns distinct count. A typical example: SELECT COUNT(DISTINCT department) AS unique_depts FROM employees; -- Number of different departments SELECT COUNT(DISTINCT city) FROM customers; -- Unique cities in customer base SELECT department, COUNT(DISTINCT job_title) AS unique_roles FROM employees GROUP BY department; -- Role diversity per department -- Performance: COUNT(DISTINCT) can be slow on large datasets -- Approximation (PostgreSQL): -- SELECT APPROX_COUNT_DISTINCT(column) FROM table; A close relative is COUNT(), which returns the number of rows (or non-NULL values) in a group. A close relative is SUM(), which returns the sum of all non-NULL values in a group. Works with numeric columns. A close relative is AVG(), which returns the average (mean) of all non-NULL values in a group. More about this category: Aggregation functions — COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING.