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!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

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.

SQL Count Distinct Example Usage


-- Example checking for null values in the 'customer' table
SELECT customer_id, customer_name
FROM customer
WHERE customer_name IS NULL;

-- Example checking for data type mismatch in the 'order_date' column
SELECT order_id, order_date
FROM orders
WHERE order_date NOT LIKE '%/%/%';

-- Example checking for constraint violations (e.g., foreign key)
SELECT o.order_id, o.customer_id
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;

SQL Count Distinct Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

When should I use COUNT DISTINCT instead of a regular COUNT?

Use COUNT DISTINCT whenever you need the number of unique values in a column rather than the total number of rows. For example, to see how many different countries your customers come from, COUNT DISTINCT(country) guarantees each country is counted once, eliminating duplicate entries that would inflate a plain COUNT result.

How does COUNT DISTINCT improve reporting accuracy with repeated data like product names?

Repeated data can distort metrics. If a product name appears 100 times in an inventory table, COUNT returns 100, suggesting 100 different products. COUNT DISTINCT(product_name) returns 1, giving you a true picture of catalog breadth. This distinction is crucial for dashboards, KPI tracking, and any analysis where data duplication skews insights.

Can Galaxy help me write and optimize COUNT DISTINCT queries faster?

Absolutely. Galaxy’s lightning-fast SQL editor offers context-aware autocomplete, instant table metadata, and an AI copilot that can generate or refactor COUNT DISTINCT queries in seconds. You can even chat with your database, share endorsed queries with teammates, and avoid the copy-paste churn common in legacy editors—dramatically accelerating accurate, unique-value reporting.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.