Aggregation functions perform calculations on sets of rows and return a single summary value. Theyβre often used with GROUP BY
to summarize data by categories.
Function | Description | Example |
---|---|---|
COUNT() |
Count rows or non-NULL values | COUNT(*) , COUNT(column) |
SUM() |
Sum of numeric column | SUM(amount) |
AVG() |
Average of numeric column | AVG(score) |
MIN() |
Minimum value | MIN(price) |
MAX() |
Maximum value | MAX(date) |
GROUP_CONCAT() |
Concatenate values from a group | GROUP_CONCAT(name) |
COUNT(DISTINCT) |
Count unique values | COUNT(DISTINCT user_id) |
SELECT COUNT(*) AS total_orders, SUM(amount) AS total_amount, AVG(amount) AS avg_amount, MIN(amount) AS smallest_order, MAX(amount) AS largest_order FROM orders;
SELECT customer_id, COUNT(*) AS orders_count, SUM(amount) AS total_spent, AVG(amount) AS avg_spent FROM orders GROUP BY customer_id ORDER BY total_spent DESC;
SELECT department, COUNT(*) AS emp_count, AVG(salary) AS avg_salary FROM employees GROUP BY department HAVING AVG(salary) > 60000;
COUNT(DISTINCT ...)
to count unique entries.GROUP BY
groups non-aggregated columns only.HAVING
for post-aggregation filtering, and WHERE
for pre-aggregation.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!