Introduction
The GROUP BY clause helps you organize data into groups so you can summarize or count things easily.
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
SELECT department, COUNT(*) FROM employees GROUP BY department;
SELECT product_id, SUM(quantity) FROM sales GROUP BY product_id;
SELECT city, AVG(temperature) FROM weather GROUP BY city;
CREATE TABLE sales ( id INT, product VARCHAR(20), quantity INT ); INSERT INTO sales (id, product, quantity) VALUES (1, 'Apple', 10), (2, 'Banana', 5), (3, 'Apple', 7), (4, 'Banana', 3), (5, 'Cherry', 8); SELECT product, SUM(quantity) AS total_quantity FROM sales GROUP BY product;
GROUP BY clause in a MySQL query?department in a MySQL query?sales with columns region and amount, what will the following query return?SELECT region, SUM(amount) FROM sales GROUP BY region;
SELECT department, COUNT(employee_id) FROM employees WHERE salary > 50000 GROUP BY employee_id;
products table with columns category, price, and stock. Which query correctly finds the average price and total stock for each category?Pass the test and this pattern moves unknown