The COUNT()
function returns the number of rows that match a specified condition.
Syntax:
SELECT COUNT(column_name)
FROM table_name;
COUNT()
: Counts non-NULL values in the specified column.The SUM()
function returns the total sum of a numeric column.
Syntax:
SELECT SUM(column_name)
FROM table_name;
SUM()
: Adds up all the values in the specified numeric column.
The AVG()
function calculates the average value of a numeric column.
Syntax:
SELECT AVG(column_name)
FROM table_name;
AVG()
: Returns the average (mean) value of the specified numeric column.
Let’s assume we have a table orders
with columns order_id
, amount
, and customer_id
. We want to calculate the total number of orders, total sales amount, and average order value.
SELECT
COUNT(order_id) AS total_orders,
SUM(amount) AS total_sales,
AVG(amount) AS average_order_value
FROM orders;
In this example:
COUNT(order_id)
counts the total number of orders.SUM(amount)
calculates the total sales amount.AVG(amount)
calculates the average order value.COUNT()
: Counts rows or non-NULL values.SUM()
: Adds up values in a numeric column.AVG()
: Calculates the average of a numeric column.The COUNT()
, SUM()
, and AVG()
functions in MySQL are powerful tools for analyzing and summarizing data. Whether you need to count records, calculate totals, or find averages, these functions make complex queries easier and more efficient.
.