0
0
SQLquery~5 mins

GROUP BY multiple columns in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SQL clause GROUP BY do when used with multiple columns?
It groups rows that have the same values in all the specified columns, so you can perform aggregate functions on each unique combination of those columns.
Click to reveal answer
beginner
Write a simple example of a GROUP BY clause using two columns: city and department.
Example: <br>SELECT city, department, COUNT(*) FROM employees GROUP BY city, department;<br>This counts employees grouped by each city and department combination.
Click to reveal answer
intermediate
Why is it important to include all non-aggregated columns in the GROUP BY clause?
Because SQL requires that every column in the SELECT list that is not inside an aggregate function must be listed in the GROUP BY clause to know how to group the data.
Click to reveal answer
intermediate
What happens if you use GROUP BY on multiple columns but forget one column that appears in SELECT without aggregation?
The query will cause an error because SQL does not know how to group by the missing column, violating grouping rules.
Click to reveal answer
beginner
Can GROUP BY multiple columns be used to find unique combinations of those columns?
Yes, grouping by multiple columns returns one row per unique combination of those columns, which helps identify distinct groups.
Click to reveal answer
What does GROUP BY city, department do in a SQL query?
AGroups rows by city only
BGroups rows by department only
CGroups rows by unique pairs of city and department
DSorts rows by city and department
Which columns must appear in the GROUP BY clause?
AOnly columns with numeric data
BAll columns in SELECT that are not aggregated
COnly the first column in SELECT
DNo columns are required
What will happen if you omit a non-aggregated column from GROUP BY?
AQuery will return an error
BQuery will run but ignore that column
CQuery will group by all columns automatically
DQuery will return duplicate rows
Which aggregate function can be used with GROUP BY?
ANOW()
BCONCAT()
CSUBSTRING()
DCOUNT()
If you want to count employees per city and department, which query is correct?
ASELECT city, department, COUNT(*) FROM employees GROUP BY city, department;
BSELECT city, department, COUNT(*) FROM employees;
CSELECT city, COUNT(*) FROM employees GROUP BY department;
DSELECT COUNT(*), city, department FROM employees GROUP BY city;
Explain how GROUP BY with multiple columns works and why it is useful.
Think about grouping by city and department together.
You got /3 concepts.
    Describe what happens if you include a column in SELECT that is not aggregated and not in GROUP BY.
    Remember SQL grouping rules.
    You got /3 concepts.