# SQL COUNT() with distinct

## COUNT() function with distinct clause

SQL COUNT() function with DISTINCT clause eliminates the repetitive appearance of the same data. The DISTINCT can come only once in a given select statement.

**Syntax
:**

COUNT(DISTINCT expr,[expr...]) or SELECT COUNT(DISTINCT expression) FROM table_name WHERE condition;

Here, expression refers to the combination of columns or an expression that needs to be evaluated to obtain distinct values, and condition is an optional parameter.

**Example :**

To get unique number of rows from the 'orders' table with following conditions -

1. only unique cust_code will be counted,

2. result will appear with the heading "Number of employees",

the following SQL statement can be used :

```
-- Counting the number of distinct values in the 'cust_code' column of the 'orders' table
-- and aliasing the result as "Number of employees"
SELECT COUNT(DISTINCT cust_code) AS "Number of employees"
-- From the 'orders' table
FROM orders;
```

Sample table : orders

Output :

Number of employees ------------------- 25

**Visual Presentation: **

**Example :**

Sample table : product_mast

id|prod|price|year|

--+----+-----+----+

1|Pro1| 300|2018|

2|Pro2| 400|2019|

3|Pro1| 350|2018|

4|Pro3| 500|2019|

5|Pro2| 450|2018|

To count the number of distinct products sold in the year 2018, we can use the following SQL query:

```
-- Counting the number of distinct values in the 'prod' column
SELECT COUNT(DISTINCT prod)
-- From the 'product_mast' table
FROM product_mast
-- Filtering the results to include only rows where the 'year' is equal to 2018
WHERE year = 2018;
```

Output :

count --------- 2

This will return the result 2, as there are two distinct products (Pro1 and Pro2) sold in the year 2018.

**Example :**

Sample table : product_mast

id|prod|price|year|

--+----+-----+----+

1|Pro1| 300|2018|

2|Pro2| 400|2019|

3|Pro1| 350|2018|

4|Pro3| 500|2019|

5|Pro2| 450|2018|

If we want to count the number of distinct product and year combinations, we can use the following SQL query:

```
-- Counting the number of distinct combinations of 'prod' and 'year' in the 'product_mast' table
SELECT COUNT(DISTINCT (prod, year))
-- From the 'product_mast' table
FROM product_mast;
```

Output :

count --------- 4

This will return the result 4, as there are four distinct product and year combinations (Pro1-2018, Pro2-2019, Pro2-2018, and Pro3-2019) in the product_mast table.

**Example :**

Sample table : product_mast

id|prod|price|year|

--+----+-----+----+

1|Pro1| 300|2018|

2|Pro2| 400|2019|

3|Pro1| 350|2018|

4|Pro3| 500|2019|

5|Pro2| 450|2018|

To count the number of distinct products sold in the year 2018 with a price greater than 300, we can use the following SQL query:

```
-- Counting the number of distinct values in the 'prod' column
SELECT COUNT(DISTINCT prod)
-- From the 'product_mast' table
FROM product_mast
-- Filtering the results to include only rows where the 'year' is equal to 2018
-- and the 'price' is greater than 300
WHERE year = 2018 AND price > 300;
```

Output :

count --------- 2

This will return the result 2, as there are two distinct products (Pro1 and Pro2) sold in the year 2018 with a price greater than 300.

**Example :**

Sample table : product_mast

--+----+-----+----+

1|Pro1| 300|2018|

2|Pro2| 400|2019|

3|Pro1| 350|2018|

4|Pro3| 500|2019|

5|Pro2| 450|2018|

If we want to count the number of distinct product and year combinations with a price greater than 400, we can use the following SQL query:

```
-- Counting the number of distinct combinations of 'prod' and 'year'
SELECT COUNT(DISTINCT (prod, year))
-- From the 'product_mast' table
FROM product_mast
-- Filtering the results to include only rows where the 'price' is greater than 400
WHERE price > 400;
```

Output :

count --------- 2

This will return the result 2, as there is two distinct products and year combination (Pro3-2022, Pro2-2018) in the product_mast table with a price greater than 400.

## SQL COUNT( ) with All

In the following, we have discussed the usage of ALL clause with SQL COUNT() function to count only the non NULL value for the specified column within the argument. The difference between ‘*’(asterisk) and ALL are, '*' counts the NULL value also but ALL counts only NON NULL value.

**Example:**

To get data of number of valid 'grade' from the 'customer' table with the following condition -

1. every customer must be a valid grade,

the following SQL statement can be used :

```
-- Counting the total number of rows in the 'customer' table,
-- including duplicate values in the 'grade' column
SELECT COUNT(ALL grade)
-- From the 'customer' table
FROM customer;
```

Sample table: customer

Output :

COUNT(ALLGRADE) --------------- 25

Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

Here is a slide presentation of all aggregate functions.

**Previous:** COUNT Function

**Next:** COUNT with Group by

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join