0
0
MySQLquery~5 mins

GROUP BY with multiple columns in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the GROUP BY clause do in SQL?
It groups rows that have the same values in specified columns into summary rows, like grouping all sales by each product.
Click to reveal answer
beginner
How do you use GROUP BY with multiple columns?
You list the columns separated by commas after GROUP BY. This groups rows by unique combinations of those columns.
Click to reveal answer
intermediate
Why would you group by multiple columns instead of just one?
Grouping by multiple columns lets you see detailed summaries for each unique combination, like sales by product and region together.
Click to reveal answer
beginner
Example: What does this query do?<br>
SELECT city, department, COUNT(*) FROM employees GROUP BY city, department;
It counts how many employees work in each department within each city, grouping by both city and department.
Click to reveal answer
intermediate
Can you use ORDER BY with GROUP BY on multiple columns?
Yes, you can order the grouped results by one or more columns to sort the summary rows as you want.
Click to reveal answer
What happens if you use GROUP BY city, department in a query?
ARows are grouped by unique city and department pairs.
BRows are grouped only by city.
CRows are grouped only by department.
DRows are not grouped at all.
Which SQL clause is used to summarize data by groups?
AJOIN
BWHERE
CORDER BY
DGROUP BY
If you want to count employees per city and department, which is correct?
AGROUP BY city
BGROUP BY city, department
CGROUP BY department
DGROUP BY COUNT(*)
Can you use aggregate functions like COUNT() with GROUP BY multiple columns?
AAggregate functions are not related to GROUP BY.
BNo, aggregate functions only work without GROUP BY.
CYes, aggregate functions work on each group.
DOnly COUNT() works, others don't.
What does this query return?<br>
SELECT product, region, SUM(sales) FROM sales_data GROUP BY product, region;
ATotal sales per product and region combination.
BTotal sales per product only.
CTotal sales per region only.
DAll sales data without grouping.
Explain how GROUP BY with multiple columns works and why it is useful.
Think about grouping data like sorting mail by city and street together.
You got /3 concepts.
    Write a simple SQL query using GROUP BY with two columns and explain what it does.
    Use example like counting employees by city and department.
    You got /3 concepts.