Introduction
In this chapter, we learned how to use a SQL COUNT statement with various options.
SQL COUNT Statement
This statement is used to return the number of items found in a group Count operates like the COUNT_BIG function. These statements differ only in the data types of their return values. Count always returns an int data type value. COUNT_BIG always returns a bigint data type value.
The Count() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. Count() returns 0 if there were no matching rows.
Syntax
- SELECT COUNT(column_name)
- FROM table_name
- WHERE condition;
This example is the general SQL 2019 ANSI standard syntax. It helps us understand the way in which the SQL Count() Function is used. Different database vendors may have different ways of applying the Count() function.
Below, you can see that MySQL, PostgreSQL, and Microsoft SQL Server follow the same syntax as given above, but DB2 and oracle differ slightly.
Overall, you can use * or ALL or distinct or some expression along with the count, in order to count the number of rows for some condition or all of the rows, depending upon the arguments you are using along with the count() function.
Using Count and Distinct statement
This example returns the number of different titles that a Sample in OrderDetails can hold.
Syntax
- SELECT COUNT(DISTINCT OrderName )
- FROM OrderDetails
- GO
The Count statement * is used for Count(), all records ( rows ) are Counted if some content NULL but Count(column_name) does not Count a record if its field is NULL
Select Count statement (*)
This example returns the total number of Sample cycles EmployeeDetail
Syntax
- SELECT COUNT(*)
- FROM EmployeeDetail;
- GO
Example
Using Count( ) with column name
In this example, SQL Count() function excludes the NULL values for a specific column if specified the column as an argument in the parenthesis of the Count function.
Syntax
- SELECT COUNT(OrderId)
- FROM OrderDetails ;
Select Count (*) for multiple Tables
The following query counts the number of rows from two different tables (here we use EmployeeDetail and OrderDetails) using the Count(*) command.
Syntax
- SELECT(
- SELECT COUNT(*)
- FROM EmployeeDetail
- ) AS Total_Employees,
- (SELECT COUNT(*)
- FROM OrderDetails
- ) AS OrderName
- FROM OrderDetails
Example
Select Count(*) with other aggregates
This example shows that Count(*) works with other aggregate functions in the Select list. The example uses the Sample database.
Syntax
- SELECT COUNT(*),AVG(OrderId)
- FROM OrderDetails
- Where OrderId>5;
- GO
Example
Using the over a clause in the statement
This example uses the Min, Max, Avg, and Count functions with the Over clause, to return aggregated values for each department in the Sample database OrderDetails table.
Syntax
- SELECT DISTINCT OrderName
- , MIN(OrderId) OVER (PARTITION BY edh.OrderId) AS OrderDetails
- , MAX(EmpId) OVER (PARTITION BY edh.OrderId) AS EmployeeDetail
- , AVG( EmployeeID) OVER (PARTITION B edh.OrderId) class="string">"keyword">AS EmployeeDetails
- ,COUNT(OrderId) OVER (PARTITION BY edh.OrderId) class="keyword">AS OrderDetails
- FROM EmployeeDetail AS eph
- JOIN EmployeeDetails AS edh
- ON eph.EmpId = edh.EmployeeID
- JOIN OrderDetails AS d
- ON d.OrderId = edh.EmployeeID
- WHERE edh.EmployeeID IS
- ORDER BY EmployeeName;
Application of Count() function
In this section, we have discussed how to apply to count() withvarious SQL clauses for those applications, we have used Oracle 10g express edition.
Count with Distinct page discusses how to apply the count function with distinct and also discusses how to apply the count function with All clause. unlike using *,when all is used, Null values are not selected.
Count Having page discusses how to apply the count function with the Having clause and group by.
Count with Group by page discusses how to apply the Count function with Group By in ascending order and in descending order.
Summary
In this chapter, we learned how to use a SQL DATE TIME statement with various options.