Consider the relation account (customer, balance) where the 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 Ties are not broke but ranks are skipped: if exactly two customers have the largest balance they each get rank and rank is not assigned.
select A.customer, count(B.customer) from account A, account B where A.balance <=B.balance group by A.customer
select A.customer, 1+count(B.customer) from account A, account B where A.balance < B.balance group by A.customer
Consider these statements about Query and Query
- Query will produce the same row set as Query for some but not all databases.
- Both Query and Query are a correct implementation of the specification
- Query is a correct implementation of the specification but Query is not
- Neither Query nor Query is a correct implementation of the specification
- Assigning rank with a pure relational query takes less time than scanning in decreasing balance order assigning ranks using ODBC.
Which two of the above statements are correct?
View worked solution
Answer: C
To determine which statements are correct, let us analyze both queries with a sample database of three customers:
- with balance
- with balance
- with balance
According to the specification, the correct ranks should be: , , and (rank 2 is skipped).
1. Analysis of Query 1
Query 1 uses the join condition A.balance <= B.balance:
- For (): . Count is .
- For (): . Count is .
- For (): . Count is .
The output ranks are , , . This is incorrect because the top rank is instead of . Thus, Query 1 is incorrect.
2. Analysis of Query 2
Query 2 uses the join condition A.balance < B.balance:
Because of the inner join, any customer who has the absolute maximum balance will find no customer with a strictly greater balance (i.e., A.balance < B.balance is false for all ). Consequently, the maximum balance customer(s) are completely filtered out and omitted from the final output. Thus, Query 2 is also incorrect.
Since neither query is correct, Statement 4 is correct.
3. Analysis of Statement 1
- If the database is empty, both Query 1 and Query 2 produce an empty result set (they are identical).
- For any non-empty database, Query 1 will output the maximum balance customer(s), whereas Query 2 will omit them. Thus, their outputs will differ.
Hence, Query 1 produces the same row set as Query 2 for some (empty) but not all databases. This makes Statement 1 correct.
Therefore, statements 1 and 4 are correct, matching Option C.
Free practice · Your answers on this page are not saved.
