SQL COUNT() Function


The COUNT returns the number of rows present in the table either based on some condition or without condition.


SQL COUNT(column_name) Syntax

The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:

SELECT COUNT(column_name) FROM table_name;

Example

student Table

stud_sidstud_namestud_fee
11211Ashwani400
11212Sachin200
11213Deepak600
11214Sanjay800
SELECT COUNT(stud_name) FROM student;
Output
4

SQL COUNT(*) Syntax

The COUNT(*) function returns the number of records in a table:

SELECT COUNT(*) FROM table_name;

Example

student Table

stud_sidstud_namestud_fee
11211Ashwani400
11212Sachin200
11213Deepak600
11214Sanjay800
SELECT COUNT(*) FROM student;
Output
12

SQL COUNT(DISTINCT column_name) Syntax

The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column.

The distinct keyword is used with Select statement to retrieve unique values from the table. Distinct removes all the duplicate records while retrieving from database.

SELECT COUNT(DISTINCT column_name) FROM table_name;

Example

student Table

stud_sidstud_namestud_fee
11211Ashwani400
11212Sachin200
11213Deepak400
11214Sanjay200
SELECT COUNT(DISTINCT stud_fee) FROM student;
Output
2

Share this article on