EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL COUNT
 

SQL COUNT

Updated March 8, 2023

SQL COUNT

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

SQL COUNT 1

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:

SQL COUNT 2

Code:

SELECT COUNT(1)
FROM registrations;

Output:

SQL COUNT 3

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:

SQL COUNT 4

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:

total number of regis_ids

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:

SQL COUNT 6

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:

total number of unique cities

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:

SQL COUNT 8

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:

made from different cities.

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:

total number of registrations

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:

SQL COUNT 11

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:

SQL COUNT 12

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.

  1. SQL ORDER BY CASE
  2. SQL GROUP BY Month
  3. SQL GROUP BY Multiple Columns
  4. SQL NOT Operator

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW