{"id":"data2-sql-rank-reps-by-region-ties","audience":"analysts","category":"data","track":"data","technique":"constraints","level":"advanced","title":"Write SQL for the top 2 reps per region with ties","free":false,"task":"A sales ops manager wants the top 2 reps by closed-won revenue in each region for Q3 2025, and if two reps tie for 2nd place both should appear. Ask for one Postgres query in a single code block with at most 35 words outside it. Count only deals with stage 'Closed Won' closed between July 1 and September 30, 2025.","materialLabel":"Schema and notes (attached to your prompt automatically)","material":"Schema (Postgres) - Redwood CRM warehouse\n\nreps(rep_id, rep_name, region, is_active)\ndeals(deal_id, rep_id, stage, close_date, amount_usd, is_refunded)\n  stage values: 'Open', 'Closed Won', 'Closed Lost'\n\nNotes:\n- amount_usd is whole dollars (numeric).\n- A rep can have many deals; a few reps have no deals in Q3.\n- is_refunded = true marks deals that were later reversed; those should not count toward revenue.\n- Departed reps have is_active = false but their Q3 deals still count and they can still rank.\n- Last quarter the report used ROW_NUMBER() and dropped a rep who tied for 2nd with an equal total, which caused a complaint.\n- Q3 2025 = close_date from 2025-07-01 through 2025-09-30 inclusive.\n- Columns wanted: region, rep_name, revenue, rank within region.","rules":[{"type":"codeBlocks","count":1},{"type":"maxProseWords","max":35}],"mistakes":[{"title":"Using ROW_NUMBER","why":"It numbers rows 1, 2, 3 and drops a rep who tied for 2nd.","fix":"Add: \"Use RANK() so ties for 2nd both appear.\"","checks":["r0"]},{"title":"Wrong deal filter","why":"The AI counts every deal, including refunded or lost ones.","fix":"Add: \"Closed Won, Q3 dates inclusive, is_refunded false.\"","checks":["r1"]},{"title":"Filtering rank in WHERE","why":"Window functions can't be used in WHERE, so the query errors.","fix":"Add: \"Compute revenue in a CTE, rank in a subquery, filter rank <= 2 outside.\"","checks":["r2"]},{"title":"Leaving the format to chance","why":"The AI picks its own length and layout unless told the exact shape.","fix":"Add: \"Put the code in exactly 1 code block. Keep the writing outside the code under 35 words.\"","checks":["codeBlocks","maxProseWords"]}],"promptHints":[{"label":"Names the ranking function","patterns":["rank\\(\\)","not row_number","ties"]},{"label":"Describes the deal filter","patterns":["closed won","is_refunded","2025-09-30"]},{"label":"Says where to filter","patterns":["cte","subquery","outside"]},{"label":"Keeps departed reps","patterns":["is_active","departed"]},{"label":"Limits the writing","patterns":["one (short )?sentence","\\d+ words","code block"]}],"hint":"Ranking questions hinge on tie handling and where the filter goes. Name the function and the order of steps.","mission":"Write SQL for top 2 reps per region.","musts":["One Postgres query","Ties for 2nd both appear","Refunds and departed reps handled"],"rubricLabels":["The query sums amount_usd per rep for stage = 'Closed Won' with close_date between 2025-07-01 and 2025-09-30 inclusive, and excludes is_refunded deals","It ranks within each region with RANK() or DENSE_RANK() (not ROW_NUMBER) using PARTITION BY region and revenue descending, and keeps rank <= 2 so ties for 2nd both appear","The ranking is applied in a CTE or subquery before the rank <= 2 filter (not in a WHERE clause directly), it does not filter on is_active, and it returns region, rep_name, revenue and rank"]}