Other related articles:

Recently viewed articles:

COUNT (SQL)

COUNT(SQL) function is used to count the number of records in the results. It’s used in the SELECT statement along with the column list. Inside the brackets, insert the name of the column you want counted.

The value returned in the results set is the number of non-NULL values in that column. Alternatively, you can insert an asterisk (*), in which case all columns for all records in the results set are counted regardless of whether the value is NULL or not.

Example:

The following COUNT SQL function counts total number of records in employee table.

SELECT COUNT(*) AS number_of_records FROM employee;
Output:
COUNT SQL 1

Example 2:

This example includes more than one COUNT SQL function in the SELECT statement.

SELECT COUNT(first_name), COUNT(last_name)
FROM employee;

Output:
COUNT SQL 2
Note: The count value for first_name and last_name might be different because it excludes NULL values.

Example 3:

The following query COUNT SQL function to count number of employees in each department

SELECT dept_no, COUNT(*) number_of_employees
FROM employee
GROUP BY dept_no;

Output:
COUNT SQL 3