loading..
Русский    English
15:31

Exercise #18 (tips and solutions)

In the following solution

Console
Execute
  1. SELECT c.maker, a.priceA price
  2. FROM (SELECT MIN(price) priceA
  3. FROM Printer
  4. WHERE Color ='y'
  5. ) a INNER JOIN
  6. Printer b ON a.priceA = b.price INNER JOIN
  7. Product c ON b.model = c.model;
subquery defines lowest price for color printers with subsequent joining with Printer table on this price to find all printers having such a price. Finally, joining with Product table gives us the makers of these printers.

Surely, joining on price might be replaced by simple comparison:

  1. WHERE price = (SELECT MIN(price) priceA
  2. FROM Printer
  3. WHERE Color ='y'
  4. )

A mistake in the query is not related to said above, but to the fact that the solution determines ANY printers which have price coincident with a minimal price for color printers. Database includes corresponding items. As a result, we obtain

maker price
D 270.0
A 270.0

Meanwhile, correct answer gives only one row:

maker price
D 270.0

Second row is not related to color printer. You can be easyly convinced of it if you'll include additional columns in SELECT clause:

  1. SELECT c.maker, a.priceA price, color, b.type
which give

maker price color type
D 270.0 y Jet
A 270.0 n Matrix

Second mistake is in possible duplicates, as a maker may have several printers having the same price.

To return to discussion of exercise #18

To solve a problem on SQL-EX.RU

Bookmark and Share
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.