SQL -Structured Query Language - Sql Select - Select - Count Tutorial
To retrieve a number of the record of a specific column from the existing created table. Then we can use a ' SELECT ' statement with COUNT() function.
Without condition
Syntax-
The basic syntax of the SELECT statement with COUNT() function is given below –
SELECT COUNT(column_name) FROM table_name;
For example-
- Using *
SELECT COUNT(*) FROM employees;
Here it will retrieve the count of total no. of record from the employees' table.
- Using specific column name
SELECT COUNT(phone_number) FROM employees;
Here it will retrieve the count of total no. of phone_number from the employees' table, but it will not calculate phone_number having null field.
We can use whereas Condition / Clause in a select statement such as WHERE, GROUP BY, HAVING, ORDER BY, ETC.
With condition (USING WHERE)
Syntax-
The basic syntax of the ‘ SELECT ‘ statement with COUNT() function using WHERE condition is given below –
SELECT COUNT(numeric_column_name) FROM table_name WHERE [CONDITION] ;
For example-
- Using *
SELECT COUNT(*) FROM employees WHERE employee_id>150;
Here it will retrieve the count of total no. of record from the employees' table whose employee_id is greater than 150.
- Using specific column name
SELECT COUNT(phone_number) FROM employees WHERE employee_id>150;
Here it will retrieve the count of total no. of phone_number from employees table whose employee_id is greater than 150., but it will not calculate name having null field.