SQL is a programming language that is widely used for managing relational databases. One of the most commonly used SQL functions is the COUNT function. It allows you to count the number of records in a table or a subset of a table that satisfy a given condition. However, in some cases, you may want to count only the number of distinct values in a column or columns. In this article, we will discuss how to use SQL COUNT function with DISTINCT, along with some code examples.
What is SQL COUNT with DISTINCT?
SQL COUNT function is used to count the number of rows in a table or a subset of a table that meet a specified condition. However, if you want to count only the distinct values in a column or columns, you need to use SQL COUNT with DISTINCT. The DISTINCT keyword is used to return only the distinct or unique values from a column or columns. This means that if a column has duplicates, they will be treated as a single value and counted only once.
Syntax of SQL COUNT with DISTINCT
The syntax of SQL COUNT with DISTINCT is as follows:
SELECT COUNT(DISTINCT column_name) FROM table_name;
The SELECT statement is used to select the data from a table. The COUNT function is used to count the number of distinct values in a column or columns. The DISTINCT keyword is used to return only the unique values in the specified column or columns.
Examples of SQL COUNT with DISTINCT
Let's take a look at some examples of SQL COUNT with DISTINCT.
Example 1: Count the number of unique values in a single column
Suppose we have a table named "employees" with the following data:
emp_id | emp_name | emp_dept |
---|---|---|
1 | John | HR |
2 | Sarah | IT |
3 | John | HR |
4 | James | IT |
5 | Sarah | HR |
We want to count the number of unique values in the "emp_name" column. Here's how we can do it:
SELECT COUNT(DISTINCT emp_name) FROM employees;
The result will be:
COUNT(DISTINCT emp_name) |
---|
3 |
Note that the "emp_name" column has three unique values: John, Sarah, and James.
Example 2: Count the number of unique values in multiple columns
Suppose we have a table named "orders" with the following data:
order_id | customer_name | order_date |
---|---|---|
1 | John | 2021-01-01 |
2 | Sarah | 2021-01-02 |
3 | John | 2021-01-03 |
4 | James | 2021-01-04 |
5 | Sarah | 2021-01-05 |
We want to count the number of unique values in the "customer_name" and "order_date" columns. Here's how we can do it:
SELECT COUNT(DISTINCT customer_name, order_date) FROM orders;
The result will be:
COUNT(DISTINCT customer_name, order_date) |
---|
5 |
Note that the combination of "customer_name" and "order_date" columns has five unique values: John, Sarah, James, and the two orders placed by Sarah on different dates.
Example 3: Count the number of unique values in a joined table
Suppose we have two tables named "employees" and "departments" with the following data:
employees table:
emp_id | emp_name | dept_id |
---|---|---|
1 | John | 1 |
2 | Sarah | 2 |
3 | John | 1 |
4 | James | 2 |
5 | Sarah | 1 |
departments table:
dept_id | dept_name |
---|---|
1 | HR |
2 | IT |
3 | Finance |
We want to count the number of unique values in the "dept_name" column, after joining the "employees" and "departments" tables on the "dept_id" column. Here's how we can do it:
SELECT COUNT(DISTINCT dept_name) FROM employees JOIN departments USING (dept_id);
The result will be:
COUNT(DISTINCT dept_name) |
---|
2 |
Note that there are only two unique "dept_name" values: HR and IT, even though there are three unique "dept_id" values in the "employees" table.
Conclusion
In this article, we discussed SQL COUNT with DISTINCT, which allows you to count only the unique or distinct values in a column or columns. We also provided some code examples to illustrate how to use this function. SQL COUNT with DISTINCT is a powerful tool that can help you get more precise results from your queries, so be sure to add it to your SQL toolkit.
Sure! Let's start with SQL COUNT function.
SQL COUNT Function
SQL COUNT function is used to count the number of rows in a table or a subset of a table that meet a specified condition. It is one of the most commonly used SQL functions, and is frequently used with the WHERE clause to filter the rows that need to be counted. Here's the basic syntax of the SQL COUNT function:
SELECT COUNT(column_name) FROM table_name WHERE condition;
The COUNT function takes a column as an argument, and returns the number of non-null values in that column. If you want to count all the rows in a table, you can use the asterisk (*) symbol instead of a column name, like this:
SELECT COUNT(*) FROM table_name;
Note that COUNT function does not count NULL values, and only counts the rows with non-null values.
SQL DISTINCT Keyword
SQL DISTINCT keyword is used to return only the unique or distinct values from a table. It eliminates the duplicate rows from the result set and returns only the unique values. Here's the syntax of SQL DISTINCT keyword:
SELECT DISTINCT column_name FROM table_name;
The DISTINCT keyword can be used with the SELECT statement to return only the unique values from a column or columns.
SQL COUNT with DISTINCT
Now that we know about SQL COUNT function and SQL DISTINCT keyword, we can use them together to count only the unique values in a column or columns. Here's the syntax of SQL COUNT with DISTINCT:
SELECT COUNT(DISTINCT column_name) FROM table_name WHERE condition;
The COUNT function is used along with the DISTINCT keyword to count only the unique values in a column or columns. The WHERE clause is optional, and is used to filter the rows that need to be counted. The result set will contain only one row with one column, which will give the count of the unique values in the specified column or columns.
SQL COUNT with DISTINCT Examples
Let's see some examples of SQL COUNT with DISTINCT:
Example 1: Count the number of unique values in a single column
Suppose we have a table named "students" with the following data:
student_id | student_name | date_of_birth |
---|---|---|
1 | John | 2000-01-01 |
2 | Sarah | 2000-02-01 |
3 | John | 2000-03-01 |
4 | James | 2000-04-01 |
5 | Sarah | 2000-05-01 |
We want to count the number of unique values in the "student_name" column. Here's how we can do it:
SELECT COUNT(DISTINCT student_name) FROM students;
The result will be:
COUNT(DISTINCT student_name) |
---|
3 |
Note that the "student_name" column has three unique values: John, Sarah, and James.
Example 2: Count the number of unique values in multiple columns
Suppose we have a table named "sales" with the following data:
sales_id | customer_name | product_name | sale_date |
---|---|---|---|
1 | John | Laptop | 2021-01-01 |
2 | Sarah | Tablet | 2021-01-02 |
3 | John | Laptop | 2021-01-03 |
4 | James | Smartphone | 2021-01-04 |
5 | Sarah | Laptop | 2021-01-05 |
We want to count the number of unique values for each combination of "customer_name" and "product_name". Here's how we can do it:
SELECT COUNT(DISTINCT customer_name, product_name) FROM sales;
The result will be:
COUNT(DISTINCT customer_name, product_name) |
---|
4 |
Note that there are four unique combinations of "customer_name" and "product_name": John-Laptop, Sarah-Tablet, James-Smartphone, and Sarah-Laptop.
Example 3: Count the number of unique values in a joined table
Suppose we have two tables named "orders" and "customers" with the following data:
orders table:
order_id | customer_id | order_date |
---|---|---|
1 | 1 | 2021-01-01 |
2 | 2 | 2021-01-02 |
3 | 1 | 2021-01-03 |
4 | 3 | 2021-01-04 |
5 | 2 | 2021-01-05 |
customers table:
customer_id | customer_name |
---|---|
1 | John |
2 | Sarah |
3 | James |
We want to count the number of unique customers who placed orders. Here's how we can do it:
SELECT COUNT(DISTINCT orders.customer_id) FROM orders JOIN customers ON orders.customer_id = customers.customer_id;
The result will be:
COUNT(DISTINCT orders.customer_id) |
---|
3 |
Note that there are three unique customers who placed orders: John, Sarah, and James.
Conclusion
Using SQL COUNT with DISTINCT is a powerful tool that you can use to count only the unique values in a column or columns. This can be useful in many scenarios, such as counting the number of unique customers, products, or orders in a database. It's also a great way to get more precise results from your queries, and to ensure that you're not double-counting or including duplicates in your analysis.
Popular questions
- What is SQL COUNT with DISTINCT used for?
SQL COUNT with DISTINCT is used to count only the unique values in a column or columns. It allows you to eliminate duplicate rows from the result set and count only the distinct values.
- What is the syntax for SQL COUNT with DISTINCT?
The syntax for SQL COUNT with DISTINCT is:
SELECT COUNT(DISTINCT column_name) FROM table_name WHERE condition;
The COUNT function is used along with the DISTINCT keyword to count only the unique values in a column or columns. The WHERE clause is optional and is used to filter the rows that need to be counted.
- How does SQL COUNT with DISTINCT work?
SQL COUNT with DISTINCT works by counting only the unique values in a column or columns. When the DISTINCT keyword is used, it eliminates the duplicate rows from the result set and returns only the unique values. The COUNT function then counts the number of distinct values.
- Can we use SQL COUNT with DISTINCT on multiple columns?
Yes, SQL COUNT with DISTINCT can be used on multiple columns. In this case, the COUNT function will count the unique combinations of values in the specified columns.
- In which scenarios SQL COUNT with DISTINCT can be useful?
SQL COUNT with DISTINCT can be useful in many scenarios, such as counting the number of unique customers, products, or orders in a database. It's also a great way to get more precise results from your queries, and to ensure that you're not double-counting or including duplicates in your analysis.
Tag
DistinctCountSQL