When would you reach for a SQL window function instead of GROUP BY, and how do ROW_NUMBER, RANK, and DENSE_RANK differ?

technical-conceptual · Mid level · data-ml

What the interviewer is really asking

Assesses whether the candidate understands that window functions keep every row while GROUP BY collapses them, and can pick the right ranking function based on how ties should be handled.

What to say

What to avoid

Example answers

Strong: To get the latest order row per customer I used ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_ts DESC) and filtered to rn = 1 — that keeps the full row, which a GROUP BY MAX(order_ts) would have collapsed and forced me to join back to recover.

Weak: I'd just GROUP BY and use MAX — window functions are basically the same and I find them confusing.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.