Reviewed by Aditya Kumar · Last reviewed 2026-03-24
SELECT * FROM (SELECT customer_id, region, SUM(revenue) AS total, RANK() OVER (PARTITION BY region ORDER BY SUM(revenue) DESC) rn FROM orders GROUP BY customer_id, region) WHERE rn <= 3. **Why**: Aggregate then rank per region. RANK or DENSE_RANK per tie rule....
SELECT * FROM (SELECT customer_id, region, SUM(revenue) AS total, RANK() OVER (PARTITION BY region ORDER BY SUM(revenue) DESC) rn FROM orders GROUP BY customer_id, region) WHERE rn <= 3. Why: Aggregate then rank per region. RANK or DENSE_RANK per tie rule. Scalability: Standard top-N per partition.
Red Flag: RANK when exactly 3 needed—ties can yield 4+. Pro-Move: 'ROW_NUMBER for exactly 3; RANK when ties share rank.'
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.