In the realm of Structured Query Language (SQL), having a proper understanding of aggregate functions like COUNT, SUM, MIN, MAX, AVG, and their respective handling of NULL values is crucial. One such function, DISTINCT, is often misunderstood and misused. This article delves into the mechanics of the DISTINCT keyword in SQL and clarifies its usage in conjunction with the antigen function, COUNT. Let's dive in and demystify COUNT(DISTINCT).

COUNT is a fundamental SQL function that returns the number of rows within a specified set. When applied to a column, it usually counts all non-NULL values. However, if you need to count distinct (unique) values, that's where the DISTINCT keyword comes into play. But what exactly does COUNT(DISTINCT) do, and how does it differ from COUNT alone?

Counting vs. Counting Distinct
The primary difference lies in what these functions consider when calculating their totals:

COUNT considers each row, meaning it counts the number of rows, not the number of values. If a single row contains multiple duplicate values, COUNT still counts it as one row.
On the other hand, COUNT(DISTINCT) considers each unique value in a column as a separate entity. It ignores duplicates and NULL values, providing an accurate count of distinct, non-null values.

Differences in Action
Let's illustrate this with a simple example using a hypothetical Employees table:
| EmployeeID | FirstName | LastName |
|---|---|---|
| 1 | John | Doe |
| 2 | Jane | Doe |
| 1 | John | Doe |

Here, COUNT(*) would return 3 (all rows), while COUNT(DISTINCT EmployeeID) would also return 3 (three unique identifiers). But COUNT(DISTINCT LastName) would return 2 (Doe and another unique last name).
Ignoring NULL in COUNT(DISTINCT)
Another critical aspect is how COUNT(DISTINCT) handles NULL values. Unsurprisingly, it ignores NULL values, as they don't equate to any specific value. Here's how it works:

- COUNT(DISTINCT null) returns 0.
- COUNT(DISTINCT column) will ignore NULLs in the specified column and count only the non-null values.
Use Cases and Best Practices



















Knowing the difference between COUNT and COUNT(DISTINCT) enables you to craft more accurate queries. Here are some use cases:
COUNT(DISTINCT) is invaluable when you need to determine:
- The number of unique customers made a purchase.
- The variety of products sold.
- The distinct number of employees in each department.
Never use COUNT(*) when you want COUNT(DISTINCT). Inaccurate results can lead to skewed insights and flawed decision-making.
Alternative Solutions
While COUNT(DISTINCT) is powerful, it may not be the best fit for complex use cases. Consider:
- Using berdasarkan WHEN GROUP BY with functions like MIN() or MAX() to handle aggregations across complex conditions.
- Employing tools like PIVOT, CROSS APPLY, or Outer APPLY for advanced data manipulation.
In SQL, there's no one-size-fits-all solution, and particular use cases might demand more complex constructions. But having a solid grasp of the fundamentals, like understanding COUNT(DISTINCT), will provide a sturdy foundation for tackling even the most daunting data challenges.