Database: ship_1

Question: How many captains younger than 50 are in each rank?
SQL: SELECT count(*) ,  rank FROM captain WHERE age  <  50 GROUP BY rank

Question: Which rank is the most common among captains?
SQL: SELECT rank FROM captain GROUP BY rank ORDER BY count(*) DESC LIMIT 1

Question: Which classes have more than two captains?
SQL: SELECT CLASS FROM captain GROUP BY CLASS HAVING count(*)  >  2

Question: Find the name of captains whose rank are either Midshipman or Lieutenant.
SQL: SELECT name FROM captain WHERE rank  =  'Midshipman' OR rank  =  'Lieutenant'

Question: What are the average and minimum age of captains in different class?
SQL: SELECT avg(age) ,  min(age) ,  CLASS FROM captain GROUP BY CLASS

Question: Find the captain rank that has some captains in both Cutter and Armed schooner classes.
SQL: SELECT rank FROM captain WHERE CLASS  =  'Cutter' INTERSECT SELECT rank FROM captain WHERE CLASS  =  'Armed schooner'

Question: Find the captain rank that has no captain in Third-rate ship of the line class.
SQL: SELECT rank FROM captain EXCEPT SELECT rank FROM captain WHERE CLASS  =  'Third-rate ship of the line'

Question: What is the name of the youngest captain?
SQL: SELECT name FROM captain ORDER BY age LIMIT 1

Question: Find the name, type, and flag of the ships that were built in the most recent year.
SQL: SELECT name, type, flag from ship where built_year in (SELECT max(built_year) from ship)

Question: Which flag is most widely used among all ships?
SQL: SELECT flag FROM ship GROUP BY flag ORDER BY count(*) DESC LIMIT 1

Question: Find the ship type that are used by both ships with Panama and Malta flags.
SQL: SELECT TYPE FROM ship WHERE flag  =  'Panama' INTERSECT SELECT TYPE FROM ship WHERE flag  =  'Malta'

Question: In which year were most of ships built?
SQL: SELECT built_year FROM ship GROUP BY built_year ORDER BY count(*) DESC LIMIT 1

Question: Find the name of the ships that have more than one captain.
SQL: SELECT t1.name FROM ship AS t1 JOIN captain AS t2 ON t1.ship_id  =  t2.ship_id GROUP BY t2.ship_id HAVING count(*)  >  1

Question: what are the names and classes of the ships that do not have any captain yet?
SQL: SELECT name ,  CLASS FROM ship WHERE ship_id NOT IN (SELECT ship_id FROM captain)

Question: Find the name of the ship that is steered by the youngest captain.
SQL: SELECT t1.name FROM ship AS t1 JOIN captain AS t2 ON t1.ship_id  =  t2.ship_id ORDER BY t2.age LIMIT 1

Question: Find the name and flag of ships that are not steered by any captain with Midshipman rank.
SQL: SELECT name ,  flag FROM ship WHERE ship_id NOT IN (SELECT ship_id FROM captain WHERE rank  =  'Midshipman')