LearnFrenzy
Sign in Take a free test

DBMS & SQL · Topic 4 of 10

Window Functions: questions with answers

ROW_NUMBER, RANK, DENSE_RANK, NTILE, LAG and LEAD, running totals, frames, top-N per group and gaps and islands.

Questions
45
Timed sets
3
Queries executed
35
Your accuracy
–

What this topic covers

31 subtopics
  • OVER ()3
  • Where windows can be used3
  • De-duplication2
  • Frames and ties2
  • LAG2
  • LAG with PARTITION2
  • Logical order2
  • NTILE2
  • PARTITION BY2
  • ROW_NUMBER2
  • Running total2
  • Top-N per group2
Show all 31 subtopics
  • Choosing a ranking function1
  • CUME_DIST1
  • DENSE_RANK1
  • DENSE_RANK with PARTITION1
  • FIRST_VALUE1
  • Frames1
  • Gaps and islands1
  • LAST_VALUE1
  • LEAD1
  • Moving average1
  • Nesting rules1
  • Nth highest per group1
  • Percent of total1
  • PERCENT_RANK1
  • Performance1
  • RANK1
  • RANK vs DENSE_RANK1
  • Window basics1
  • Windows over GROUP BY1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Window functions compute a value for each row from a set of related rows – the window – without collapsing rows the way GROUP BY does. They power rankings, top-N per group, running totals, period-over-period comparisons and de-duplication, and they are a favourite of data-analyst and backend interviews. Watch the details: ties in ranking functions, the default frame of an ordered window, and the fact that WHERE runs before any window is computed.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Window basics Beginner Practice

What is the key difference between AVG(marks) OVER (PARTITION BY subject) and AVG(marks) … GROUP BY subject?

GROUP BY keeps every row
The window version cannot use AVG
The window version keeps every row and adds the average to each; GROUP BY returns one row per subject
They return exactly the same rows
Show answer and explanation
Correct answer C

Explanation

A window function does not collapse rows. Each row sees an aggregate computed over its window (here, its subject).

Why the other options are wrong

  • AThat is what OVER does, not GROUP BY.
  • BAny aggregate can be used as a window function.
  • DGROUP BY collapses each subject to one row.
Question 2 RANK Beginner Practice Query executed

What does RANK give for Maths?

scores
idstudentsubjectmarks
1AshaMaths92
2RaviMaths85
3MeenaMaths92
4JohnMaths78
5AshaPhysics88
6RaviPhysics90
7MeenaPhysics75
8JohnPhysics90
SELECT student, RANK() OVER (ORDER BY marks DESC) AS r
FROM scores
WHERE subject = 'Maths'
ORDER BY r, student;
studentr
Asha1
Meena1
Ravi3
John4
studentr
Asha1
Meena1
Ravi2
John3
studentr
Asha1
Meena1
Ravi2
John4
studentr
Asha1
Meena2
Ravi3
John4
Show answer and explanation
Correct answer A

Explanation

RANK gives tied rows the same rank and then skips: two students share 1st, so the next rank is 3.

Why the other options are wrong

  • BRanks without gaps come from DENSE_RANK.
  • CAfter rank 3 the next distinct value gets 4, not a mix.
  • DUnique numbers come from ROW_NUMBER.
Question 3 Top-N per group Beginner Practice Query executed

What does this "top scorer per subject" query return?

scores
idstudentsubjectmarks
1AshaMaths92
2RaviMaths85
3MeenaMaths92
4JohnMaths78
5AshaPhysics88
6RaviPhysics90
7MeenaPhysics75
8JohnPhysics90
SELECT subject, student FROM (
  SELECT subject, student,
    ROW_NUMBER() OVER (PARTITION BY subject ORDER BY marks DESC, id) AS rn
  FROM scores
) t
WHERE rn = 1
ORDER BY subject;
subjectstudent
MathsAsha
subjectstudent
MathsAsha
PhysicsJohn
subjectstudent
MathsAsha
MathsMeena
PhysicsRavi
PhysicsJohn
subjectstudent
MathsAsha
PhysicsRavi
Show answer and explanation
Correct answer D

Explanation

ROW_NUMBER restarts per subject. In Physics, Ravi (id 6) and John (id 8) tie on 90; the id tie-breaker makes Ravi number 1. ROW_NUMBER always picks exactly one row per group.

Why the other options are wrong

  • AEach subject is a separate partition.
  • BRavi has the lower id.
  • CKeeping all ties needs RANK.
Question 4 Logical order Intermediate Practice Query executed

WHERE runs before window functions. What does this return?

scores
idstudentsubjectmarks
1AshaMaths92
2RaviMaths85
3MeenaMaths92
4JohnMaths78
5AshaPhysics88
6RaviPhysics90
7MeenaPhysics75
8JohnPhysics90
SELECT student, subject, AVG(marks) OVER (PARTITION BY subject) AS subject_avg
FROM scores
WHERE student = 'Asha'
ORDER BY subject;
studentsubjectsubject_avg
AshaMaths86.25
AshaPhysics86.25
studentsubjectsubject_avg
AshaMaths86.75
AshaPhysics85.75
studentsubjectsubject_avg
AshaMaths92
AshaPhysics88
studentsubjectsubject_avg
AshaMaths90
AshaPhysics90
Show answer and explanation
Correct answer C

Explanation

The WHERE keeps only Asha's two rows, so each partition contains just her own mark and the "average" is that mark. To compare her with the class, compute the window in a subquery and filter outside.

Why the other options are wrong

  • A86.25 is the average of all eight scores.
  • BThose class averages are computed only if the other rows survive the WHERE.
  • D90 is her overall average, but the window is partitioned by subject.
Question 5 Nth highest per group Advanced Practice Query executed

What does this "second-highest mark per subject" query return?

scores
idstudentsubjectmarks
1AshaMaths92
2RaviMaths85
3MeenaMaths92
4JohnMaths78
5AshaPhysics88
6RaviPhysics90
7MeenaPhysics75
8JohnPhysics90
SELECT subject, student FROM (
  SELECT subject, student, DENSE_RANK() OVER (PARTITION BY subject ORDER BY marks DESC) AS r
  FROM scores
) t
WHERE r = 2
ORDER BY subject, student;
subjectstudent
MathsRavi
PhysicsAsha
subjectstudent
MathsRavi
PhysicsJohn
subjectstudent
MathsMeena
PhysicsJohn
subjectstudent
PhysicsAsha
Show answer and explanation
Correct answer A

Explanation

DENSE_RANK treats the tied toppers as rank 1, so the second-highest mark gets rank 2: 85 in Maths (Ravi) and 88 in Physics (Asha). With RANK, Maths would have no rank 2 at all.

Why the other options are wrong

  • BJohn is tied for first in Physics.
  • CMeena and John share the highest marks.
  • DMaths also has a second-highest mark.
40 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DBMS & SQL

All 10 topics

Topics, levels and full mocks
  1. 01 DBMS and SQL Fundamentals45 questions · 3 levels
  2. 02 Joins and Subqueries45 questions · 3 levels
  3. 03 Aggregates and GROUP BY45 questions · 3 levels
  4. 04 Window Functions45 questions · 3 levels
  5. 05 Keys and Constraints45 questions · 3 levels
  6. 06 ER Model and Relational Mapping45 questions · 3 levels
  7. 07 Normalization45 questions · 3 levels
  8. 08 Transactions and Concurrency45 questions · 3 levels
  9. 09 Indexing and Query Optimisation45 questions · 3 levels
  10. 10 Advanced SQL Queries45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links