Explain how PARTITION BY and ORDER BY define the window in a SQL window function

Quick Overview

Asks how PARTITION BY and ORDER BY work inside a SQL window function and how they decide which rows each calculation sees. It tests the difference from GROUP BY, default window frames and ties, and the pitfalls that make running totals and rankings come out wrong.

Explain how PARTITION BY and ORDER BY define the window in a SQL window function

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the purpose and mechanics of the `PARTITION BY` and `ORDER BY` clauses within a window function. How do they define the window for calculations? ```hint Try it on ties Compare `SUM(salary) OVER (PARTITION BY dept)` with `SUM(salary) OVER (PARTITION BY dept ORDER BY salary DESC)` on a small table, and watch what happens to two people with the same salary. ``` ### What a Strong Answer Covers - What `PARTITION BY` does and how it differs from `GROUP BY` - What `ORDER BY` does inside `OVER (...)`, and which functions depend on it - How the frame is set, including the default frames with and without `ORDER BY` and how ties are treated - A small worked example that shows the effect on actual rows - Common pitfalls, such as output ordering and filtering on a window result ### Follow-up Questions - Why can two rows get the same running total under the default frame, and how do you change that? - Why does `LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary)` usually fail to return the department's highest salary? - How would you keep only the top earner in each department, given that a window function cannot appear in `WHERE`? - What happens when the `PARTITION BY` column contains NULLs?

Overview: Asks how PARTITION BY and ORDER BY work inside a SQL window function and how they decide which rows each calculation sees. It tests the difference from GROUP BY, default window frames and ties, and the pitfalls that make running totals and rankings come out wrong.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the purpose and mechanics of the PARTITION BY and ORDER BY clauses within a window function. How do they define the window for calculations?

What a Strong Answer Covers Guidance

  • What PARTITION BY does and how it differs from GROUP BY
  • What ORDER BY does inside OVER (...) , and which functions depend on it
  • How the frame is set, including the default frames with and without ORDER BY and how ties are treated
  • A small worked example that shows the effect on actual rows
  • Common pitfalls, such as output ordering and filtering on a window result

Follow-up Questions Guidance

  • Why can two rows get the same running total under the default frame, and how do you change that?
  • Why does LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary) usually fail to return the department's highest salary?
  • How would you keep only the top earner in each department, given that a window function cannot appear in WHERE ?
  • What happens when the PARTITION BY column contains NULLs?