SQL COUNT Vs COUNT DISTINCT Function
SQL Server has two aggregate functions: Count
and Count Distinct
.
We cover many sorts of functions in aggregates, such as count
,max
, avg
, min
, and sum
.
The Count function, which is used to count the number of rows in a database table, is discussed in this article.
The SELECT
query's DISTINCT
keyword combined with the COUNT function reveals the number of unique data in the table's field.
SQL COUNT Function
COUNT() is a SQL function that returns the number of records in the output table. The SELECT
statement in SQL is used with this function.
The COUNT function returns the total number of rows returned by the SELECT
command, including NULL
and duplicate entries.
When you combine a where condition with the COUNT function in a select statement, the COUNT() function is utilised to count the number of rows.
The number of rows from the table that satisfy the where condition will be returned. The COUNT() function will not count NULL
values.
Example 1:
We've used the following two SQL examples in this article to demonstrate how to use the Count function in a query:
We have a table called Book with 5 columns in this example:
id | name | price | year | domain |
---|---|---|---|---|
1 | MySql Query Performance Tuning | 100 | 2020 | Administration |
2 | SQL For Microsoft Access | 75 | 2017 | Optimization |
3 | Pro MySql Administration | 170 | 2017 | Performance |
4 | Troubleshooting MYSQL | 71.87 | 2016 | Database |
Let's say you want to count how many different bike colours there are in Book Table. You must construct the following SQL statement for this operation:
SELECT COUNT (Domain) AS Department FROM BookAdvance ;
Following the COUNT keyword, we must specify the column name as well as the name of the table on which the Count function will be run.
Output:
department |
---|
4 |
Because the Bike Color column does not contain any NULL
values, the result of this query is six.
Example 2:
For the SQL COUNT function examples, we'll use the tables and data listed below.
Without any NULL
or DISTINCT
tests, we'll use COUNT to return the number of rows.
We discuss the Student table and count the number of values
id | name | gender | sub1 | sub2 | sub3 |
---|---|---|---|---|---|
1 | Ranjani Mai | Male | 60 | 67 | 57 |
2 | Bala Murugan | Female | 9 | 48 | 44 |
3 | Pandurengan | Male | 72 | 33 | 99 |
4 | Hari Krishnan | Female | 33 | 12 | 77 |
SELECT COUNT(*) As Total
FROM Student;
Output:
total |
---|
4 |
We'll just retrieve the number of rows returned by the select statement with the above query.
SQL COUNT DISTINCT Fuction
The SELECT
query's DISTINCT
keyword combined with the COUNT function reveals the number of unique data in the table's field.
We used the COUNT function with an expression in the previous examples.
We can also use the phrase in conjunction with the DISTINCT
command to get all of the NON NULL
values, which are also UNIQUE
.
All duplicate values are removed before the function count is applied, or if you want to count the number of unique rows in the database, you can use the DISTINCT
keyword.
Rows with null values for the column are not counted in this version of Count.
Example 1:
The execution of the Count Function with Distinct
keyword is demonstrated in the following two SQL examples:
The Country table is created using the following query, which has four fields:
The following SQL statement from the Country dataset counts the unique values of the Continent column:
id | code | name | continent |
---|---|---|---|
1 | KGZ | Kyrgyzstan | Asia |
2 | DNK | Denmark | Europe |
3 | LKA | Sri Lanka | Asia |
4 | SVN | Slovenia | Europe |
5 | YUG | Yugoslavia | Europe |
6 | VIR | Virgin Islands, U.S. | North America |
SELECT COUNT (DISTINCT (Continent) )AS Landmass
FROM Country ;
Output:
landmass |
---|
3 |
SQL Differnce Between COUNT And COUNT DISTINCT Function
There 5 contrast in count vs count distinct aggregate functions:-
COUNT | COUNT DISTICT |
---|---|
When counting, COUNT(column name) will contain duplicate values. | Only distinct (unique) rows in the defined column will be counted with COUNT (DISTINCT column name). |
When you combine a where condition with the Count function in a select statement, the COUNT() function is utilised to count the number of rows. | If you use the DISTINCT keyword, all duplicate values are removed before using the function count, or if you wish to count the number of unique rows in the table. |
The Count function does not count NULL values. | The Count DISTINCT function will ignore NULL values. |
For each GROUP BY result, the COUNT function counts the number of records with non-NULL values in a field. |
For each GROUP BY result, the COUNT DISTINCT function reports the number of unique |
Counts the number of rows with non-NULL expr values. | Counts the number of rows with non-NULL expr values. |