loading..
Русский    English
17:35

Exercise #70 page 1

Point out the battles in which at least three ships from the same country took part.

Solution 3.7.1

Console
Execute
  1. SELECT AA.name AS bat
  2. FROM (SELECT O.battle AS name, C.country, COUNT(O.ship) AS cnt
  3. FROM Outcomes O, Ships S, Classes C
  4. WHERE O.ship = S.name AND
  5. C.class = S.class
  6. GROUP BY O.battle, C.country
  7. ) AA
  8. WHERE AA.cnt >= 3;

This query may be called a "first approximation" to the solution. All required tables are joined by WHERE clause. After that the battle and the country (from the Classes table) are determined for ships from the Outcomes table. Then the result have got grouped by battle with following selection of ships by count.

The mistake covers in fact that we do not take into account ships which is absent in the Ships table. This mistake occurs beacuse we use inner joins. For now it`s should be clear for reader that we do not take into account leading ships. The class of leading ship can be founded not only from Ships table, but also directly from Classes table. Thereupon the owner country can be founded. Now let`s consider solutions containing attempts to take into account this peculiarity.

Bookmark and Share
Pages 1 2 3 4
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 CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema date/time functions DATEADD DATEDIFF DATENAME DATEPART DATETIME DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates EXCEPT exercise (-2) exercise 19 exercise 23 exercise 32 More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.
Rambler's Top100