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 product.
Click to reveal answer
beginner
Why do we use
ORDER BY after GROUP BY?To sort the grouped results in a specific order, such as sorting groups by total sales from highest to lowest.
Click to reveal answer
intermediate
Can you use aggregate functions like
SUM() with GROUP BY? Give an example.Yes. For example,
SELECT product, SUM(sales) FROM sales_data GROUP BY product; sums sales for each product group.Click to reveal answer
intermediate
What happens if you use
ORDER BY on a column not in the GROUP BY clause?You can order by aggregate results or columns in the SELECT list, but not by columns not grouped or aggregated, or it causes an error.
Click to reveal answer
beginner
Write a simple SQL query that groups sales by region and orders the result by total sales descending.
Example:
SELECT region, SUM(sales) AS total_sales FROM sales_data GROUP BY region ORDER BY total_sales DESC;Click to reveal answer
What is the purpose of the
GROUP BY clause in SQL?✗ Incorrect
GROUP BY groups rows that share the same values in specified columns.
Which clause should come first in a SQL query:
GROUP BY or ORDER BY?✗ Incorrect
GROUP BY must come before ORDER BY to group rows before sorting.
What will this query do?
SELECT city, COUNT(*) FROM customers GROUP BY city ORDER BY COUNT(*) DESC;✗ Incorrect
It groups customers by city, counts them, and orders cities by count descending.
Can you use
ORDER BY on an aggregate function result like SUM(sales)?✗ Incorrect
ORDER BY can sort results by aggregate function values.
If you group by
department, which column can you order by?✗ Incorrect
You can order by grouped columns or aggregate results.
Explain how
GROUP BY and ORDER BY work together in a SQL query.Think about grouping first, then sorting the groups.
You got /4 concepts.
Write a SQL query that groups sales by product category and orders the categories by total sales ascending.
Use SUM() and ORDER BY with ASC.
You got /3 concepts.