Window Functions · Question 2 · Easy

Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does this query return (day_no, row number)?

SELECT day_no,
       ROW_NUMBER() OVER (ORDER BY amount DESC)
FROM DailySales
ORDER BY day_no;
  1. 1, 4; 2, 2; 3, 3; 4, 1
  2. 1, 1; 2, 2; 3, 3; 4, 4
  3. 1, 1; 2, 3; 3, 2; 4, 4
  4. 4, 1; 2, 2; 3, 3; 1, 4
Answer and explanation

Answer: A. 1, 4; 2, 2; 3, 3; 4, 1

Explanation: The numbering follows the ORDER BY inside OVER: 300 (day 4) gets 1, 250 (day 2) gets 2, 150 (day 3) gets 3 and 100 (day 1) gets 4. The outer ORDER BY day_no only decides the order in which rows are shown.

Previous: Three students are tied on the highest marks. Which function, used with OVER… · Next: Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…

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