Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

lab2.SQL

Uploaded by

sidimed.jeilang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lab2.SQL

Uploaded by

sidimed.jeilang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MSC Data Science for Business Jannuary 2023

Data management

Exercises sheet n ̊2 : Advanced SQL

The following relations keep track of airline ight information:

Flights( no: integer, d_from: string, d_to: string, distance: integer, departs: time,
arrives: time, price: real)
Aircraft(aid: integer, aname: string, cruisingrange: integer)
Certi ed(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)

Note that the Employees relation describes pilots and other kinds of employees as well;
every pilot is certi ed for some aircraft, and only pilots are certi ed to y.

Write each of the following queries in SQL.

1. Find the names of aircraft such that all pilots certi ed to operate them have salaries
more than $80,000.

2. For each pilot who is certi ed for more than three aircraft, nd the eid and the
maximum cruising range of the aircraft for which she or he is certi ed.

3. Find the names of pilots whose salary is less than the price of the cheapest route from
Los Angeles to Honolulu.

4. For all aircraft with cruising range over 1000 miles, nd the name of the aircraft and the
average salary of all pilots certi ed for this aircraft.

5. Find the names of pilots certi ed for some Boeing aircraft.

6. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago.

7. Identify the routes that can be piloted by every pilot who earns less than $100.

8. Print the names of pilots who can operate planes with cruising range greater than 3000
miles but are not certi ed on any Boeing aircraft.

9. A customer wants to travel from Madison to New York with no more than two changes
of ight. List the choice of departure times from Madison if the customer wants to arrive
in New York by 6 p.m.

10. Compute the difference between the average salary of pilots and the average salary of
all employees (including pilots).

11. Print the name and salary of every non-pilot whose salary is more than the average
salary for pilots.
fl
fi
fl
fi
fi
fi
fi
fi
fl
fi
fi
fi
fi
fi
fl
MSC Data Science for Business Jannuary 2023
12. Print the names of employees who are certi ed only on aircrafts with cruising range
longer than 1000 miles.

13. Print the names of employees who are certi ed only on aircrafts with cruising range
longer than 1000 miles, but on at least two such aircrafts.

14. Print the names of employees who are certi ed only on aircrafts with cruising range
longer than 1000 miles and who are certi ed on some Boeing aircraft.

In order to create the database on Postgres, proceed as follows.

If you use Postgres, rst create a database named ‘tp2’ for the exercise:

create database tp2;


Then connect to that database:
\c tp2;
Execute the following commands one after the other for creating the tables :

create table Flights (


flno integer PRIMARY KEY,
d_from char(50),
d_to char(50),
distance integer,
departs timestamp,
arrives timestamp,
price real
);

CREATE TABLE Aircraft(


aid INT PRIMARY KEY ,
aname CHAR(50) NOT NULL,
cruisingrange INT NOT NULL
);

CREATE TABLE Employees(


eid INTEGER PRIMARY KEY
,ename CHAR(50) NOT NULL
,salary INTEGER NOT NULL
);

CREATE TABLE Certified(


aid INTEGER REFERENCES Aircraft(aid),
eid INTEGER REFERENCES Employees(eid),
PRIMARY KEY (aid, eid)
);

Insert commands for lling the database are available here :


https://www.dropbox.com/s/9z0bej5oswj4yyw/tp3_insert.sql
fi
fi
fi
fi
fi
fi
MSC Data Science for Business Jannuary 2023
Expected results for queries

Find the names of aircraft such that all pilots certi ed to operate them have salaries more than
$80,000.

aname
----------------------------------------------------
Boeing 737-800
Lockheed L1011
Airbus A320
Boeing 757-300
Boeing 747-400
Boeing 767-400ER
British Aerospace Jetstream 41
Boeing 727
Airbus A319
Airbus A340-300
Boeing 777-300
Tupolev 154
(12 rows)

For each pilot who is certi ed for more than three aircraft, nd
the eid and the maximum cruising range of the aircraft for which she or he is certi ed.

eid | max
-----------+------
567354612 | 8430
269734834 | 8430
142519864 | 8430
(3 rows)

Find the names of pilots whose salary is less than the price of the cheapest
route from Los Angeles to Honolulu.

