Introduction
Aggregation helps us combine many rows of data into a single summary. It makes large data easier to understand by showing totals, averages, counts, and more.
Aggregation helps us combine many rows of data into a single summary. It makes large data easier to understand by showing totals, averages, counts, and more.
SELECT AGGREGATE_FUNCTION(column_name) FROM table_name;
SELECT COUNT(*) FROM customers;
SELECT AVG(price) FROM products;
SELECT category, SUM(quantity) FROM sales GROUP BY category;
This example creates a sales table, inserts some data, and then sums the quantity sold for each category.
CREATE TABLE sales ( id INT, category VARCHAR(20), quantity INT ); INSERT INTO sales VALUES (1, 'Books', 5), (2, 'Books', 3), (3, 'Toys', 7), (4, 'Toys', 2), (5, 'Clothes', 4); SELECT category, SUM(quantity) AS total_quantity FROM sales GROUP BY category;
Aggregation functions ignore NULL values except COUNT(*).
GROUP BY is used to group rows before aggregation.
Aggregation combines many rows into summary values.
Common functions: SUM, COUNT, AVG, MAX, MIN.
Use GROUP BY to summarize data by categories.
SUM and COUNT in SQL?employees?sales with columns region and amount, what will this query return?SELECT region, SUM(amount) FROM sales GROUP BY region;
SELECT department, COUNT(employee_id) FROM employees;
employees table. Which query correctly summarizes this data?Pass the test and this pattern moves unknown