COUNT

COUNT is a function in SQL that returns the number of rows that matches a specified criteria. It can be used with the SELECT statement.

COUNT(expression)

  • expression: It could either be a specific string, the name of a column for which the total number of non-NULL values is to be returned, or an asterisk (*) indicating that the function should count all rows irrespective of the contents. The COUNT() function returns the number of rows that match a specified criterion.

Example

SELECT COUNT(EmployeeID)
FROM Employees;

Output

| COUNT(EmployeeID) |
|-------------------|
| 50 |

Explanation

The above SQL statement counts the total number of EmployeeID in the Employees table providing the total number of employees currently available in the database.

COUNT(expression)

  • expression: Specifies the column or set of columns that will be counted. The expression can be a column name, an arithmetic operation, or a star (*) to count the total number of rows.
  • distinct: If supplied, COUNT will return the number of distinct values of the specified expression, eliminating duplicates from the count.
  • null: By default, COUNT excludes NULL values. But when used with specific functions like COUNT(*), it also includes the NULL values in the count.

Example

SELECT COUNT(*) FROM employees;

Output

count
-------
100

Explanation

The COUNT(*) function has been used to return the total number of rows in the employees table.

COUNT( { [ ALL ] expression } | * )

  • all: This clause counts all instances of the provided expression, including duplicates.
  • expression: A specific column or set of columns to count values from.
  • *: An operator used to count all rows, irrespective of any specified column or condition.

Example

SELECT COUNT(*) AS TotalRecords FROM Customers;

Output

TotalRecords
----------------
500

Explanation

The COUNT(*) function is used to return the total number of records in the Customers table. The AS keyword is used to rename the column “TotalRecords” for readability purposes in the results.

COUNT([DISTINCT] expr)

  • distinct: This is an optional parameter. If included, COUNT will only consider distinct values of expr. It effectively eliminates duplicate values before performing the count operation.
  • expr: This parameter represents the expression that will be counted by the COUNT function. It can be a column or a set of columns.

Example

SELECT COUNT(emp_id)
FROM employees;

Output

COUNT(emp_id)
-------------
100

Explanation

The SQL COUNT function is used to count the number of rows in a database table. In this example, COUNT(emp_id) returns the total number of employee IDs in the employees table.

COUNT(expression)

  • expression: Refers to the column or set of columns that are being counted. This cannot be an arbitrary expression, but must refer directly to the data available within a table. The expression can include table columns, constants, literal strings, or functions.

Example

SELECT COUNT(*) FROM Employees;

Output

7

Explanation

In this code, COUNT(*) is used to count the total number of records in the ‘Employees’ table. The output ‘7’ means that there are 7 records in the table.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.