Window Functions Questions with Answers

41 practice questions on Window Functions (SQL) with answers and step-by-step solutions. Try 10 samples below, then practise all 41 free in the AptiNest app.

Sample questions

Question 1Easy

Compared with GROUP BY region, what does SUM(amount) OVER (PARTITION BY region) do differently?

  1. GROUP BY is evaluated after window functions
  2. A window function returns a value for every input row instead of collapsing rows into groups
  3. Window functions cannot use PARTITION BY
  4. Window functions can appear only in WHERE

Answer: B. A window function returns a value for every input row instead of collapsing rows into groups

Explanation

SUM(x) OVER (PARTITION BY g) attaches the group total to each row, while GROUP BY returns one row per group.

Step-by-step solution

  1. GROUP BY collapses rows into one per group.
  2. A window function with OVER keeps every row.
  3. It adds the group total as a column on each row.

Why the other options are wrong

  • A. Window functions run after GROUP BY, not before.
  • C. PARTITION BY is the main feature of windows.
  • D. They are allowed in SELECT and ORDER BY, not WHERE.

Tip: Window = keeps the rows.

Question 2Easy

What does PARTITION BY do in a window specification?

  1. It splits rows into groups so the window function restarts for each group
  2. It removes duplicate rows
  3. It physically partitions the table on disk
  4. It sorts the final output

Answer: A. It splits rows into groups so the window function restarts for each group

Explanation

For example ROW_NUMBER() OVER (PARTITION BY team ORDER BY runs DESC) numbers players separately inside each team.

Step-by-step solution

  1. PARTITION BY splits the rows into groups.
  2. The window function (such as ROW_NUMBER) restarts for each group.
  3. So option A is correct.

Why the other options are wrong

  • B. Removing duplicates is DISTINCT, not PARTITION BY.
  • C. PARTITION BY is a logical grouping of rows in a query, not disk storage.
  • D. Sorting the output is ORDER BY.

Tip: PARTITION BY gives each group its own window; ORDER BY sets the order inside each window.

Question 3Easy

What does this query return for Deepa?

scores
player | team | runs
Arun   | Red  | 50
Bhavna | Red  | 70
Chris  | Red  | 70
Deepa  | Blue | 40
Eshan  | Blue | 90
Fiza   | Blue | NULL
SELECT player, SUM(runs) OVER (PARTITION BY team) AS team_total
FROM scores
WHERE player = 'Deepa';
  1. NULL
  2. 130
  3. 230
  4. 40

Answer: D. 40

Explanation

The window is computed after WHERE, so the partition contains only Deepa’s row. Without the filter the Blue total would be 130.

Step-by-step solution

  1. WHERE is applied before window functions.
  2. Only Deepa's row survives, so the Blue partition has just 40.
  3. SUM = 40.

Why the other options are wrong

  • A. There is a value to sum: 40.
  • B. 130 would be the total only if the other Blue rows were kept.
  • C. 230 mixes the Red team.

Tip: Filter first, window after.

Question 4Easy

What values does nxt contain, in order of day?

daily
day | amount
1   | 10
2   | 30
3   | 20
4   | NULL
5   | 40
SELECT day, LEAD(amount) OVER (ORDER BY day) AS nxt
FROM daily;
  1. 30, 20, NULL, 40, NULL
  2. 10, 30, 20, NULL, 40
  3. 30, 20, NULL, 40, 0
  4. 30, 20, 40, 40, NULL

Answer: A. 30, 20, NULL, 40, NULL

Explanation

LEAD looks one row ahead; day 3 sees day 4’s NULL and the last row has no next row.

Step-by-step solution

  1. LEAD looks at the next row.
  2. Days 1-5 give 30, 20, NULL, 40.
  3. The last row has no next row: NULL.

Why the other options are wrong

  • B. That is the current value (the amount itself).
  • C. The last row gives NULL, not 0, without a default.
  • D. Values are not repeated.

Tip: LEAD = next, LAG = previous.

Question 5Medium

What values does diff contain, in order of day?

daily
day | amount
1   | 10
2   | 30
3   | 20
4   | NULL
5   | 40
SELECT day, amount - LAG(amount) OVER (ORDER BY day) AS diff
FROM daily
WHERE amount IS NOT NULL;
  1. 0, 20, -10, 20
  2. NULL, 20, -10, NULL
  3. NULL, 20, -10, 20
  4. NULL, 20, -10, 40

Answer: C. NULL, 20, -10, 20

Explanation

WHERE removes day 4 before the window is computed, so day 5’s previous row is day 3 (40 - 20 = 20).

Step-by-step solution

  1. WHERE removes day 4 first; remaining rows are days 1, 2, 3 and 5.
  2. Day 1 has no previous row, so NULL. Day 2: 30 − 10 = 20. Day 3: 20 − 30 = −10.
  3. Day 5's previous row is day 3 (20), so 40 − 20 = 20. Result: NULL, 20, −10, 20 (option C).

Why the other options are wrong

  • A. Day 1 has no previous row, so it is NULL, not 0.
  • B. Day 5 is not NULL; day 4 is removed by WHERE before LAG runs.
  • D. Day 5 subtracts day 3's 20, so the difference is 20, not 40.

Tip: WHERE runs before window functions, so filtered rows are skipped by LAG.

Question 6Medium

What rank does Fiza (NULL runs) get here (SQLite)?

