Introduction
Grouping data by multiple columns helps you organize and summarize information based on more than one category at the same time.
SELECT column1, column2, aggregate_function(column3) FROM table_name GROUP BY column1, column2;
SELECT department, role, COUNT(*) FROM employees GROUP BY department, role;
SELECT city, product, SUM(sales) FROM sales_data GROUP BY city, product;
SELECT year, month, AVG(temperature) FROM weather GROUP BY year, month;
CREATE TABLE orders ( order_id INT, customer_id INT, product_category VARCHAR(50), order_amount DECIMAL(10,2) ); INSERT INTO orders VALUES (1, 101, 'Books', 15.99), (2, 102, 'Books', 25.50), (3, 101, 'Electronics', 199.99), (4, 103, 'Books', 8.99), (5, 102, 'Electronics', 99.99), (6, 101, 'Books', 12.00); SELECT customer_id, product_category, COUNT(*) AS order_count, SUM(order_amount) AS total_spent FROM orders GROUP BY customer_id, product_category ORDER BY customer_id, product_category;
GROUP BY clause with multiple columns do in a MySQL query?GROUP BY clause groups rows that have the same values in the specified columns together.city and department in MySQL?GROUP BY followed by column names separated by commas.sales with columns region, product, and amount, what will this query return?SELECT region, product, SUM(amount) FROM sales GROUP BY region, product;
region and product together.amount values, giving total sales per region-product pair.SELECT city, department, COUNT(*) FROM employees GROUP BY city;
city and department but groups only by city.transactions table with columns store_id, employee_id, and sales_amount. You want to find the total sales per store and employee but only for employees with total sales above 1000. Which query correctly achieves this?Pass the test and this pattern moves unknown