Updated March 8, 2023
Introduction to SQL COUNT
COUNT() function in standard query language (SQL) is an aggregate function that returns the total number of records returned by a SELECT query based on the specified conditions. COUNT(*) function counts all the records. COUNT() function counts only non-NULL records. COUNT(), when used with the DISTINCT keyword, returns the count of only unique records in the result set of the SELECT statement. The function returns 0 if there is no matching record.
Syntax and Parameters of SQL COUNT
The basic syntax used for using the COUNT function in SQL is as follows:
SELECT COUNT(expression)
FROM table_name;
The syntax for writing COUNT function with a DISTINCT keyword.
SELECT COUNT(DISTINCT column_name)
FROM table_name;
The syntax for writing COUNT function in GROUPBY clause.
SELECT count(column_name1), column_name2
FROM table_name
GROUP BY column_name2;
The parameters used in the above-mentioned syntax are as follows:
- expression: The expression can be ‘*’, column_name or 1. ‘*’ and 1 in COUNT function counts all the rows in the given table, whereas column_name counts only non-null values.
- column_name: The column_name is the field names whose values will be counted.
- table_name: The database table from which these columns are being fetched.
- column_name1: The column on which counting will be performed.
- column_name2: The column on which grouping will be performed and then counting will be done group-wise for column_name1.
Here we have used the minimum possible clauses. You may add WHERE, HAVING, ORDER BY etc., to these syntaxes based on your requirement.
Examples of SQL COUNT
In order to illustrate the working of the COUNT() function in SQL, what could be better than trying a few examples on a dummy table.
Ergo, let us create a dummy table called “registrations”. Here is the create a statement for creating the table.
Code:
CREATE TABLE registrations (
regis_id INT NOT NULL,
username VARCHAR(255),
city VARCHAR(255),
date_of_birth DATE,
date_of_regis DATE
);
Having successfully created the registrations table.
Let’s insert a few records in it to work with.
Code:
INSERT INTO registrations(
regis_id, username, city, date_of_birth, date_of_regis)
VALUES (1,'Mohit Kumar','New Delhi','2000-01-01','2020-06-05'),
(2,'Reshma Khan','New Delhi','1998-11-21','2020-06-15'),
(3,'Biju Mathews','Bangalore','2000-01-15','2020-05-25'),
(4,'Samantha','Bangalore','1999-12-06','2020-05-01'),
(5, NULL,'Bangalore','2000-03-04','2020-05-15'),
(6,'Varun Reddy','Hyderabad','1999-01-01','2020-06-25'),
(7,'Mohini Gupta','New Delhi','2000-01-11','2020-06-05'),
(8,'Mohit Sharma','New Delhi',NULL,NULL);
The query returned successfully. The data in the registrations table looks something as follows:
Code:
SELECT * FROM registrations;
Output:
Now we are all set to do a few examples based on the COUNT() function.
COUNT (*) and COUNT(1) functions.
Example #1
Find the total number of rows/records in the registrations table.
Code:
SELECT COUNT(*)
FROM registrations;
Output:
Code:
SELECT COUNT(1)
FROM registrations;
Output:
Both the queries return the same output and serve the same purpose, then what is the difference between them? The difference is count(1) performs better than count(*). It returns queries quicker than the latter.
COUNT(column_name) and COUNT(DISTINCT column_name).
Example #2
Find the total number of usernames in the registrations table.
Code:
SELECT COUNT(username)
FROM registrations;
Output:
You must be wondering; the select query might have returned 8 records, then why does COUNT() function return 7? This is because the count() function does not count NULL values, and one of the usernames is a NULL value.
Example #3
Find the total number of regis_ids in the registrations table.
Code:
SELECT COUNT(regis_id)
FROM registrations;
Output:
Here, we did not have any NULL value, and hence the function returned 8.
Example #4
Find the total number of cities in the registrations table.
Code:
SELECT COUNT(city) as "No. of cities"
FROM registrations;
Output:
Example #5
Find the total number of unique cities in the registrations table.
Code:
SELECT COUNT( DISTINCT city) as "No. distinct of cities"
FROM registrations;
Output:
COUNT() with GROUP BY clause.
Example #6
Find the total number of registrations grouped together on a monthly basis.
Code:
SELECT date_part('Month',date_of_regis) as "Month",
count(regis_id) as "Total no. of registrations"
FROM registrations
GROUP BY date_part('Month',date_of_regis)
ORDER BY 1;
Output:
Example #7
Find the total number of registrations made from different cities.
Code:
SELECT city,
count(regis_id) as "Total no. of registrations"
FROM registrations
GROUP BY city
ORDER BY city;
Output:
COUNT() with HAVING clause.
Example #8
Find the total number of registrations grouped together by year of birth of the users having more than 2 registrations.
Code:
SELECT DATE_PART('Year', Date_of_birth) as "Birth Year",
count(regis_id) as "No. of registration"
FROM registrations
GROUP BY DATE_PART('Year', Date_of_birth)
HAVING COUNT(regis_id) >= 2;
Output:
Example #9
Find the total number of registrations grouped together by city having registrations more than 1 but less than 4.
Code:
SELECT city,
count(regis_id) as "Total no. of registrations"
FROM registrations
GROUP BY city
HAVING count(regis_id) BETWEEN 1 AND 3;
Output:
COUNT() with ORDER BY clause.
Example #10
Find the total number of users grouped together by date of registration. Order the records fetched according to the total number of usernames.
Code:
SELECT date_of_regis, count(username)
FROM registrations
GROUP BY date_of_regis
ORDER BY count(username);
Output:
Advantages of SQL COUNT
Given below are the advantages mentioned:
- COUNT() is a statistical function that helps to count the number of records in the result set of a SELECT query.
- COUNT(), when used with DISTINCT, helps to count the number of unique records in the table.
- COUNT(), when used as an aggregate function in the GROUP BY clause, helps in finding the number of records in each group.
- COUNT(), when used with the HAVING clause, helps to filter records based on the number of records.
Conclusion
COUNT() is an aggregate function in SQL that returns the total number of records fetched by a SELECT statement based on the specified conditions in the WHERE, GROUP BY or HAVING clauses.
Recommended Articles
We hope that this EDUCBA information on “SQL COUNT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.