GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 67

Ranking balances with self-joins

Database Management Systems · SQL & Queries · Full GATE CSE 2006 paper

GATE 2006, Q67 2 marks · MCQ
Report issue

SQL & Queries

Question 67, GATE 2006: Relation account(customer,balance) has customer as primary key and no nulls. Rank by decreasing balance, starting at 1, tying equal balances and skipping ranks (two tied maxima both rank 1, next rank 3).
Q1: SELECT A.customer,COUNT(B.customer) FROM account A,account B WHERE A.balance<=B.balance GROUP BY A.customer;
Q2: SELECT A.customer,1+COUNT(B.customer) FROM account A,account B WHERE A.balance<B.balance GROUP BY A.customer;
Statements: 1 Q1 and Q2 agree for some but not all databases; 2 both implement the rank specification; 3 Q1 is correct but Q2 is not; 4 neither is correct; 5 pure relational ranking is faster than scanning in decreasing balance order and assigning ranks through ODBC. Which pair is correct?
Options:
(A) 2 and 5
(B) 1 and 3
(C) 1 and 4
(D) 3 and 5
Question 67, GATE 2006: Relation account(customer,balance) has customer as primary key and no nulls. Rank by decreasing balance, starting at 1, tying equal balances and skipping ranks (two tied maxima both rank 1, next rank 3).
Q1: SELECT A.customer,COUNT(B.customer) FROM account A,account B WHERE A.balance<=B.balance GROUP BY A.customer;
Q2: SELECT A.customer,1+COUNT(B.customer) FROM account A,account B WHERE A.balance<B.balance GROUP BY A.customer;
Statements: 1 Q1 and Q2 agree for some but not all databases; 2 both implement the rank specification; 3 Q1 is correct but Q2 is not; 4 neither is correct; 5 pure relational ranking is faster than scanning in decreasing balance order and assigning ranks through ODBC. Which pair is correct?
Options:
(A) 2 and 5
(B) 1 and 3
(C) 1 and 4
(D) 3 and 5
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Query1 counts B rows with A.balance≤B.balance, so a customer tied with t customers at its balance receives the count of higher-balance customers plus t, instead of plus 1. For two tied maximum balances it gives 2, not 1.

2. Query2 uses A.balance<B.balance and adds 1 to the count, but its inner join produces no group for maximum-balance customers; those customers disappear. Thus neither query is correct (statement 4).

3. They agree on the empty database but differ on every nonempty database because Query1 includes the maximum group and Query2 does not, establishing statement 1 under the allowed empty-database case. Hence statements 1 and 4, option C.

4. Statement 5 is not a general performance guarantee.

5. Teaching check: The intended rank is 1 plus the number of strictly higher balances. Q1 instead counts all tied customers along with higher ones. Q2 uses the correct comparison but an inner join removes maxima, so adding one to its count does not restore the missing rows.

Original paper, page 10