COUNT
COUNT(expression)
Section titled “COUNT(expression)”- expression: It could either be a specific string, the name of a column for which the total number of non-NULL values is to be returned, or an asterisk (*) indicating that the function should count all rows irrespective of the contents. The COUNT() function returns the number of rows that match a specified criterion.
Example
Section titled “Example”SELECT COUNT(EmployeeID)FROM Employees;Output
Section titled “Output”| COUNT(EmployeeID) ||-------------------|| 50 |Explanation
Section titled “Explanation”The above SQL statement counts the total number of EmployeeID in the Employees table providing the total number of employees currently available in the database.
COUNT(expression)
Section titled “COUNT(expression)”- expression: Specifies the column or set of columns that will be counted. The expression can be a column name, an arithmetic operation, or a star (*) to count the total number of rows.
- distinct: If supplied, COUNT will return the number of distinct values of the specified expression, eliminating duplicates from the count.
- null: By default, COUNT excludes NULL values. But when used with specific functions like COUNT(*), it also includes the NULL values in the count.
Example
Section titled “Example”SELECT COUNT(*) FROM employees;Output
Section titled “Output” count------- 100Explanation
Section titled “Explanation”The COUNT(*) function has been used to return the total number of rows in the employees table.
COUNT( { [ ALL ] expression } | * )
Section titled “COUNT( { [ ALL ] expression } | * )”- all: This clause counts all instances of the provided expression, including duplicates.
- expression: A specific column or set of columns to count values from.
- *: An operator used to count all rows, irrespective of any specified column or condition.
Example
Section titled “Example”SELECT COUNT(*) AS TotalRecords FROM Customers;Output
Section titled “Output”TotalRecords----------------500Explanation
Section titled “Explanation”The COUNT(*) function is used to return the total number of records in the Customers table. The AS keyword is used to rename the column “TotalRecords” for readability purposes in the results.
COUNT([DISTINCT] expr)
Section titled “COUNT([DISTINCT] expr)”- distinct: This is an optional parameter. If included, COUNT will only consider distinct values of expr. It effectively eliminates duplicate values before performing the count operation.
- expr: This parameter represents the expression that will be counted by the COUNT function. It can be a column or a set of columns.
Example
Section titled “Example”SELECT COUNT(emp_id)FROM employees;Output
Section titled “Output”COUNT(emp_id)------------- 100Explanation
Section titled “Explanation”The SQL COUNT function is used to count the number of rows in a database table. In this example, COUNT(emp_id) returns the total number of employee IDs in the employees table.
COUNT(expression)
Section titled “COUNT(expression)”- expression: Refers to the column or set of columns that are being counted. This cannot be an arbitrary expression, but must refer directly to the data available within a table. The expression can include table columns, constants, literal strings, or functions.
Example
Section titled “Example”SELECT COUNT(*) FROM Employees;Output
Section titled “Output”7Explanation
Section titled “Explanation”In this code, COUNT(*) is used to count the total number of records in the ‘Employees’ table. The output ‘7’ means that there are 7 records in the table.