SQL COUNT() Function


The SQL COUNT() is a function, and return total number of records or rows from query result.

The SQL COUNT() function will not count the NULL value records or rows.

The SQL COUNT() function is supports all types of column.

It can be used in SQL SELECT statement as well in SQL WHERE clause.

You can also search these topics using sql count function, sql average count, sql get row count, sql count rows in table.

SQL COUNT() Syntax

The below syntax is used to select specific column from the specific table.


SELECT COUNT(column_name1) FROM table_name;

The below syntax is used to select all column from the specific table.


SELECT COUNT(*) FROM table_name;

The SQL COUNT() function will not include or fetch NULL value records or rows.


Sample Database Table - Employee

ID EmpName Designation Dept JoinYear Salary
1 Geetha Relational DBMS Oracle 2013 8300
2 Padmavathi SQL Security MySQL 2014 15230.8
3 Harish Karthik Administrator C#.Net 2015 19430.9
4 Hari Krishnan Computer Science MySQL 2013 10610.1
5 Hanumanthan Database Developer PHP 2013 15020
6 Dharan Administrator PHP 2012 4940

SQL COUNT(column_name1) Example

The following SQL SELECT statement find the total number of values or records in the "Dept" column from the "Employee" table:


SELECT 
COUNT(Dept) As 'Total Values In Dept Column' 
FROM Employee;

The result of above query is:

Total Values In Dept Column
6

SQL COUNT(*) Example

The following SQL SELECT statement find the total number of records available for column "Dept" is "PHP" from the "Employee" table:


SELECT COUNT(*) As 'Total Records In PHP Dept Column' 
FROM Employee 
WHERE Dept = 'PHP';

The result of above query is:

Total Records In PHP Dept Column
2

SQL COUNT() Example - Using Group By Clause

The following SQL SELECT statement find the total number of employee or records by grouping "Dept" column from the "Employee" table:


SELECT 
Dept, COUNT(Dept) As 'Total Employee' 
FROM Employee 
GROUP BY Dept;

The result of above query is:

Dept Total Employee
C#.Net 1
MySQL 2
Oracle 1
PHP 2
You can also search these topics using how to use count in sql, sql server count distinct, select rowcount sql, sql order by count, sql table row count, sql server select count, sql count command, count sql example, count records sql, select count from table, sql count expression, sql count function examples, sql multiple counts.