CS FundamentalsHardTCS NQT · Infosys interview pattern

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). For the order with id 1, what are the values of r and dr?

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). For the order with id 1, what are the values of r and dr?
SELECT id,
       RANK()       OVER (ORDER BY amount DESC) AS r,
       DENSE_RANK() OVER (ORDER BY amount DESC) AS dr
FROM Orders WHERE amount IS NOT NULL;
  1. A. r = 2, dr = 2
  2. B. r = 3, dr = 3
  3. C. r = 3, dr = 2
  4. D. r = 2, dr = 3

Correct answer: C. r = 3, dr = 2

Amounts in order: 700, 700, 500, … The two 700s share rank 1. RANK leaves a gap, so 500 gets rank 3; DENSE_RANK does not, so 500 gets 2. ROW_NUMBER would give 3 as well, but without ties.

More DBMS and SQL questions

Exams that ask this topic