SQL
Aggregate Data by Category with GROUP BY
Understand how to group rows that have the same values into summary rows using GROUP BY, often with aggregate functions like COUNT or SUM.
SELECT category, COUNT(id) AS total_products, SUM(price) AS total_value
FROM products
GROUP BY category
HAVING COUNT(id) > 10
ORDER BY total_products DESC;
How it works: This query groups products by their 'category' and calculates the total number of products and the sum of their prices for each category. `GROUP BY` organizes the results, while `COUNT()` and `SUM()` are aggregate functions. The `HAVING` clause filters these grouped results, showing only categories with more than 10 products, and `ORDER BY` sorts them by the total product count.