1

This is the Database schemaThe system shows that I got the correct answer but im wrong logically? Heres the description for the database: The database of naval ships that took part in World War II is under consideration. The database consists of the following relations:

  • Classes(class, type, country, numGuns, bore, displacement)
  • Ships(name, class, launched)
  • Battles(name, date)
  • Outcomes(ship, battle, result)

Ships in classes all have the same general design. A class is normally assigned either the name of the first ship built according to the corresponding design, or a name that is different from any ship name in the database. The ship whose name is assigned to a class is called a lead ship. The Classes relation includes the name of the class, type (can be either bb for a battle ship, or bc for a battle cruiser), country the ship was built in, the number of main guns, gun caliber (bore diameter in inches), and displacement (weight in tons). The Ships relation holds information about the ship name, the name of its corresponding class, and the year the ship was launched. The Battles relation contains names and dates of battles the ships participated in, and the Outcomes relation - the battle result for a given ship (may be sunk, damaged, or OK, the last value meaning the ship survived the battle unharmed).

The question is: For each ship that participated in the Battle of Guadalcanal, get its name, displacement, and the number of guns.

and here is my code whats wrong with my code?

select ships.name, displacement, numguns from battles 

full outer join outcomes on battles.name=outcomes.battle 
full outer join ships on ships.name=outcomes.ship
full outer join classes on classes.class=ships.class

where battles.name = 'Guadalcanal'

3 Answers 3

3

You need to use LEFT JOIN here in order to consider ships which do not have classes.

    SELECT    ship ,
            c.displacement ,
            c.numguns
  FROM      ( SELECT    o.ship ,
                        COALESCE(s.class, o.ship) class
              FROM      outcomes o
                        INNER JOIN Battles b ON o.battle = b.name
                        LEFT JOIN ships s ON s.name = o.ship
              WHERE     b.name = 'Guadalcanal'
            ) s
            LEFT JOIN classes c ON c.class = s.class;
0

First, based on the image you provided, in your query the table names are wrong.

Second, unless you really need to, just use a regular join (INNER JOIN, keyword JOIN is inner join

Third in your column selection, put the table name for all fields and make sure the table names are the same. Not mandatory but good practice, especially when joining many tables and selecting a lot of data.

0

Your SQL can be done using INNER JOIN

Joins between tables can be read here

For example :

SELECT a.name, c.displacement, c.numGuns
FROM Ships a
INNER JOIN Outcomes b ON a.name = b.ship
INNER JOIN Battles c ON b.battle=c.name
INNER JOIN Classes d ON a.class=d.class
WHERE c.name = 'Guadalcanal'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.