Aggregate Functions vs Window Functions: Concept
Module: Aggregate Functions & Grouping
Aggregate functions with GROUP BY collapse rows into summaries. Window functions preserve all rows while adding aggregate context using OVER clause.
**Key Differences:**
**Aggregate Functions (GROUP BY):**
- Collapse multiple rows into summary rows
- One row per group
- Use GROUP BY to define groups
- Result: Summary report
**Window Functions (OVER):**
- Preserve all detail rows
- Add aggregate context to each row
- Use OVER to define window
- Result: Detail with context
**Example Comparison:**
Aggregate (GROUP BY):
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
-- Returns 3 rows (one per department)
Window Function (OVER):
SELECT employee_name, salary,
AVG(salary) OVER (PARTITION BY department)
FROM employees;
-- Returns 100 rows (one per employee with dept avg)
**When to Use Each:**
- Aggregates: Summary reports, totals, KPIs
- Window: Detail with context, rankings, running totals
Critical for advanced analytics. Data analysts and BI developers must understand when to collapse data (aggregates) vs preserve detail (window functions).
Window functions preserve detail while adding context: show each employee with department average, each order with customer total, each sale with running total. Aggregates collapse to summaries.