Introduction
GROUP BY helps us organize data by one column so we can see summaries for each group.
Jump into concepts and practice - no test required
SELECT column_name, AGGREGATE_FUNCTION(column_name) FROM table_name GROUP BY column_name;
SELECT department, COUNT(*) FROM employees GROUP BY department;
SELECT category, SUM(price) FROM products GROUP BY category;
SELECT city, AVG(age) FROM customers GROUP BY city;
CREATE TABLE sales ( id INT, product VARCHAR(20), amount INT ); INSERT INTO sales VALUES (1, 'Apple', 10), (2, 'Banana', 5), (3, 'Apple', 15), (4, 'Banana', 7), (5, 'Cherry', 20); SELECT product, SUM(amount) AS total_amount FROM sales GROUP BY product;
GROUP BY clause do in an SQL query?department?sales with columns region and amount, what is the output of this query?SELECT region, SUM(amount) FROM sales GROUP BY region;
SELECT department, COUNT(*) FROM employees;
orders with columns customer_id, order_date, and total. You want to find the average order total per customer but only for customers who have placed more than 3 orders. Which query correctly achieves this?