The GROUP BY clause groups rows sharing identical column values into summary rows. It is used with aggregate functions (COUNT(), SUM(), AVG(), MIN(), MAX()).
flowchart TD
A["Raw Table Rows"] --> B["Filter Rows (WHERE)"]
B --> C["Group Rows (GROUP BY)"]
C --> D["Calculate Aggregates (SUM, AVG)"]
D --> E["Filter Groups (HAVING)"]
-- Calculate total revenue per product category
SELECT
category,
COUNT(id) AS total_products,
SUM(sales_amount) AS category_revenue
FROM sales
GROUP BY category;
| category | total_products | category_revenue |
|---|---|---|
| Electronics | 42 | 125400.00 |
| Books | 115 | 18450.50 |
SELECT must be explicitly included in GROUP BY.WHERE to filter raw rows before grouping; use HAVING to filter aggregated values after grouping.What is the operational difference between the WHERE clause and the HAVING clause?
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With