Introduction
Aggregation helps us summarize many rows of data into useful information like totals or averages.
Jump into concepts and practice - no test required
Aggregation helps us summarize many rows of data into useful information like totals or averages.
SELECT AGGREGATE_FUNCTION(column_name) FROM table_name;
AGGREGATE_FUNCTION can be SUM, COUNT, AVG, MAX, MIN.
You can add GROUP BY to get summaries per group.
SELECT COUNT(*) FROM orders;
SELECT SUM(price) FROM sales;
SELECT AVG(score) FROM tests;
SELECT department, MAX(salary) FROM employees GROUP BY department;
This creates a sales table, adds some rows, then sums the amount sold per product.
CREATE TABLE sales (id INT, product VARCHAR(20), amount INT); INSERT INTO sales VALUES (1, 'apple', 10), (2, 'banana', 5), (3, 'apple', 15); SELECT product, SUM(amount) FROM sales GROUP BY product;
Aggregation reduces many rows into fewer summary rows.
Without aggregation, you see raw data; with it, you see meaningful summaries.
Aggregation helps summarize data with functions like SUM, COUNT, AVG.
It is useful to get totals, averages, counts, and extremes.
GROUP BY lets you summarize data per category.
SUM() or COUNT() in SQL?Orders with a column Amount?Sales with columns Region and Amount, what will this query return?SELECT Region, COUNT(*) FROM Sales GROUP BY Region;
SELECT Department, AVG(Salary) FROM Employees;
Sales table with columns Department and Amount. Which query correctly achieves this?