Window Functions · Question 13 · Medium

Window Functions question 13

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, gap)?

SELECT player,
       MAX(runs) OVER (PARTITION BY team) - runs
FROM Scores
ORDER BY player;
  1. Arjun, 0; Bhuvan, 0; Chandan, 22; Dhawal, 0; Eknath, 55; Faisal, 24
  2. Arjun, 13; Bhuvan, 13; Chandan, 35; Dhawal, 0; Eknath, 55; Faisal, 24
  3. Chandan, 22; Eknath, 55; Faisal, 24
  4. Arjun, 82; Bhuvan, 82; Chandan, 82; Dhawal, 95; Eknath, 95; Faisal, 95
Answer and explanation

Answer: A. Arjun, 0; Bhuvan, 0; Chandan, 22; Dhawal, 0; Eknath, 55; Faisal, 24

Explanation: MAX(runs) OVER (PARTITION BY team) puts the team's best score on every row: 82 for Royals and 95 for Titans. Subtracting each player's own runs gives the gap to the team's top scorer, and all 6 rows are kept.

Previous: A table Scores(player, team, runs) is given. Why is the following query… · Next: Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…

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