0
0
SQLquery~5 mins

GROUP BY single column 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 value in a specified column into summary rows, like grouping all sales by each product.
Click to reveal answer
beginner
Write a simple SQL query to count how many orders each customer has made using GROUP BY on a single column.
SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id;
Click to reveal answer
beginner
Why do we need to use GROUP BY when using aggregate functions like COUNT() or SUM()?
Because aggregate functions summarize data, GROUP BY tells SQL how to group rows before summarizing, so you get results per group instead of one total.
Click to reveal answer
intermediate
Can you use GROUP BY on multiple columns? What happens if you only use one column?
Yes, you can group by multiple columns to get more detailed groups. Using one column groups rows only by that column's values, combining all other differences.
Click to reveal answer
intermediate
What will happen if you select columns in a query with GROUP BY but don't include them in the GROUP BY clause or an aggregate function?
SQL will give an error because it doesn't know how to group or summarize those columns. Every selected column must be grouped or aggregated.
Click to reveal answer
What does the SQL clause GROUP BY do when used with a single column?
ASorts the rows by that column
BDeletes duplicate rows from the table
CGroups rows with the same value in that column into summary rows
DFilters rows based on a condition
Which aggregate function counts the number of rows in each group?
ASUM()
BCOUNT()
CAVG()
DMAX()
What will this query return?<br>SELECT category, COUNT(*) FROM products GROUP BY category;
ATotal count of all products
BError because COUNT(*) is used
CList of categories without counts
DCount of products per category
If you select a column not in the GROUP BY clause or an aggregate function, what happens?
ASQL returns an error
BSQL ignores that column
CSQL returns the first value it finds
DSQL groups by that column automatically
Which of these is a valid use of GROUP BY on a single column?
ASELECT name, SUM(price) FROM sales GROUP BY name;
BSELECT name, SUM(price) FROM sales;
CSELECT SUM(price) FROM sales GROUP BY name;
DSELECT name FROM sales GROUP BY price;
Explain in your own words how the GROUP BY clause works when grouping by a single column.
Think about how you might group items in real life, like sorting fruits by type.
You got /3 concepts.
    Describe a simple example where you would use GROUP BY on one column and an aggregate function in a query.
    Imagine counting how many orders each customer made.
    You got /4 concepts.