Open In App

SQL SELECT COUNT()

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the world of SQL(Structured Query Language), the ‘SELECT’ statement is a fundamental and powerful tool that allows you to retrieve and manipulate data stored in a database. In this article, we see what is SELECT in SQL, how the Count function works with the select command, the different arguments of the Select statement, the syntax of the Select statement with the Count function, and the examples on different conditions.

SELECT

In SQL ‘Select’ is a very important statement that is commonly used in every SQL command. Select statement of SQL is used for fetching records from a table based on condition. Select statement allows you to specify which column you want to retrieve from the table. Using select statements you can also apply filters using different clauses like where, Group By, Having, etc.

Syntax:

SELECT <COLUMNS_NAME> FROM <TABLE_NAME>

WHERE <CONDITIONS>;

SELECT COUNT

In SQL Count( ) is an aggregate function that counts the number of rows available in a table or the number of rows that match condition criteria.

Syntax:

SELECT COUNT(coumn_name) FROM <TABLE_NAME>

WHERE <CONDITION>;

  • COUNT(column_name): This part specifies the column that you want to count.
  • FROM <TABLE_NAME>: This part specifies the table from which you want to retrieve the column data.
  • WHERE <CONDITION>: This part is optional but if you want to apply filter data then you use this. This part applies the condition while retrieving the data.

Examples of Select Count

Table Employee

Emp_id

Emp_Name

Phone_no

Email

Salary

100

Ram

000000

ram@gmail,co

10000

101

Shyam

11111111

shyam@gmail.com

12000

102

Mohan

22222

10000

103

Sohan

sohan@gmail.com

15000

104

Rakesh

44444

18000

105

Suresh

55555

16000

106

Mukesh

mulesk@gmail.com

17000

107

mina

77777

20000

Example 1: Number of Rows Available

SELECT COUNT(Emp_id) FROM EMPLOYEE

In this example, we apply the count function on the Emp_id column that returns the number of rows that are not null

Output:

COUNT(Emp_id)

8

Example 2: Use, of AS keyword with Count

SELECT COUNT (Emp_id) AS Number_of_Employee FROM EMPLOYEE

In this example, we use the ‘AS’ keyword that changes the header name of the column of the output table.

Number_of_Employee

8

Example 3: Handling the null values

SELECT COUNT(Phone) AS Number_Available FROM EMPLOYEE

In this example we get the number of rows that are not null will return.

Output:

Number_Available

6

Example 4: Count with Condition

SELECT COUNT(Salary) AS SALARY FROM EMPLOYEE

WHERE SALARY >=15000

In this example, we have the number of employees whose salary is more than and equal to 15000

Output:

Salary

5

Example 5: is Count with *

SELECT COUNT(*) FROM EMPLOYEE

In this example, we use ‘*’ in the place of column_name This gives all the number of rows if it is null or not null.

Output:

COUNT(*)

8

Example 6:Count with Distinct

SELECT COUNT(DISTINCT Salary) AS Distinct_Salary FROM EMPLOYEE

In this example, we use a Distinct keyword that does not count the duplicate rows in a column.

Output:

Distinct_Salary

7

Conclusion

The SELECT statement in SQL is a flexible and essential tool for retrieving and manipulating data from the database. When we use the COUNT() function with Select, it becomes more powerful, allowing you to perform data analysis and obtain valuable required data. The COUNT() function can also handle null values while counting rows and finding distinct values from the column. After reading this article you can use the COUNT() function easly and retrive valuable data from a table.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads