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.


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.


Search Keys

  • sql count function
  • sql count
  • select count
  • postgresql count
  • sql count where
  • sql server count
  • sql count rows
  • sql count if
  • count function
  • sql select count
  • record count sql
  • sql average count
  • sql group count
  • sql get row count
  • sql count group
  • sql count rows in table
  • sql row count

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 Rishi Keshan Information Tech Oracle 2015 19430.9
7 Keshavan Project Lead Oracle 2013 9560
8 Nirmala SQL Security Java 2013 22370.9
9 Varshini Kutty Database Security C#.Net 2015 9560
10 Chandra Programmer JQuery 2013 6830.3
11 Dharan Administrator PHP 2012 4940
12 Vidyavathi Mobile Database MySQL 2014 9560

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
12

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 2
Java 1
JQuery 1
MySQL 3
Oracle 3
PHP 2

Search Keys

  • sql count join
  • sql count column
  • 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
  • count unique sql
  • sql count command
  • count sql example
  • count records sql
  • select count from table
  • sql count expression
  • sql count all
  • sql count function examples
  • sql multiple counts