SQL COUNT() Function - The Coding Shala

Home >> Learn SQL >> SQL COUNT() Function

SQL COUNT() Function

The SQL COUNT() function returns the number of rows(count of rows) which are returned by the SELECT statement.
SQL COUNT() Function - The Coding Shala

SQL COUNT() Syntax 

The basic syntax of SQL COUNT() is as follows - 

SELECT COUNT(column_name)  
 FROM table_name  
 WHERE condition; 

The following 'emp' is used for the examples - 

 emp_id     emp_name     city     dept_no     salary  
 1          Akshay        Pune      101       50000  
 3          Nikhil        Pune      101       51000  
 5          Mohit         Delhi     103       40000  
 6          Shubham       Surat     105       42000  
 7          Akash         Mumbai    106       45000

SQL COUNT() Examples 

The following SQL query will return the number of records present in the emp table using the COUNT() function. We can use column name with count() function like count(emp_name) or can use count(*) that count all the records that returned by the SELECT statement. 

 SQL >> select count(*)  
       from emp;  
 
Output >>   
 count(*)  
 5  

The following SQL query will count the employees who live in the city 'Pune' - 

 SQL >> select count(emp_name)  
        from emp  
        where city = 'Pune';  

 Output >>   
 count(emp_name)  
 2  



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala