InterviewPrepKit

Home / SQL / Aggregation & Grouping

Min and Max Sale Per Region

easy
Solving tips
  • MIN and MAX are ordinary aggregates — combine several of them in one GROUP BY pass.
  • You can compute a derived value like MAX(x) - MIN(x) directly in SELECT without a subquery.
  • Add a stable tie-break column to ORDER BY when the primary sort key has duplicates.

Given a sales log, report the smallest and largest sale amount in each region and the spread between them.

Schema

CREATE TABLE sales (
  sale_id INTEGER PRIMARY KEY,
  region  TEXT NOT NULL,
  amount  NUMERIC NOT NULL
);

Sample data:

sale_idregionamount
1North100
2North250
3North175
4South400
5South150
6West300

Task

For each region, return:

  • region
  • min_amount — the smallest amount in that region
  • max_amount — the largest amount in that region
  • spreadmax_amount minus min_amount

Order the result by spread descending, then by region ascending as a tie-break.

Expected output

regionmin_amountmax_amountspread
South150400250
North100250150
West3003000
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.