Advertisement
heavenriver

ex04.sql

Oct 23rd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.71 KB | None | 0 0
  1. -- initialisation: creates tables
  2. use omnibox;
  3. drop table if exists company;
  4. drop table if exists flight;
  5. create table if not exists company
  6.   (code int(10) unsigned not null AUTO_INCREMENT,
  7.    nation varchar(45) not null,
  8.    primary key (code));
  9. create table if not exists flight
  10.   (code int(10) unsigned not null AUTO_INCREMENT,
  11.    departure varchar(45) not null,
  12.    arrival varchar(45) not null,
  13.    passengers int(10) unsigned not null,
  14.    company varchar(45) not null,
  15.    primary key (code));
  16.  
  17. -- populates tables
  18. insert into company values
  19.  (1, 'Italy'),
  20.  (2, 'Belgium'),
  21.  (3, 'France'),
  22.  (4, 'Spain'),
  23.  (5, 'France'),
  24.  (6, 'Italy'),
  25.  (7, 'Italy');
  26. insert into flight values
  27.  (11, 'Belize', 'Baden', 110, '01'),
  28.  (13, 'Baden', 'Keflovik', 110, '01'),
  29.  (15, 'Bombay', 'Dublin', 100, '02'),
  30.  (17, 'Dublin', 'Baden', 180, '02'),
  31.  (19, 'Belize', 'Dublin', 180, '03'),
  32.  (21, 'Moscow', 'Singapore', 210, '04'),
  33.  (27, 'CapeTown', 'Johannesburg', 180, '04'),
  34.  (30, 'Basel', 'Taipei', 100, '03'),
  35.  (31, 'Belize', 'Dublin', 100, '05'),
  36.  (32, 'Firenze', 'London', 200, '06'),
  37.  (34, 'Firenze', 'Belfast', 110, '06'),
  38.  (35, 'Belfast', 'Dublin', 110, '06'),
  39.  (37, 'Moscow', 'Taipei', 110, '05'),
  40.  (41, 'Firenze', 'Taipei', 110, '06'),
  41.  (45, 'Dublin', 'Moscow', 120, '06'),
  42.  (46, 'Dublin', 'Belize', 120, '06'),
  43.  (56, 'Moscow', 'Dublin', 180, '04'),
  44.  (57, 'Rome', 'London', 210, '04'),
  45.  (58, 'Palermo', 'Milano', 100, '04'),
  46.  (59, 'Singapore', 'London', 110, '07'),
  47.  (75, 'Parigi', 'London', 140, '07'),
  48.  (76, 'Belize', 'Taipei', 200, '07'),
  49.  (91, 'London', 'Rome', 150, '07'),
  50.  (96, 'Keflowik', 'Moscow', 145, '07'),
  51.  (97, 'Dublin', 'Rome', 150, '03'),
  52.  (102, 'Bombay', 'Singapore', 120, '03'),
  53.  (121, 'Belize', 'Dublin', 150, '03'),
  54.  (126, 'Tapei', 'Baden', 180, '03'),
  55.  (178, 'Napoli', 'Roma', 150, '05');
  56.  
  57. -- EXERCISE 4.1
  58. -- show the flight data for flights of Italian companies
  59. select f.*
  60.     from flight f, company c
  61.     where f.company = c.code
  62.           and c.nation = 'Italy';
  63.  
  64. -- EXERCISE 4.2
  65. -- show airports from which no French company flights take off
  66. select departure as airport
  67.     from flight
  68.     where departure <> all
  69.         (select f.departure
  70.             from flight f, company c
  71.             where f.company = c.code
  72.                   and c.nation = 'France');
  73.  
  74. -- EXERCISE 4.3
  75. -- for each airport, show the overall number of passengers, but only if the number of flights is greater than 4
  76. select t.airport, sum(t.passengers)
  77.     from
  78.     (select f1.departure as airport, f1.passengers, count(f1.arrival) as flights
  79.         from flight f1, flight f2
  80.         where f1.departure = f2.departure
  81.         group by f1.departure, f1.arrival, f1.passengers
  82.         having count(f1.arrival) > 4) t;
  83.  
  84. -- removes data
  85. drop table if exists company;
  86. drop table if exists flight;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement