SQL QuestSQL Interview Questions › Conditional Logic

Ticket-Size Mix by Category

MediumFreeQuerying BasicsConditional LogicJoinsAggregation & Grouping

Pivot the ledger into spend segments. A dataset question on a card screen often wants one row per category with the transaction mix across amount buckets as COLUMNS — that is conditional aggregation: SUM(CASE WHEN ... THEN 1 ELSE 0 END) once per bucket. Buckets: small below 25, mid from 25 up to but not including 100, large 100 and above.

Return category, small_txns, mid_txns, large_txns, and total_txns (COUNT of all transactions in the category). Order by total_txns descending, then category ascending.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

transactions

txn_idaccount_idamounttxn_atmerchant_idlatlngstatus
1149151.842026-03-04T12:39:39.078Z2440.342-74.4092completed
22110.722026-03-04T13:04:50.641Z752.936613.3229completed
335158.782026-03-04T13:27:15.528Z2552.739713.3134completed

merchants

merchant_idnamecategorycountryrisk_tier
1BigBox MartGroceryTRhigh
2Quick StopElectronicsJPhigh
3Aurora CafeTravelTRhigh

Expected output: Clothing 26 132 289 447, Travel 28 101 244 373 ...

Hint

Three SUM(CASE ...) columns and a COUNT(*) in the same SELECT, GROUP BY m.category. The boundaries are amount < 25, amount >= 25 AND amount < 100, amount >= 100 — the three should add up to total_txns.

Concepts

SELECT CASE JOIN GROUP BY SUM Pivot / Conditional Aggregation

Practise the topic: SQL practice questions · CASE WHEN practice · JOIN practice · GROUP BY exercises

In these company practice sets

Capital One · Ramp · Bloomberg

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Movie Rating Tier BreakdownMedium · FreeGenre Financial ReportMedium · FreeDepartment Compensation ReportMedium · FreePivot: Order Status by CountryMedium · FreeGenre Box Office ReportMedium · FreeFare Imputation AnalysisMedium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test