MySQL COUNT() Function

The COUNT() function in MySQL counts the number of rows in a table or the number of non-null values in a column. It's a fundamental aggregate function used for summarizing data.



COUNT(): Definition and Usage

COUNT() is very useful for getting totals and understanding the size of your datasets. It's often used in conjunction with GROUP BY to count items within different categories. It's important to note that COUNT() ignores NULL values.

Syntax

Syntax

COUNT(expression)
      

Parameter Values

Parameter Description
expression A column name (to count non-null values in that column) or an asterisk (*) to count all rows, even those with NULL values in the specified column. This is required.

Example

Counting Products

This example counts the number of products in the 'Products' table (assuming a table named 'Products' exists with a 'ProductID' column). If there are any rows where 'ProductID' is NULL, those rows will not be included in the count.

Syntax

SELECT COUNT(ProductID) AS NumberOfProducts FROM Products;
      
Output

NumberOfProducts
----------------
(The number of non-NULL ProductIDs in the Products table will be displayed here.)
      

**Note:** The output will vary depending on the number of rows and the number of `NULL` values in the `ProductID` column of your `Products` table. To count all rows regardless of `NULL` values in `ProductID`, use `COUNT(*)` instead of `COUNT(ProductID)`.