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?✗ Incorrect
GROUP BY groups rows sharing the same value in the specified column, allowing aggregate functions to summarize each group.
Which aggregate function counts the number of rows in each group?
✗ Incorrect
COUNT() counts the number of rows in each group.
What will this query return?<br>
SELECT category, COUNT(*) FROM products GROUP BY category;✗ Incorrect
The query groups products by category and counts how many products are in each category.
If you select a column not in the
GROUP BY clause or an aggregate function, what happens?✗ Incorrect
SQL requires all selected columns to be either grouped or aggregated; otherwise, it throws an error.
Which of these is a valid use of
GROUP BY on a single column?✗ Incorrect
Option A groups sales by name and sums prices per name, which is valid.
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.