CS FundamentalsVery hardAmazon · Microsoft MCQ pattern

Table Sales(sid, pid, qty, saledate, cust) rows: (101, 1, 10, ...), (102, 3, 1, ...), (103, 2, 5, ...), (104, 5, 2, ...), (105, 4, 3, ...), (106, 7, 20, ...), (107, 3, 1, ...), where qty values are 10, 1, 5, 2, 3, 20, 1 respectively (sid 102 and sid 107 are tied at qty = 1, the lowest). For sid 103 (qty = 5), what do RANK() and DENSERANK() return?

Table Sales(sid, pid, qty, saledate, cust) rows: (101, 1, 10, ...), (102, 3, 1, ...), (103, 2, 5, ...), (104, 5, 2, ...), (105, 4, 3, ...), (106, 7, 20, ...), (107, 3, 1, ...), where qty values are 10, 1, 5, 2, 3, 20, 1 respectively (sid 102 and sid 107 are tied at qty = 1, the lowest). For sid 103 (qty = 5), what do RANK() and DENSE_RANK() return?
SELECT sid, qty,
       RANK()       OVER (ORDER BY qty ASC) AS rk,
       DENSE_RANK() OVER (ORDER BY qty ASC) AS drk
FROM Sales;
  1. A. rk = 4, drk = 4
  2. B. rk = 5, drk = 5
  3. C. rk = 4, drk = 3
  4. D. rk = 5, drk = 4

Correct answer: D. rk = 5, drk = 4

Ascending order: 1,1 (tied), 2, 3, 5, 10, 20. RANK assigns the tied pair both rank 1, then SKIPS to rank 3 for qty=2 (since two rows occupied ranks 1-2): qty=2 is rank 3, qty=3 is rank 4, qty=5 is rank 5. DENSE_RANK does not skip: 1→1, 2→2, 3→3, 5→4. So sid 103 (qty=5) has rk=5, drk=4.

More DBMS and SQL questions

Exams that ask this topic