Window Functions questions and answers

ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals. Practise window functions questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample window functions questions with worked answers

Question 1

Three students are tied on the highest marks. Which function, used with OVER (ORDER BY marks DESC), still gives them the three different numbers 1, 2 and 3?

  1. RANK()
  2. DENSE_RANK()
  3. ROW_NUMBER()
  4. COUNT(*)

Answer: C. ROW_NUMBER()

Explanation: ROW_NUMBER() numbers rows 1, 2, 3, ... with no repeats, breaking ties arbitrarily. RANK() and DENSE_RANK() would give all three tied students the number 1.

Question 2

Table Results:
student | section | marks
Aditi | A | 92
Bharti | A | 92
Chetna | A | 85
Divya | B | 75
Ekta | B | 81
Fatima | B | 78

What does this query return?

SELECT student,
       DENSE_RANK() OVER (PARTITION BY section
                          ORDER BY marks DESC)
FROM Results
ORDER BY student;
  1. Aditi, 1; Bharti, 1; Chetna, 3; Divya, 3; Ekta, 1; Fatima, 2
  2. Aditi, 1; Bharti, 1; Chetna, 2; Divya, 5; Ekta, 3; Fatima, 4
  3. Aditi, 1; Bharti, 2; Chetna, 3; Divya, 3; Ekta, 1; Fatima, 2
  4. Aditi, 1; Bharti, 1; Chetna, 2; Divya, 3; Ekta, 1; Fatima, 2

Answer: D. Aditi, 1; Bharti, 1; Chetna, 2; Divya, 3; Ekta, 1; Fatima, 2

Explanation: Ranking restarts in each section. In A, the two 92s share rank 1 and DENSE_RANK leaves no gap, so Chetna is 2. In B the order is Ekta 81, Fatima 78, Divya 75, giving 1, 2, 3.

Question 3

Table Deposits:
dep_id | day_no | amount
1 | 1 | 100
2 | 1 | 200
3 | 2 | 50
4 | 3 | 400

Two deposits were made on day 1. What does this query return (dep_id, total)?

SELECT dep_id,
       SUM(amount) OVER (ORDER BY day_no)
FROM Deposits
ORDER BY dep_id;
  1. 1, 100; 2, 300; 3, 350; 4, 750
  2. 1, 750; 2, 750; 3, 750; 4, 750
  3. 1, 300; 2, 300; 3, 50; 4, 400
  4. 1, 300; 2, 300; 3, 350; 4, 750

Answer: D. 1, 300; 2, 300; 3, 350; 4, 750

Explanation: With ORDER BY and no frame clause, the default frame runs from the start up to the current row and all rows tied with it on the ORDER BY value. Both day-1 rows are peers, so each gets 100 + 200 = 300. Adding ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (with a tie-breaking sort key) would give 100 and 300.

All 20 window functions questions with answers

Window Functions: common questions

What are window functions questions?

ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals. They appear in the sql section of many placement tests.

Where can I practise window functions questions with answers for free?

Right here on LogicWiz Exams. Every window functions question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise window functions?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of window functions questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep