GATE CS 2006 · SQL Queries

MCQ · 1 mark · Problem DBMS-SQL-0129

Consider the relation account (customer, balance) where customer is a primary key and there are no null values. We would like to rank customers according to decreasing balance. The customer with the largest balance gets rank 1. Ties are not broken but ranks are skipped: if exactly two customers have the largest balance they each get rank 1 and rank 2 is not assigned.Query 1:select A.customer, count(B.customer) from account A, account B where A.balance <= B.balance group by A.customerQuery 2:select A.customer, 1 + count(B.customer) from account A, account B where A.balance < B.balance group by A.customerConsider these statements about Query 1 and Query 2.1. Query 1 will produce the same row set as Query 2 for some but not all databases.2. Both Query 1 and Query 2 are correct implementations of the specification.3. Query 1 is a correct implementation of the specification but Query 2 is not.4. Neither Query 1 nor Query 2 is a correct implementation of the specification.5. Assigning rank with a pure relational query takes less time than scanning in decreasing balance order and then assigning ranks using ODBC.Which two of the above statements are correct?
  1. A
    2 and 5
  2. B
    1 and 3
  3. C
    1 and 4
  4. D
    3 and 5

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solution\[\boxed{\text{rank} = 1 + \#\{\text{customers with a strictly larger balance}\}}\]Query 1 counts the customers with balance \(\ge\) A's, which includes A and everyone tied with A: two customers tied at the top both get 2 instead of 1 — wrong.Query 2 computes \(1 + \#\{\text{larger}\}\) correctly, but a customer with the largest balance has no B with a larger balance, so the join drops that customer and the top customers never appear — wrong. So statement 4 is true and 2, 3 are false.The two queries give the same row set only for special databases (for example an empty account table), not in general, so statement 1 is true.Statement 5 is false: the self-join is quadratic, while one sorted scan assigns ranks in a single pass.Answer: C

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com