SQL

Calculate Aggregate Values with GROUP BY

Summarize data in SQL by grouping rows with similar values using GROUP BY, enabling calculations like total sum or average for each group to generate reports.

SELECT
  category,
  COUNT(product_id) AS total_products,
  SUM(price) AS total_category_value
FROM
  products
GROUP BY
  category
HAVING
  COUNT(product_id) > 5
ORDER BY
  total_products DESC;
How it works: This query groups products by their `category` and calculates aggregate values: the total count of products and the sum of their prices within each category. The `HAVING` clause then filters these groups, showing only categories that contain more than 5 products, ordered by the total product count.

Need help integrating this into your project?

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

Hire DigitalCodeLabs