03:06

# Exercise #24 page 1

Find the model number of the product (PC, laptop, or printer) with the highest price.Result set: model

Below is erroneous solution that has been admitted by site's checking system some time ago.

Solution 1.16.1

Console
Execute
`SELECT  model FROM (SELECT  model, price       FROM PC      WHERE price = (SELECT MAX(price)                      FROM PC                     )      UNION      SELECT  model,  price       FROM Laptop      WHERE price = (SELECT MAX(price)                      FROM Laptop                     )      UNION      SELECT  model,  price       FROM Printer      WHERE price = (SELECT MAX(price)                      FROM Printer                     )      ) TWHERE price = (SELECT MAX(price)                FROM Laptop               );`

I suggest to clear up what data must be in the test database to block such solutions.

So, what does this query do? In each of three similar subqueries models from three types of products - PC, PC-laptop or printer - are selected by maximum price. Then UNION is used to combine the found models and besides to remove duplicated rows {model, price}. At last, the models having the same price as the maximum price for PC-laptops are left.

So if the maximum price over all products coincides with the maximum price on printers this solution will be rejected by the system. But then a solution having

`WHERE price = (SELECT MAX(price)                FROM Printer               )`
as a last string will be taken. What's more, if the maximum price will be only for one type of products (printers, for instance), even more incorrect solution will be passed through checking:

Solution 1.16.2

Console
Execute
`SELECT   DISTINCT  model FROM PrinterWHERE price = (SELECT MAX(price)                FROM Printer               );`

Conclusion. Whatever the data may be, with the help of the first query the solution can be adjusted in three attempts, in the worst case. The second solution won't pass at all, if the maximum is reached for at least two types of products. But then for adjusting the first solution we'll need only two attempts. If in every type of product there is a model with the same maximum price, one try will be enough.

By the way, the data is picked up optimally for the examined cases, but, anyway, it doesn't prevent from the wrong queries' passage.

Way out of this situation, and not only this, can be found in increasing the amount of test databases, where different data variants will be simulated. The only thing that prevents me from doing this is slowing down the system's work, which will cause the user to spend more time waiting. I also comfort myself by a thought that our visitors' motivation is SQL study and qualification improvement but not a tendency to fool the system.

 Pages 1 2