Reformat Department Table - Problem

You're a data analyst working with a company's monthly revenue data. The finance team has provided you with a database table that stores each department's revenue for different months in a normalized format (each row represents one department-month combination).

Your task is to pivot this data into a more readable format where each department appears as a single row with separate columns for each month's revenue.

Input Format:
A table named Department with the following structure:

Column NameType
idint
revenueint
monthvarchar

Where (id, month) is the primary key, and month contains values from ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].

Output Format:
Transform the table so that there's one row per department with columns for id and each month's revenue (Jan_Revenue, Feb_Revenue, etc.).

Input & Output

example_1.sql โ€” Basic Example
$ Input: Department table: | id | revenue | month | |----|---------|-------| | 1 | 8000 | Jan | | 2 | 9000 | Jan | | 3 | 10000 | Feb | | 1 | 7000 | Feb | | 1 | 6000 | Mar |
โ€บ Output: | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | Apr_Revenue | ... | Dec_Revenue | |----|-------------|-------------|-------------|-------------|-----|-------------| | 1 | 8000 | 7000 | 6000 | null | ... | null | | 2 | 9000 | null | null | null | ... | null | | 3 | null | 10000 | null | null | ... | null |
๐Ÿ’ก Note: Each department ID becomes a single row with separate columns for each month's revenue. Missing month data shows as NULL.
example_2.sql โ€” Single Department
$ Input: Department table: | id | revenue | month | |----|---------|-------| | 1 | 8000 | Jan | | 1 | 7500 | Jun | | 1 | 9200 | Dec |
โ€บ Output: | id | Jan_Revenue | Feb_Revenue | ... | Jun_Revenue | ... | Dec_Revenue | |----|-------------|-------------|-----|-------------|-----|-------------| | 1 | 8000 | null | ... | 7500 | ... | 9200 |
๐Ÿ’ก Note: Department 1 has revenue data for only 3 months (Jan, Jun, Dec), other months show NULL.
example_3.sql โ€” Empty Table
$ Input: Department table: | id | revenue | month | |----|---------|-------| (empty table)
โ€บ Output: | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | Apr_Revenue | ... | Dec_Revenue | |----|-------------|-------------|-------------|-------------|-----|-------------| (no rows)
๐Ÿ’ก Note: When the input table is empty, the pivoted result is also empty.

Visualization

Tap to expand
Before: Normalized DataDept 1Jan8000Dept 1Feb7000Dept 2Jan9000After: Pivoted DataDept 1Jan: 8000Feb: 7000Mar: NULLDept 2Jan: 9000Feb: NULLMar: NULLPIVOTKey SQL ComponentsGROUP BY id - Groups rows by departmentCASE WHEN month = 'Jan' THEN revenue END - Conditional selectionSUM(...) - Aggregates matching valuesResult: One row per department, columns for each month
Understanding the Visualization
1
Original Data Structure
Each row represents one department's revenue for one specific month
2
Group by Department
SQL groups all rows belonging to the same department ID
3
Apply Conditional Logic
CASE WHEN statements check each row's month and assign revenue to appropriate column
4
Aggregate Results
SUM function combines matching values (only one value per department-month due to primary key constraint)
Key Takeaway
๐ŸŽฏ Key Insight: SQL's CASE WHEN combined with GROUP BY enables efficient row-to-column transformation in a single table scan, making it the optimal approach for pivoting data.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the table with GROUP BY aggregation

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space for d departments in the result set

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Department table rows โ‰ค 1000
  • 1 โ‰ค id โ‰ค 100
  • 0 โ‰ค revenue โ‰ค 106
  • month is one of ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
  • (id, month) forms the primary key - no duplicate combinations
Asked in
Amazon 15 Microsoft 12 Meta 8 Google 6
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen