Question discussion

What is the output (each name followed by its rank) of the following SQL statements?CREATE TABLE e(name TEXT, sal INT); INSERT INTO e VALUE…

2153Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output (each name followed by its rank) of the following SQL statements?

CREATE TABLE e(name TEXT, sal INT);
INSERT INTO e VALUES ('A',50),('B',40),('C',40),('D',30);
SELECT name, RANK() OVER (ORDER BY sal DESC) AS r FROM e ORDER BY r, name;
  • AA 1, B 2, C 3, D 4
  • BA 1, B 2, C 2, D 3
  • CA 1, B 2, C 2, D 4
  • DA 1, B 1, C 1, D 4
Correct answer: C

Explanation

RANK() gives tied rows the same rank and then skips numbers: B and C are both rank 2 and D becomes rank 4 (DENSE_RANK would give 3).

Discussion 0

Nobody has replied yet. Start the discussion!