Pivot rows to columns in SQL: conditional aggregation versus a PIVOT operator, and the limits of dynamic pivoting

Quick Overview

Asks how to turn rows into columns in SQL with conditional aggregation and how that compares with a vendor PIVOT operator. Tests portability, NULL handling for missing combinations, why a query's output columns must be known up front, and how dynamic pivoting works when categories change.

Pivot rows to columns in SQL: conditional aggregation versus a PIVOT operator, and the limits of dynamic pivoting

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A `sales` table holds one row per sale with a `product`, a `month` and an amount, and a report needs one row per product with one column per month. How can you produce that pivot with conditional aggregation, and how does it compare with a dedicated `PIVOT` operator? What limitations do the approaches have, such as dynamic pivoting when the set of categories is not known in advance? ```hint Write one column by hand Pick a single output column, such as January, and decide which rows should feed it and what it should show for a product with no January sales. ``` ```hint Think about the output schema Ask when the database has to know the names and number of the output columns, and what that means for a category that first appears in the data next week. ``` ### What a Strong Answer Covers - How conditional aggregation builds each output column from a `CASE` (or `FILTER`) inside an aggregate, grouped by the row key - How a `PIVOT` operator expresses the same result, where it is available, and its quirks - Why the pivot values must be listed in the query, and how dynamic pivoting works around that and what it costs - How missing combinations and unmatched category values show up in the result - When to pivot in SQL and when to leave the data long and pivot in the application or BI layer ### Follow-up Questions - How would you reverse the operation and unpivot a wide table back to long form? - How would you generate a dynamic pivot safely, and what risks does building SQL strings introduce? - The report needs both total revenue and order count per month. How does each approach handle that? - A dashboard reads from a dynamically pivoted query. What breaks when a new category appears?

Overview: Asks how to turn rows into columns in SQL with conditional aggregation and how that compares with a vendor PIVOT operator. Tests portability, NULL handling for missing combinations, why a query's output columns must be known up front, and how dynamic pivoting works when categories change.

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

A sales table holds one row per sale with a product, a month and an amount, and a report needs one row per product with one column per month. How can you produce that pivot with conditional aggregation, and how does it compare with a dedicated PIVOT operator? What limitations do the approaches have, such as dynamic pivoting when the set of categories is not known in advance?

What a Strong Answer Covers Guidance

  • How conditional aggregation builds each output column from a CASE (or FILTER ) inside an aggregate, grouped by the row key
  • How a PIVOT operator expresses the same result, where it is available, and its quirks
  • Why the pivot values must be listed in the query, and how dynamic pivoting works around that and what it costs
  • How missing combinations and unmatched category values show up in the result
  • When to pivot in SQL and when to leave the data long and pivot in the application or BI layer

Follow-up Questions Guidance

  • How would you reverse the operation and unpivot a wide table back to long form?
  • How would you generate a dynamic pivot safely, and what risks does building SQL strings introduce?
  • The report needs both total revenue and order count per month. How does each approach handle that?
  • A dashboard reads from a dynamically pivoted query. What breaks when a new category appears?