Aggregation: GROUP BY & HAVING
Aggregation collapses multiple matching rows into calculation fields using SUM, AVG, COUNT, MIN, and MAX operations.
1 Grouping Results
SQL — Aggregations
# Calculate count of posts per user
SELECT user_id, COUNT(id) AS total_posts
FROM posts
GROUP BY user_id;
# GROUP BY with HAVING filter constraint on aggregated results
SELECT user_id, COUNT(id) AS total_posts
FROM posts
GROUP BY user_id
HAVING total_posts > 5;
2 Code Challenge
Challenge: Write a query groups products by category, displaying category name, average product price, and total stock quantity. Filter out categories having average price below $10.