Back to Practice
#0334

Assign Revenue Quartiles

MediumSQL24 min20 XP

Problem

Return customer_id, revenue, and quartile using NTILE(4) ordered by revenue descending.

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

customers(customer_id INTEGER, revenue INTEGER)

Examples

Example 1
Inputfour customers with descending revenue
Outputquartiles 1 through 4

NTILE(4) splits the ordered rows into four buckets.

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
Inputfour customers with descending revenue
Expected[[1,400,1],[2,300,2],[3,200,3],[4,100,4]]

NTILE(4) splits the ordered rows into four buckets.

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