Explain conditional aggregation with CASE inside aggregate functions for pivoting and cohort analysis

Quick Overview

Asks you to explain conditional aggregation, where a CASE expression sits inside an aggregate function, and to show where it helps with pivot tables and cohort analysis. It tests SQL fluency with NULL handling, GROUP BY design and retention-style reporting.

Explain conditional aggregation with CASE inside aggregate functions for pivoting and cohort analysis

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Describe conditional aggregation using `CASE` expressions inside aggregate functions. Give a conceptual example of when this is useful for pivoting or cohort analysis. ```hint NULLs Think about what a `CASE` with no `ELSE` returns for rows that do not match, and what each aggregate function does with that value. ``` ### What a Strong Answer Covers - The pattern itself: a `CASE` (or `FILTER`) inside an aggregate so only matching rows contribute, with `GROUP BY` setting the row dimension - How the choice of `ELSE 0` versus no `ELSE` changes the results of `SUM`, `COUNT` and `AVG` - A pivot example that turns category values into columns - A cohort example with the cohort as the row and conditional counts per period as columns, counting distinct users - Practical limits, such as a column list fixed in the query and the portability of `FILTER` ### Follow-up Questions - Why does `AVG(CASE WHEN cond THEN x ELSE 0 END)` give a different answer from `AVG(CASE WHEN cond THEN x END)`, and when would you want each? - How would you pivot when the set of category values is not known in advance? - How would you turn the cohort counts into retention percentages while keeping users who were never active after signup? - When would you choose a self-join or a window function over conditional aggregation?

Overview: Asks you to explain conditional aggregation, where a CASE expression sits inside an aggregate function, and to show where it helps with pivot tables and cohort analysis. It tests SQL fluency with NULL handling, GROUP BY design and retention-style reporting.

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

Describe conditional aggregation using CASE expressions inside aggregate functions. Give a conceptual example of when this is useful for pivoting or cohort analysis.

What a Strong Answer Covers Guidance

  • The pattern itself: a CASE (or FILTER ) inside an aggregate so only matching rows contribute, with GROUP BY setting the row dimension
  • How the choice of ELSE 0 versus no ELSE changes the results of SUM , COUNT and AVG
  • A pivot example that turns category values into columns
  • A cohort example with the cohort as the row and conditional counts per period as columns, counting distinct users
  • Practical limits, such as a column list fixed in the query and the portability of FILTER

Follow-up Questions Guidance

  • Why does AVG(CASE WHEN cond THEN x ELSE 0 END) give a different answer from AVG(CASE WHEN cond THEN x END) , and when would you want each?
  • How would you pivot when the set of category values is not known in advance?
  • How would you turn the cohort counts into retention percentages while keeping users who were never active after signup?
  • When would you choose a self-join or a window function over conditional aggregation?