Window Functions · Question 15 · Medium

Window Functions question 15

Table Scores:
player | team | runs
Arjun | Royals | 82
Bhuvan | Royals | 82
Chandan | Royals | 60
Dhawal | Titans | 95
Eknath | Titans | 40
Faisal | Titans | 71

What does this query return (player, rk, drk)?

SELECT player, rk, drk
FROM (SELECT player,
             RANK() OVER (ORDER BY runs DESC) AS rk,
             DENSE_RANK() OVER (ORDER BY runs DESC) AS drk
      FROM Scores) t
WHERE player IN ('Chandan', 'Faisal')
ORDER BY player;
  1. Chandan, 4, 5; Faisal, 3, 4
  2. Chandan, 5, 4; Faisal, 4, 3
  3. Chandan, 5, 5; Faisal, 4, 4
  4. Chandan, 2, 2; Faisal, 1, 1
Answer and explanation

Answer: B. Chandan, 5, 4; Faisal, 4, 3

Explanation: Ranks are computed over all 6 rows before the outer WHERE: 95 is 1, the two 82s are both 2. RANK then skips to 4 for Faisal (71) and 5 for Chandan (60), while DENSE_RANK continues with 3 and 4.

Previous: Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82… · Next: Table Deposits: dep_id | day_no | amount 1 | 1 | 100 2 | 1 | 200 3 | 2 | 50 4…

Practise window functions

Try a fresh set of window functions questions with an explanation after every answer, free and without signing up: Window Functions practice. Or sit a full placement mock test.

More window functions questions

More sql practice

NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge