0
0
PostgreSQLquery~5 mins

GROUP BY single and multiple columns in PostgreSQL - 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 finding the total sales per product.
Click to reveal answer
beginner
How do you use GROUP BY with a single column?
You list one column after GROUP BY to group rows by that column's values. For example, GROUP BY department groups all rows by each department.
Click to reveal answer
intermediate
How does GROUP BY work with multiple columns?
You list multiple columns separated by commas after GROUP BY. This groups rows by unique combinations of those columns. For example, GROUP BY city, department groups rows by each city and department pair.
Click to reveal answer
beginner
Why do you often use aggregate functions with GROUP BY?
Because GROUP BY creates groups, aggregate functions like SUM(), COUNT(), or AVG() calculate summary values for each group.
Click to reveal answer
intermediate
What happens if you select columns not in the GROUP BY clause or aggregate functions?
PostgreSQL will give an error because every selected column must be either grouped or aggregated to avoid confusion about which row's value to show.
Click to reveal answer
What does GROUP BY department do?
AGroups rows by each unique department
BSorts rows by department
CDeletes duplicate departments
DFilters rows with department
Which query groups by both city and department?
AGROUP BY city; GROUP BY department
BGROUP BY city OR department
CGROUP BY city, department
DGROUP BY city AND department
Which aggregate function counts rows in each group?
ACOUNT()
BMAX()
CAVG()
DSUM()
What error occurs if you select a column not in GROUP BY or aggregated?
ASyntax error
BColumn must appear in the GROUP BY clause or be used in an aggregate function
CMissing FROM clause
DNo error
What is the result of GROUP BY?
AA single row
BRows deleted
CRows sorted alphabetically
DRows grouped into summary rows
Explain how to use GROUP BY with one and multiple columns in PostgreSQL.
Think about grouping by one column versus grouping by pairs or more.
You got /4 concepts.
    Why must columns in SELECT be either in GROUP BY or aggregated?
    Consider what happens if you select a column not grouped or aggregated.
    You got /4 concepts.