Examples of Select Count
Table Employee
Emp_id |
Emp_Name |
Phone_no |
|
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 |
SQL SELECT COUNT()
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.