----------------------------------------------------
Milo Brooks
(1 row)
fi
fi
fi
fi
MSC Data Science for Business Jannuary 2023
4

For all aircraft with cruising range over 1000 miles, nd the name of the aircraft and the average
salary of all pilots certi ed for this aircraft.

name | avgsalary
----------------------------------------------------+---------------------
Lockheed L1011 | 242685.750000000000
Airbus A320 | 243198.333333333333
Embraer ERJ-145 | 182838.200000000000
Airbus A340-300 | 217597.666666666667
Boeing 737-800 | 191700.250000000000
SAAB 340 | 118113.500000000000
British Aerospace Jetstream 41 | 220251.333333333333
Boeing 777-300 | 257973.333333333333
Boeing 757-300 | 189508.571428571429
Boeing 747-400 | 244776.750000000000
Boeing 727 | 273215.500000000000
Tupolev 154 | 205001.250000000000
Boeing 767-400ER | 209557.000000000000
Airbus A319 | 201283.666666666667
(14 rows)

Find the names of pilots certi ed for some Boeing aircraft.

ename
----------------------------------------------------
Betty Adams
George Wright
James Smith
John Williams
Karen Scott
Larry West
Lisa Walker
Mark Young
Mary Johnson
Michael Miller
(10 rows)
fi
fi
fi
MSC Data Science for Business Jannuary 2023
6

Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago.

aid
-----
1
2
3
6
8
9
10
11
12
13
14
(11 rows)

Identify the routes that can be piloted by every pilot who earns less than $100.

d_from | d_to
----------------------------------------------------+----------------------------------------------------
Detroit | New York
Madison | Chicago
Madison | Detroit
Madison | Minneapolis
Madison | Pittsburgh
Pittsburgh | New York
(6 rows)

Print the names of pilots who can operate planes with cruising range greater than 3000 miles but
are not certi ed on any Boeing aircraft.

ename
----------------------------------------------------
Eric Cooper
William Jones
Lawrence Sperry
Angela Martinez
Joseph Thompson
(5 rows)
fi
MSC Data Science for Business Jannuary 2023
9

A customer wants to travel from Madison to New York with no more than two changes of ight.
List the choice of departure times from Madison if the customer wants to arrive in New York by 6
p.m.

departs
---------------------
2005-04-12 06:15:00
2005-04-12 08:32:00
2005-04-12 07:05:00
(3 rows)

10

Compute the di erence between the average salary of pilots and the average salary of all
employees (including pilots).

res
--------------------
11449.645161290323
(1 row)

11

Print the name and salary of every non-pilot whose salary is more than the average salary for
pilots.

ename | salary
----------------------------------------------------+--------
David Anderson | 743001

12

Print the names of employees who are certi ed only on aircrafts with cruising range
longer than 1000 miles.

eid | ename
-----------+----------------------------------------------------
574489456 | William Jones
11564812 | John Williams
390487451 | Lawrence Sperry
310454876 | Joseph Thompson
274878974 | Michael Miller
90873519 | Elizabeth Taylor
356187925 | Robert Brown
573284895 | Eric Cooper
556784565 | Mark Young
242518965 | James Smith
550156548 | Karen Scott
141582651 | Mary Johnson
355548984 | Angela Martinez
(13 rows)
ff
fi
fl
MSC Data Science for Business Jannuary 2023

13

Print the names of employees who are certi ed only on aircrafts with cruising range longer than
1000 miles, but on at least two such aircrafts.

eid | ename
-----------+----------------------------------------------------
574489456 | William Jones
11564812 | John Williams
390487451 | Lawrence Sperry
310454876 | Joseph Thompson
274878974 | Michael Miller
573284895 | Eric Cooper
556784565 | Mark Young
242518965 | James Smith
550156548 | Karen Scott
355548984 | Angela Martinez
141582651 | Mary Johnson
(11 rows)

14

Print the names of employees who are certi ed only on aircrafts with cruising range longer than
1000 miles and who are certi ed on some Boeing aircraft.

eid | ename
-----------+----------------------------------------------------
274878974 | Michael Miller
242518965 | James Smith
556784565 | Mark Young
550156548 | Karen Scott
11564812 | John Williams
141582651 | Mary Johnson
(6 rows)
fi
fi
fi

You might also like