Interview Q&A / SQL & DBMS

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How is a window function different from GROUP BY?

SHORT ANSWER

GROUP BY collapses rows into one row per group. A window function computes an aggregate or rank over a set of related rows but keeps every original row in the output.

Explain it simply

GROUP BY is like announcing only the section-wise average marks: 10 sections, 10 lines. A window function is like writing next to each student's own mark the average of their section. All students stay visible, and each carries the group figure alongside.

Example

Table employees(name, dept, salary).

sql
-- GROUP BY, one row per dept
SELECT dept, AVG(salary) AS avg_sal
FROM employees
GROUP BY dept;

-- window, one row per employee
SELECT name, dept, salary,
       AVG(salary) OVER (PARTITION BY dept) AS dept_avg,
       salary - AVG(salary) OVER (PARTITION BY dept) AS diff_from_avg
FROM employees;

The second query lets you see each person and the department average together without a join back to a grouped subquery.

Window functions cannot be used directly in WHERE or HAVING, because they run after those clauses. Wrap the query in a subquery or CTE to filter on them.

What to say in the interview

"GROUP BY reduces rows, a window function does not. OVER defines the window with PARTITION BY and ORDER BY. I use windows for rankings, running totals and comparing a row to its group."

Common mistakes

  • Trying to put a window function in WHERE.

#sql#window-functions#group-by

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.