Which SQL feature allows calculations to be performed across a group of…
2024
Which SQL feature allows calculations to be performed across a group of related rows while still returning each individual row separately, typically using the PARTITION BY clause?
- A.
Nested Query
- B.
Trigger Function
- C.
Scalar Function
- D.
Aggregate Constraint
- E.
Window Function
Attempted by 28 students.
Show answer & explanation
Correct answer: E
The correct answer is Window Function.
A window function performs a calculation across a set of rows that are related to the current row — called a "window" — without collapsing those rows into a single output row. It is written with the OVER clause, and the optional PARTITION BY clause divides the rows into groups (partitions) over which the calculation is applied independently. For example: SUM(salary) OVER (PARTITION BY department) returns, on every employee row, the total salary of that employee's department, while still listing each employee separately.
This is exactly what distinguishes a window function from a GROUP BY aggregate. GROUP BY collapses each group into one summary row and discards the individual rows, so per-row detail (like an employee's name or id) is lost. A window function keeps every original row and simply attaches the group-level value alongside it.
Why the other options are wrong: a nested query (subquery) only composes one query inside another; a trigger is a procedure auto-fired on INSERT/UPDATE/DELETE; a scalar function returns one value per row in isolation and cannot span a partition; and "Aggregate Constraint" is not a real SQL feature. None of these provide the PARTITION BY, per-row-preserving behaviour the question describes.