SQL

Grouping Data and Filtering Aggregates with SQL

Discover how to group SQL data using GROUP BY and apply conditional filtering to aggregated results with HAVING, perfect for generating summary reports.

SELECT category, COUNT(id) AS total_products, AVG(price) AS average_price
FROM products
GROUP BY category
HAVING COUNT(id) > 5 AND AVG(price) > 50.00
ORDER BY total_products DESC;
How it works: This query groups products by their `category`, then calculates the total number of products (`COUNT`) and their average price (`AVG`) for each category. The `HAVING` clause filters these grouped results, showing only categories that have more than 5 products AND an average price greater than 50.00. The `ORDER BY` clause sorts the final categories by their total product count in descending order.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs