SQL Count Function – How To Use Count in SQL

Summary: This tutorial explains how to use the SQL COUNT function with syntax, examples, and also you will learn how to get the number of items in a set with the function count in SQL.

Introduction to SQL COUNT function

The SQL COUNT function returns the number of rows in a table satisfying the criteria described in the WHERE clause. It sets the non NULL column values or the number of rows. COUNT() returns zero if there were no matching rows. You’ll use the SQL server COUNT function in the SQL SELECT count statement such as to get the no. of employees, the no. of employees in every department, and the no. of employees who hold a particular job, etc.

.
Note: In SQL Server COUNT function returns the count of an expression.
The SQL COUNT function returns the number of records returned by a select query.
NULL values are not counted.

Syntax:

The following illustrates the syntax of the COUNT function:

COUNT([ALL | DISTINCT] expression);

Therefore, the above general syntax is SQL 2003 ANSI standard syntax. this helps us to understand the way COUNT() function is used. but you can see that different database vendors may have different ways of applying the COUNT() function.
The result of the COUNT function in SQL depends on the argument that you pass to it.

  • Overall, you can use * or ALL or DISTINCT or some expression along with COUNT to COUNT the number of rows w.r.t. some condition or all of the rows, depending upon the arguments you are using along with the COUNT() function.
  • By default, the function COUNT in SQL uses the ALL keyword whether you specify it or not. The means of ALL keyword is that all items in the group are considered including the duplicate values. For example, if you have a group (1, 4, 5, 5, 6, 6, 7) and apply the COUNT function, the result is 7.
  • Therefore, If you specify the DISTINCT keyword explicitly, only unique non-null values are considered. The COUNT function returns 5 if you apply it to the group (1, 4, 5, 5, 6, 6, 7).

Furthermore, Another form of the COUNT function in SQL that accepts an asterisk (*) as the argument is as follows:

COUNT(*)

DBMS Support: SQL COUNT Function

DBMS Command
PostgreSQL Supported
SQL Server Supported
MySQL Supported
Oracle Supported
.
Note: The COUNT(*) function returns the number of rows in a table including the rows that contain the NULL values. Therefore, you can see that PostgreSQL, MySQL, and Microsoft SQL Server follows the same syntax as given above. But DB2 and Oracle differs slightly.

DB2 and Oracle Syntax:

COUNT ({*|[DISTINCT] expression}) OVER (window_clause)
Parameters:
Name Description
ALL ALL returns the number of non NULL values. Applies to all values
DISTINCT Ignored the duplicate values and COUNT returns a number of unique nonnull values.
expression An expression made up of a single constant, scalar function, variable, or column name and can also be the pieces of a SQL query that compare values against other values. Expression of any type except image or text. The Aggregate functions and subqueries are not permitted.
* COUNTs all the rows in the target table whether or not they include NULLs.

SQL COUNT Function Examples

There is the given below the following example, an asterisk character ( * ) is used followed by the COUNT() SQL means which indicates all the rows of the table even if there is any NULL value.
Let’s see a few examples of how the COUNT function works. We will use the employees table in the sample database for demonstration purposes.

Sample Table: Orders

Employee_Id First_Name Last_Name Hire_Date Job_Id Salary Manager_Id Department_Id
1 William Josiah 2015-05-02 2 35000 4 7
2 James Hudson 2015-05-04 4 30000 3 6
3 Michael Nolan 2015-05-07 2 25000 7 7
4 Alexander Landon 2015-05-11 7 32000 8 3
5 Daniel Jeremiah 2015-06-02 2 22000 6 4
6 Gabriel Roman 2015-06-09 2 37000 5 7
7 Asher Kayden 2015-06-12 9 23000 6 8
8 Dylan Santiago 2015-06-17 10 28000 10 11
9 Thomas sawyer 2015-06-19 2 27000 9 7
10 Benjamin Hunter 2015-06-21 11 28000 13 12

SQL COUNT(*) Example

In this example, use the COUNT(*) function, to get the number of rows in the employees table, as follows:

SELECT 
    COUNT(*)
FROM
    Employees;

10

Similarly, to find the number of employees whose job id is 2, therefore, you use the following statement:

SELECT 
    COUNT(*)
FROM
    Employees
WHERE
    Job_Id = 2;

5

Furthermore, let’s find how many employees who work in the department id 7, you need to add the SQL WHERE clause to the query as follows:

SELECT 
    COUNT(*)
FROM
    Employees
WHERE
    Department_Id = 7;

4

SQL COUNT With GROUP BY Clause Example

In this example Group BY Clause, we find the number of employees per department, here we use the COUNT with GROUP BY clause as follows:

SELECT
	Department_Id,
	COUNT(*)
FROM
	Employees
GROUP BY
	Department_Id;


SQL COUNT GROUP BY OUTPUT