0
0
SQLquery~5 mins

GROUP BY with aggregate functions in SQL - 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 store.
Click to reveal answer
beginner
Name one common aggregate function used with GROUP BY.
Examples include COUNT(), SUM(), AVG(), MIN(), and MAX(). They calculate values like totals or averages for each group.
Click to reveal answer
beginner
Why do we use aggregate functions with GROUP BY?
Because GROUP BY creates groups of rows, aggregate functions summarize data within each group, like counting items or finding averages.
Click to reveal answer
intermediate
What happens if you use a column in SELECT that is not in GROUP BY or an aggregate function?
SQL will give an error because it doesn't know how to combine that column's values for each group.
Click to reveal answer
beginner
Write a simple SQL query to find the total sales per store using GROUP BY and SUM().
Example: <br>SELECT store_id, SUM(sales) AS total_sales FROM sales_table GROUP BY store_id;
Click to reveal answer
What does GROUP BY do in an SQL query?
AGroups rows with the same values in specified columns
BDeletes duplicate rows
CSorts rows alphabetically
DFilters rows based on a condition
Which aggregate function counts the number of rows in each group?
ASUM()
BAVG()
CMAX()
DCOUNT()
What will happen if you select a column not in GROUP BY or an aggregate function?
ASQL will return the first value
BSQL will return NULL
CSQL will give an error
DSQL will ignore the column
Which SQL clause is used to group rows before applying aggregate functions?
AGROUP BY
BORDER BY
CWHERE
DHAVING
How do you find the average price per category in a products table?
ASELECT AVG(price) FROM products;
BSELECT category, AVG(price) FROM products GROUP BY category;
CSELECT category, price FROM products;
DSELECT category, SUM(price) FROM products;
Explain how GROUP BY works with aggregate functions in SQL.
Think about how you would summarize sales by store.
You got /3 concepts.
    Describe a situation where using GROUP BY with SUM() would be helpful.
    Imagine you want to know total sales for each store.
    You got /3 concepts.