📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials MySQL Aggregate Functions

Aggregate Functions

5 min read Quiz at the end
Use COUNT, SUM, AVG, MIN, MAX with GROUP BY and HAVING to aggregate and filter grouped data.

Aggregate Functions and GROUP BY

SELECT COUNT(*) FROM users;
SELECT COUNT(DISTINCT country) FROM users;
SELECT SUM(amount) FROM orders WHERE status = 'completed';
SELECT AVG(age) FROM users WHERE is_active = 1;
SELECT MIN(price), MAX(price) FROM products;

-- GROUP BY
SELECT country, COUNT(*) AS user_count
FROM users
GROUP BY country
ORDER BY user_count DESC;

-- GROUP BY with HAVING (filter after grouping)
SELECT user_id, COUNT(*) AS order_count
FROM orders
GROUP BY user_id
HAVING order_count >= 5;

-- GROUP_CONCAT
SELECT user_id, GROUP_CONCAT(tag ORDER BY tag SEPARATOR ', ') AS tags
FROM post_tags
GROUP BY user_id;
Topic Quiz · 1 questions

Test your understanding before moving on

1. Which function counts all rows including NULLs?
💡 COUNT(*) counts all rows including those with NULL values in any column.