COUNT
Overview
The COUNT()
function allows you to get the number of records that match a specific condition.
The input and return types we support can be seen in the table below.
Examples
In this example, we will use an orders table that stores details of the purchase transactions:
The above query will show the following table:
#Case 1: COUNT()
with a single expression
The following example will return the number of all orders in the orders table:
The final result will be as follows:
#Case 2: COUNT()
with a GROUP BY
clause
This example will combine the COUNT()
function and the GROUP BY
clause.
-
The
GROUP BY
clause groups the orders based on the customer’s name. -
The
COUNT()
function counts the orders for each customer.
It will display the output as shown below:
#Case 3: COUNT()
with a HAVING
clause
In this example, we combine the COUNT()
function and the HAVING
clause to apply a specific condition to find customers who have made more than two orders:
-
The
GROUP BY
clause groups the orders based on the customer’s name. -
The
HAVING
clause will filter only customers with more than two order IDs. -
The
COUNT()
function counts the orders for each customer.