Aggregate Functions & GROUP BYBeginner
1Question 1 of 1
sqlHow do aggregate functions work with GROUP BY in SQL?
Explanation & Concept
Aggregate functions compute a single summary value across a set of rows. When paired with `GROUP BY`, the engine divides the table into distinct buckets based on unique values of the grouped columns, and calculates the aggregate independently for each group.
Every non-aggregated column in the `SELECT` list must be explicitly included in the `GROUP BY` clause.
Code Example
sql
SELECT
category,
COUNT(*) AS total_items,
ROUND(AVG(price), 2) AS average_price,
MAX(price) AS highest_price,
MIN(price) AS lowest_price
FROM products
WHERE in_stock = 1
GROUP BY category
ORDER BY total_items DESC;How It Works & Key Takeaways
Rows are grouped by `category`. For each category, SQL calculates the count, average price rounded to two decimals, and the maximum and minimum price.
Mock Test · 1 Questions · Optional
Ready for Normalization?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions