Skip to main content
InterviewHubPRO
Section Overview
Q13

Question 13 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

26%
SQL & Relational Databases (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

What are Window Functions and how do they differ from `GROUP BY`?

senior LevelCode ExampleSystem Design

In Simple Words:

  • GROUP BY: Collapses multiple rows into a single summary row.
  • Window Functions (OVER (PARTITION BY ... ORDER BY ...)): Performs calculations across a set of rows related to the current row, while preserving the individual identity of each row:
sql
-- Calculate rank and running total without collapsing rows:
SELECT
  employee_id,
  department,
  salary,
  RANK() OVER (PARTITION BY department ORDER BY salary DESC) as dept_rank,
  SUM(salary) OVER (PARTITION BY department ORDER BY salary ASC) as running_total
FROM employees;