Window Functions · Question 11 · Medium

Window Functions question 11

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

Which players does this query return?

SELECT player
FROM (SELECT player,
             ROW_NUMBER() OVER (PARTITION BY team
                                ORDER BY runs DESC, player) AS rn
      FROM Scores) t
WHERE rn = 1
ORDER BY player;
  1. Arjun; Bhuvan; Dhawal
  2. Arjun; Dhawal
  3. Dhawal
  4. Arjun; Bhuvan
Answer and explanation

Answer: B. Arjun; Dhawal

Explanation: ROW_NUMBER gives exactly one row per team the number 1. In Royals, Arjun and Bhuvan tie on 82 and the second sort key, player, puts Arjun first. In Titans, Dhawal (95) is first.

Previous: Table Wallet: txn_id | holder | amount 1 | Tara | 500 2 | Uday | 200 3 | Tara… · Next: A table Scores(player, team, runs) is given. Why is the following query…

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