What are common pitfalls with SQL window functions: NULLs, frame defaults and ROW_NUMBER() ties?

Quick Overview

Asks for the common correctness pitfalls in SQL window functions, from ROW_NUMBER results on tied rows to implicit frame defaults and NULL handling. Tests whether you can explain RANGE versus ROWS frames, deterministic ordering, and how NULLs behave in partitions, sorting and LAG or LEAD.

What are common pitfalls with SQL window functions: NULLs, frame defaults and ROW_NUMBER() ties?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What are common pitfalls or correctness issues with window functions, such as `NULL` handling in partitions, off-by-one frame errors, or non-deterministic `ROW_NUMBER()` results with ties? ```hint Write out the frame For a window with an `ORDER BY` and no frame clause, list exactly which rows the frame contains for a row that ties with its neighbor. ``` ```hint Look at what breaks ties Ask what decides the order of rows that share the same `ORDER BY` value, and whether that order is guaranteed to be the same on the next run. ``` ### What a Strong Answer Covers - Why `ROW_NUMBER()` over a non-unique ordering is unstable, and how to fix it or pick a different ranking function - The default frame when `ORDER BY` is present, and how `RANGE` and `ROWS` treat peer rows differently - How NULLs behave in `PARTITION BY`, in `ORDER BY`, and at partition edges for `LAG` and `LEAD` - Other frame traps, such as `LAST_VALUE` and moving windows over gaps in the data ### Follow-up Questions - Why does `LAST_VALUE` often return the current row's value, and how do you fix it? - How would you compute a 7-day moving average when some days have no rows? - Why can't you filter on a window function in `WHERE`, and how would you keep only the latest event per user? - How do aggregate window functions such as `AVG` treat NULL values inside the frame?

Overview: Asks for the common correctness pitfalls in SQL window functions, from ROW_NUMBER results on tied rows to implicit frame defaults and NULL handling. Tests whether you can explain RANGE versus ROWS frames, deterministic ordering, and how NULLs behave in partitions, sorting and LAG or LEAD.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What are common pitfalls or correctness issues with window functions, such as NULL handling in partitions, off-by-one frame errors, or non-deterministic ROW_NUMBER() results with ties?

What a Strong Answer Covers Guidance

  • Why ROW_NUMBER() over a non-unique ordering is unstable, and how to fix it or pick a different ranking function
  • The default frame when ORDER BY is present, and how RANGE and ROWS treat peer rows differently
  • How NULLs behave in PARTITION BY , in ORDER BY , and at partition edges for LAG and LEAD
  • Other frame traps, such as LAST_VALUE and moving windows over gaps in the data

Follow-up Questions Guidance

  • Why does LAST_VALUE often return the current row's value, and how do you fix it?
  • How would you compute a 7-day moving average when some days have no rows?
  • Why can't you filter on a window function in WHERE , and how would you keep only the latest event per user?
  • How do aggregate window functions such as AVG treat NULL values inside the frame?