sql count distinct

Galaxy Glossary

How do you count unique values in a SQL table?

The `COUNT DISTINCT` function in SQL is used to count the unique values within a specific column. It's crucial for getting a precise count of distinct items, unlike the standard `COUNT` function which counts all rows.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The `COUNT DISTINCT` function is a powerful tool in SQL for obtaining the number of unique values in a particular column. It's often used in data analysis to understand the variety of data present. For example, in a customer database, you might want to know how many different countries your customers are from. `COUNT DISTINCT` is the perfect solution. Unlike the standard `COUNT` function, which counts all rows, `COUNT DISTINCT` only counts each unique value once. This is essential for accurate reporting and analysis, especially when dealing with repeated data entries. For instance, if a product name appears multiple times in an inventory table, `COUNT` would count each occurrence, while `COUNT DISTINCT` would only count the product name once. This function is highly versatile and can be used in conjunction with other SQL functions and clauses to perform more complex calculations and analyses. It's a fundamental building block for data summarization and reporting.

Why sql count distinct is important

The `COUNT DISTINCT` function is vital for accurate data analysis and reporting. It allows you to understand the true variety of data present in a column, avoiding inflated counts due to repeated values. This is crucial for tasks like calculating unique customer locations, product types, or any other situation where you need a precise count of distinct items.

Example Usage

```sql -- Sample table: Customers CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, Country VARCHAR(50) ); INSERT INTO Customers (CustomerID, Country) VALUES (1, 'USA'), (2, 'Canada'), (3, 'USA'), (4, 'Mexico'), (5, 'Canada'), (6, 'USA'); -- Count distinct countries SELECT COUNT(DISTINCT Country) AS DistinctCountries FROM Customers; ```

Common Mistakes

Want to learn about other SQL terms?