Back to Practice
#0376

Product Share Within Category

MediumSQL24 min20 XP

Problem

Return category, product, revenue, and category_share.

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(category TEXT, product TEXT, revenue REAL)

Examples

Example 1
Inputcategory A revenue is 100 total
Outputp1 share 0.3, p2 share 0.7

The window sum gives the category denominator on each product row.

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
Inputcategory A revenue is 100 total
Expected[["A","p1",30,0.3],["A","p2",70,0.7],["B","q1",50,1]]

The window sum gives the category denominator on each product row.

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