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?
- RANK()
- DENSE_RANK()
- ROW_NUMBER()
- 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;
- Aditi, 1; Bharti, 1; Chetna, 3; Divya, 3; Ekta, 1; Fatima, 2
- Aditi, 1; Bharti, 1; Chetna, 2; Divya, 5; Ekta, 3; Fatima, 4
- Aditi, 1; Bharti, 2; Chetna, 3; Divya, 3; Ekta, 1; Fatima, 2
- 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, 100; 2, 300; 3, 350; 4, 750
- 1, 750; 2, 750; 3, 750; 4, 750
- 1, 300; 2, 300; 3, 50; 4, 400
- 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
- Three students are tied on the highest marks. Which function, used with OVER…
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- What does PARTITION BY do inside an OVER clause?
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table Results: student | section | marks Aditi | A | 92 Bharti | A | 92 Chetna…
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…
- Table Wallet: txn_id | holder | amount 1 | Tara | 500 2 | Uday | 200 3 | Tara…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- A table Scores(player, team, runs) is given. Why is the following query…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table Deposits: dep_id | day_no | amount 1 | 1 | 100 2 | 1 | 200 3 | 2 | 50 4…
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…
- Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 No frame…
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
- SELECT, WHERE & ORDER BY: Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT.
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Aggregates & GROUP BY: COUNT, SUM, AVG, grouping and HAVING versus WHERE.
- Subqueries & CTEs: Scalar and correlated subqueries, IN, EXISTS and WITH.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- UNION, INTERSECT & EXCEPT: Combining results, duplicates and column rules.
- Tables, Keys & Constraints: CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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