Reshaping

SQL Reshaping Interview Questions

Reshaping turns rows into columns, or columns into rows — one row per month becoming one column per month, or the other way round. The shape a database stores data in is rarely the shape somebody wants to read.

What reshaping questions ask you to do

Across the questions here, these are the shapes that come up:

  • Turn the values in one column into several columns
  • Turn several columns back into rows
  • Build a grid of counts or totals
  • Flatten a nested or repeated structure into rows

Common mistakes with reshaping

Turning rows into columns in SQL needs one CASE per output column, which means the columns have to be known before the query is written — SQL cannot discover its own column names. In pandas the two tools differ too: pivot refuses duplicate pairs while pivot_table quietly averages them, and that difference is easy to miss.

Questions to practice

Roles this comes up in

Companies these come from

Other topics