Русский    English

Exercise #70 page 1

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

Solution 3.7.1

  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