Javatpoint Logo
Javatpoint Logo

COUNT Function in SQL

The COUNT is an aggregate function in SQL which returns the total number of rows from the table.

Syntax of COUNT Function

In the Structured Query Language, we use the COUNT function with the columns of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the COUNT function.

Examples of COUNT function

Here, we will create the new table through which we will perform the COUNT function with the columns of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:


Product_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
104 P1 10.250 945 NULL 2022-04-30 NULL
202 P4 15.500 45 75 2022-01-28 5
103 P2 18.250 25 NULL 2022-02-18 4
111 P7 25.250 5 15 2021-12-25 9
210 P6 15.500 50 70 2021-10-15 NULL
212 P8 19.750 110 250 2022-01-28 4
112 P10 10.250 550 835 2022-04-11 NULL

Query 1: The following SELECT query uses the COUNT function with the Product_Quantity column of the above Product_Details table:

Output:

Total_Number_ofproduct
7

Query 2: The following SELECT query uses the COUNT function with the Selling_Price column of the above Product_Details table:

This query shows the five as result because this column contains two NULL values.

Output:

Total_Number_ofsellingpriceproducts
5

Query 3: The following SELECT query uses the COUNT function with the Product_Rating column of the above Product_Details table:

This query shows the four as result because this column contains three NULL values.

Output:

Total_Number_ofproductrating
4

Count Function with WHERE clause

We can also use the WHERE clause with the COUNT function which returns the number of rows according to the filtered rows.

The syntax for using the COUNT function with the WHERE clause is as follows:

Query 1: The following SELECT query uses the COUNT function with WHERE clause in the above Product_Details table:

This query counts those products from above table whose product id is greater than 200.

Output:

Total_Number_ofproduct>200
3

Query 2: The following SELECT query uses the COUNT function with WHERE clause in the above Product_Details table:

This query counts those products which are release on 2022-01-28.

Output:

Total_Number_ofdate
2

Count Function with DISTINCT clause

We can also use the DISTINCT clause with the COUNT function which counts only distinct values of the column from the table.

The syntax for using the COUNT function with the DISTINCT clause is as follows:

Query 1: The following SELECT query uses the COUNT function with the DISTINCT clause in the above Product_Details table:

This query counts the product according to the distinct values of product_quantity column.

Output:

Total_Number_ofproduct
5

Query 3: The following SELECT query uses the COUNT function with the DISTINCT clause of the above Product_Details table:

This query shows the three as result because this column contains three NULL values and two same values.

Output:

Total_Number_ofdistinctproductrating
3

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA