Home › Interview Questions › Why would you use a window function instead of a t…

Why would you use a window function instead of a traditional GROUP BY in SQL?

🟡 Medium Conceptual Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

Window functions compute an aggregate or ranking over a set of rows that are related to the current row, but they return one row per input row, preserving the original granularity. This is useful when you need running totals, moving averages, percent‑rank, or to compare a row to its neighbors without collapsing the result set. In contrast, GROUP BY aggregates rows into a single row per group, which discards the individual row context. For example, to compute a running total of sales per day you can use SUM(sales) OVER (ORDER BY date), which keeps each date in the result. If you used GROUP BY you would lose the ability to see the intermediate totals. Window functions also allow multiple aggregates in the same query, whereas GROUP BY would require separate queries or sub‑queries.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500