SQL · Module 06: Window Functions (SQL Interview Superpower)Tutorial 16 of 24
All Tutorials·~1.5 hrs

6.1 Window Functions: OVER(), PARTITION BY & GROUP BY vs Window

Unlock the #1 skill that separates junior from senior SQL engineers: Window Functions. Learn how OVER() and PARTITION BY compute department averages and percentages WITHOUT collapsing individual rows.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. GROUP BY vs Window Functions — The Crucial Visual Difference

With GROUP BY, 100 employee rows collapse into 5 department summary rows — you lose every individual employee's name and salary.
With a Window Function (OVER (...)), SQL computes the department aggregate and attaches it right next to every single employee row — 0 rows are collapsed!

GROUP BY (Collapses Rows)4 input rows → 2 summary rowsYou lose individual employee detailsWINDOW OVER() (Keeps Every Row!)4 input rows → 4 enriched rowsAdds rank, running total & dept avg per row
GROUP BY collapses rows into summary buckets. Window Functions (OVER) preserve every single input row while adding group-level metrics!
DimensionGROUP BYWindow Function (OVER (...))
Output Row CountCollapses to 1 row per groupIdentical to input row count
Keeps Row-Level Columns?❌ Only grouped columns allowed✅ Keeps emp_id, name, salary, etc.!
When Does It Execute?Step 3 (before HAVING)Step 5 (during SELECT) — after WHERE, GROUP BY, HAVING!
Can Filter Directly in WHERE?❌ No (use HAVING)❌ No (wrap in a CTE, then filter in outer WHERE!)

2. Anatomy of a Window Function

SQL
function_name(...) OVER (
    PARTITION BY group_column      -- Optional: splits rows into independent windows (like GROUP BY)
    ORDER BY     sort_column DESC  -- Optional: orders rows inside each window
)

Real-World Example: Compare Every Employee Against Their Department Average!

SQL
SELECT
    name,
    dept,
    salary,
    ROUND(AVG(salary) OVER (PARTITION BY dept), 0)            AS dept_avg,
    salary - ROUND(AVG(salary) OVER (PARTITION BY dept), 0)   AS diff_from_avg,
    ROUND(100.0 * salary / SUM(salary) OVER (PARTITION BY dept), 1) AS pct_of_dept_payroll
FROM employees
ORDER BY dept, salary DESC;
  • Notice OVER () (empty parentheses!) treats the entire result set as one single window — e.g., SUM(salary) OVER () gives the company-wide grand total on every row!

3. Watch AVG() OVER (PARTITION BY dept) Execute Live

Tracing Window Function AVG(salary) OVER (PARTITION BY dept)
Interactive SQL Execution Tracer · Step 1 of 3 (1. FROM employees (4 rows))
1SELECT name, dept, salary,
2 AVG(salary) OVER (PARTITION BY dept) AS dept_avg
3→FROM employees;
Live Table State · 1. FROM employees (4 rows)4 rows
namedeptsalary
AaravEng120000
KabirEng160000
MeeraSales80000
RiyaSales100000
What the DB engine just did: All 4 rows are loaded. Every single row will remain in the final output.

4. Quick Check Your Understanding

Module 6.1 Quick Quiz

Q1.What does SUM(revenue) OVER () (with empty parentheses) compute?
Q2.Can you write WHERE AVG(salary) OVER (PARTITION BY dept) > 100000 directly?

Keep Exploring: Related Lessons & Tools

All Courses →