Exercise #70 page 2

Solution 3.7.2

  1. SELECT bat
  2. FROM (SELECT DISTINCT d.battle AS bat, a.country, COUNT(d.ship) AS s
  3. FROM Outcomes d, Ships b, Classes a
  4. WHERE d.ship = b.name AND
  5. b.class=a.class
  6. GROUP BY d.battle, a.country
  7. UNION
  8. SELECT DISTINCT d.battle AS bat, a.country, COUNT(d.ship) AS s
  9. FROM Outcomes d, Classes a
  10. WHERE d.ship = a.class AND
  11. d.ship NOT IN (SELECT name
  12. FROM Ships
  13. )
  14. GROUP BY d.battle, a.country
  15. ) AS t1
  16. WHERE s > 2;

The typical mistake of many beginners is in the order of operations under the resultant set. At first, they implement grouping and then they unioning. In this case the mistake of solution 3.7.1 is absent, but this solution do not returns country, which have three ships taking part in one battle, two of its` are in the table Ships and one (leading ship) is in the Outcomes table.

There was time the checking system accepted the wrong solution:

Bookmark and Share
Pages 1 2 3 4
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
several days ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.