How do you typically handle missing values or NULLs during the pivoting and unpivoting processes?

Quick Overview

Asks how to treat NULL or missing values when pivoting data from long to wide format and when unpivoting it back. It tests where empty cells come from, how fill choices change aggregates, and how unpivot handles NULLs in different SQL dialects and in pandas.

How do you typically handle missing values or NULLs during the pivoting and unpivoting processes?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you typically handle missing values or `NULL`s when pivoting data from long to wide format, and when unpivoting it back from wide to long? ```hint Where the gaps come from Ask why a pivoted cell ends up empty. There is more than one way it can happen, and the cases may need different treatment. ``` ```hint Check an aggregate Average a pivoted column once with the empty cells left as NULL and once with them filled with 0, and compare the results. ``` ### What a Strong Answer Covers - Where NULLs come from in a pivot and how to decide between filling them and leaving them - How unpivot operators and manual unpivots treat NULL cells, including differences between SQL dialects - How the fill choice changes downstream aggregates, shown with a small example - Rows whose pivot key is NULL, and categories or periods with no rows at all ### Follow-up Questions - How would you tell a cell with no source rows apart from a cell whose source rows all had NULL values? - How do you unpivot in PostgreSQL, which has no `UNPIVOT` operator? - Your report needs a column or row for every month, including months with no sales. How do you guarantee that? - If you pivot, fill the gaps with 0 and unpivot again, what has changed compared with the original table?

Overview: Asks how to treat NULL or missing values when pivoting data from long to wide format and when unpivoting it back. It tests where empty cells come from, how fill choices change aggregates, and how unpivot handles NULLs in different SQL dialects and in pandas.

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

How do you typically handle missing values or NULLs when pivoting data from long to wide format, and when unpivoting it back from wide to long?

What a Strong Answer Covers Guidance

  • Where NULLs come from in a pivot and how to decide between filling them and leaving them
  • How unpivot operators and manual unpivots treat NULL cells, including differences between SQL dialects
  • How the fill choice changes downstream aggregates, shown with a small example
  • Rows whose pivot key is NULL, and categories or periods with no rows at all

Follow-up Questions Guidance

  • How would you tell a cell with no source rows apart from a cell whose source rows all had NULL values?
  • How do you unpivot in PostgreSQL, which has no UNPIVOT operator?
  • Your report needs a column or row for every month, including months with no sales. How do you guarantee that?
  • If you pivot, fill the gaps with 0 and unpivot again, what has changed compared with the original table?