How do NULL values affect SQL aggregate and window functions, and what precautions should you take?

Quick Overview

Asks how NULL values change the results of SQL aggregates such as COUNT, SUM and AVG and of window functions such as LAG, running totals and rankings. Tests the difference between ignoring a NULL and treating it as zero, NULL ordering, and safe defaults with COALESCE.

How do NULL values affect SQL aggregate and window functions, and what precautions should you take?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Discuss the impact of `NULL` values on aggregate and window functions. How does `NULL` handling differ, and what precautions should you take? ```hint Try a tiny table Run `COUNT(*)`, `COUNT(col)`, `SUM` and `AVG` on four rows where two values are NULL, then add a running total and a `LAG` ordered by date, and look at every result row by row. ``` ```hint Two kinds of NULL output Separate a NULL that a function receives as an input value from a NULL it returns because there is no row to read. ``` ### What a Strong Answer Covers - How standard aggregates treat NULL inputs, including `COUNT(*)` versus `COUNT(col)` and the result when every input is NULL. - The difference between ignoring a NULL and treating it as zero, with an example. - Window-specific behavior: `LAG` and `LEAD` at partition edges, NULLs in `PARTITION BY` and `ORDER BY`, running totals and `IGNORE NULLS`. - Concrete precautions: deliberate `COALESCE`, explicit NULL ordering, deliberate denominators and tests with NULL rows. ### Follow-up Questions - A dashboard's average order value dropped after someone added a LEFT JOIN to the query. How could NULLs explain it? - How would you forward-fill the last known value in an engine without `IGNORE NULLS`? - `LAG(price, 1, 0)` still returns NULL on some rows. Why? - Which aggregates do not skip NULLs, and when does that matter?

Overview: Asks how NULL values change the results of SQL aggregates such as COUNT, SUM and AVG and of window functions such as LAG, running totals and rankings. Tests the difference between ignoring a NULL and treating it as zero, NULL ordering, and safe defaults with COALESCE.

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

Discuss the impact of NULL values on aggregate and window functions. How does NULL handling differ, and what precautions should you take?

What a Strong Answer Covers Guidance

  • How standard aggregates treat NULL inputs, including COUNT(*) versus COUNT(col) and the result when every input is NULL.
  • The difference between ignoring a NULL and treating it as zero, with an example.
  • Window-specific behavior: LAG and LEAD at partition edges, NULLs in PARTITION BY and ORDER BY , running totals and IGNORE NULLS .
  • Concrete precautions: deliberate COALESCE , explicit NULL ordering, deliberate denominators and tests with NULL rows.

Follow-up Questions Guidance

  • A dashboard's average order value dropped after someone added a LEFT JOIN to the query. How could NULLs explain it?
  • How would you forward-fill the last known value in an engine without IGNORE NULLS ?
  • LAG(price, 1, 0) still returns NULL on some rows. Why?
  • Which aggregates do not skip NULLs, and when does that matter?