Back to Practice
#0353

Unpivot Quarter Sales

MediumSQL24 min20 XP

Problem

Return product, quarter, and revenue from q1 and q2 columns.

Why This Matters

SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.

Schema

sales(product TEXT, q1 INTEGER, q2 INTEGER)

Examples

Example 1
Inputq1 and q2 columns
Outputtwo rows per product

UNION ALL stacks each quarter column into a row format.

Constraints

  • Return only the requested columns.
  • Use deterministic ORDER BY when multiple rows are returned.
  • Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputq1 and q2 columns
Expected[["Book","q1",10],["Book","q2",15],["Pen","q1",5],["Pen","q2",7]]

UNION ALL stacks each quarter column into a row format.

Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks