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;- A. rk = 4, drk = 4
- B. rk = 5, drk = 5
- C. rk = 4, drk = 3
- 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
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Sales(sid, pid, qty, saledate, cust) rows: (101, 1, 10, '2024-01-05', 'Asha'), (102, 3, 1, '2024-01-06', 'Ravi'), (103, 2, 5…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…