scores
player | team | runs
Arun   | Red  | 50
Bhavna | Red  | 70
Chris  | Red  | 70
Deepa  | Blue | 40
Eshan  | Blue | 90
Fiza   | Blue | NULL
SELECT player, RANK() OVER (ORDER BY runs DESC) AS r
FROM scores
WHERE player = 'Fiza' OR 1 = 1;
  1. 1
  2. 6
  3. 5
  4. NULL

Answer: B. 6

Explanation

SQLite treats NULL as the smallest value, so in DESC order it comes last and gets rank 6. (In PostgreSQL/Oracle NULLs sort first in DESC.)

Step-by-step solution

  1. In SQLite NULL is the smallest value.
  2. In DESC order NULL goes last.
  3. Five non-NULL values rank before it, so Fiza is rank 6.

Why the other options are wrong

  • A. NULL is last here, not first.
  • C. Two players tie at 70, so the next rank is skipped; Fiza is 6th.
  • D. RANK always returns a number.

Tip: SQLite: NULL is smallest.

Question 7Medium

How many rows does this query return?

scores
player | team | runs
Arun   | Red  | 50
Bhavna | Red  | 70
Chris  | Red  | 70
Deepa  | Blue | 40
Eshan  | Blue | 90
Fiza   | Blue | NULL
SELECT team, COUNT(*) OVER (PARTITION BY team) AS n
FROM scores;
  1. 2
  2. 6
  3. 5
  4. 3

Answer: B. 6

Explanation

Unlike GROUP BY, a window function keeps every input row: all 6 players are returned, each with their team size.

Step-by-step solution

  1. A window function keeps every input row, unlike GROUP BY.
  2. COUNT(*) OVER (PARTITION BY team) gives each row its team size.
  3. All 6 players are returned, so the answer is 6 (option B).

Why the other options are wrong

  • A. 2 is the number of teams, but a window does not collapse rows to teams.
  • C. 5 is not the number of rows in the table.
  • D. 3 is one team's size, but all 6 rows are returned.

Tip: A window aggregate keeps the row count; GROUP BY reduces it.

Question 8Medium

What values does fv contain, in order of sal?

emp
name | sal
A    | 100
B    | 200
C    | 300
SELECT name, FIRST_VALUE(name) OVER (ORDER BY sal DESC) AS fv
FROM emp
ORDER BY sal;
  1. C, B, A
  2. A, A, A
  3. C, C, C
  4. A, B, C

Answer: C. C, C, C

Explanation

The window is ordered by salary descending, so the first row of every frame is C (300). The final ORDER BY only changes display order.

Step-by-step solution

  1. The window is ordered by sal DESC, so the first row of each frame is the highest salary.
  2. The highest salary is C (300), and the frame always starts at that row.
  3. Every row shows C: C, C, C (option C).

Why the other options are wrong

  • A. C, B, A would be a running list, not FIRST_VALUE; the first value never changes.
  • B. A is the lowest salary, so it is last in DESC order and cannot be the first value.
  • D. A, B, C is not the output of FIRST_VALUE in DESC order.

Tip: FIRST_VALUE returns the first row in the window order, so reverse the ORDER BY to pick the max or min.

Question 9Hard

What does this query return? (columns: name, top)

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT name,
       MAX(salary) OVER (PARTITION BY dept) AS top
FROM employees
WHERE name = 'Ravi';
  1. Ravi, 45000
  2. Ravi, 70000
  3. An error
  4. Ravi, 60000

Answer: A. Ravi, 45000

Explanation

WHERE is applied before window functions are computed. Only Ravi's row is left in the Sales partition, so the maximum is his own salary.

Step-by-step solution

  1. WHERE name = 'Ravi' runs first, leaving only Ravi's row.
  2. The window then sees only Ravi in the Sales partition.
  3. MAX is his own salary, so the result is Ravi, 45000 (option A).

Why the other options are wrong

  • B. 70000 is John's salary from the IT department, not Ravi's partition.
  • C. The query is valid; WHERE before window functions is allowed.
  • D. 60000 is Asha's salary, and Asha is filtered out by WHERE.

Tip: WHERE filters before window functions, so a window sees only the rows that pass WHERE.

Question 10Hard

What happens when this query runs?

logins
uid | d
1   | 1
1   | 2
1   | 3
1   | 5
1   | 6
SELECT d - ROW_NUMBER() OVER (ORDER BY d) AS grp, COUNT(*)
FROM logins
GROUP BY grp;
  1. 1
  2. It fails: a window function cannot be used inside GROUP BY in the same query level
  3. 2
  4. 5

Answer: B. It fails: a window function cannot be used inside GROUP BY in the same query level

Explanation

Window functions are evaluated after GROUP BY, so a grouping key cannot depend on one at the same level. Compute it in a subquery first (the "gaps and islands" trick).

Step-by-step solution

  1. GROUP BY grp uses an alias that is a window function, computed after grouping.
  2. A grouping key cannot depend on a window function in the same query level.
  3. The query fails with an error (option B).

Why the other options are wrong

  • A. The query does not return a count; it fails before grouping.
  • C. The query does not return a count of 2; it fails.
  • D. The query does not return 5; it fails before any count is made.

Tip: For runs of consecutive values, compute the window in a subquery, then GROUP BY its column in the outer query.

Practise all 41 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics