Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.01 KB | None | 0 0
  1. /*
  2.  
  3. Student ID: Name
  4. Student ID: Name
  5. Student ID: Name
  6. Student ID: Name
  7. Student ID: Name
  8.  
  9. Class Number:
  10. Group Number:
  11. Tutor's Name:
  12.  
  13. */
  14.  
  15.  
  16.  
  17.  
  18. /******* TASK 1: AFTER THIS COMMENT, INSERT YOUR CODE TO CREATE & POPULATE THE FIVE TABLES BELOW (PUT THEM IN THE CORRECT ORDER OF EXECUTION) ***********/
  19.  
  20. -- Create table aircrafttype
  21. create table aircrafttype(
  22. aircrafttypeid char(2), -- A unique identifier for the type of aircraft
  23. aircrafttypename varchar(20), -- The name of the type of aircraft
  24. primary key(aircrafttypeid)); -- Setting aircrafttypeid as the primary key
  25.  
  26. -- Insert statements for aircrafttype
  27. Insert into aircrafttype values ('22','Airbus 220');
  28. Insert into aircrafttype values ('31','Airbus 310');
  29. Insert into aircrafttype values ('B3','Boeing 737');
  30. Insert into aircrafttype values ('B6','Boeing 767');
  31. Insert into aircrafttype values ('B7','Boeing 777');
  32. Insert into aircrafttype values ('B8','Boeing 787');
  33.  
  34. -- Create table aircraft
  35. create table aircraft(
  36. aircraftid char(2), -- A unique identifier for the aircraft
  37. aircraftpurdate date, -- The date of purchase of an aircraft
  38. aircraftseatcap numeric(3), -- Seating capacity of an aircraft
  39. aircrafttypeid char(2), -- The id of the type of aircraft [FK] referencing aircrafttypeid in aircrafttype table; refers to the type of aircraft
  40. primary key (aircraftid), -- Setting aircraftid as the primary key
  41. foreign key (aircrafttypeid) references aircrafttype(aircrafttypeid)); -- Setting aircrafttypeid as the foreign key from the table aircrafttype
  42.  
  43. -- Insert statements for aircraft
  44. Insert into aircraft values ('A1','2012-06-19','140','B3');
  45. Insert into aircraft values ('A2','2013-08-19','129','B6');
  46. Insert into aircraft values ('A3','2013-05-19','104','B3');
  47. Insert into aircraft values ('A4','2017-04-19','296','B7');
  48. Insert into aircraft values ('A5','2018-03-19','120','B6');
  49. Insert into aircraft values ('A6','2014-10-19','191','31');
  50. Insert into aircraft values ('A7','2015-10-19','198','31');
  51. Insert into aircraft values ('A8','2016-12-19','204','22');
  52. Insert into aircraft values ('A9','2017-01-19','173','22');
  53.  
  54. -- Create table hangar
  55. create table hangar(
  56. hangarid char(2), -- A unique identifier for the hangar
  57. hangarlocation varchar(20), -- The location of the hangar
  58. hangarstoragecap numeric(2), -- The storage capacity of the hangar
  59. primary key(hangarid)); -- Setting hangarid as the primary key
  60.  
  61. -- Insert statements for hangar
  62. Insert into hangar values ('H1','Sydney, NSW','7');
  63. Insert into hangar values ('H2','Melbourne, VIC','22');
  64. Insert into hangar values ('H3','Sydney, NSW','25');
  65. Insert into hangar values ('H4','Brisbane, QLD','8');
  66. Insert into hangar values ('H5','Launceston, TAS','14');
  67.  
  68. -- Create table service
  69. create table service(
  70. serviceid char(3), -- A unique identifier for the service
  71. servicedate date, -- The date for a service
  72. hangarid char(2), -- The id of the hangar [FK] referencing hangarid in hangar table; refers to the hangar the service happens at
  73. aircraftid char(2), -- The id of the aircraft [FK] referencing aircraftid in aircraft table; refers to the aircraft being serviced
  74. teamid char(2), -- The id of the serviceteam [FK] referencing teamid in serviceteam table
  75. primary key(serviceid), -- Setting serviceid as the primary key
  76. foreign key(hangarid) references hangar(hangarid), -- Setting hangarid as the foreign key from the table hangar
  77. foreign key(aircraftid) references aircraft(aircraftid), -- Setting aircrafttypeid as the foreign key from the table aircraft
  78. foreign key(teamid) references serviceteam(teamid)); -- Setting teamid as the foreign key from the table serviceteam
  79.  
  80. -- Insert statements for service
  81. Insert into service values ('S1','2019-09-25','H3','A3','T1');
  82. Insert into service values ('S2','2019-08-27','H2','A7','T2');
  83. Insert into service values ('S3','2019-09-22','H5','A7','T4');
  84. Insert into service values ('S4','2019-05-13','H5','A4','T4');
  85. Insert into service values ('S5','2019-01-08','H1','A4','T1');
  86. Insert into service values ('S6','2019-09-07','H4','A9','T3');
  87. Insert into service values ('S7','2019-12-20','H3','A9','T1');
  88. Insert into service values ('S8','2019-12-20','H4','A3','T3');
  89. Insert into service values ('S9','2019-05-18','H4','A2','T3');
  90. Insert into service values ('S10','2019-05-14','H3','A3','T1');
  91. Insert into service values ('S11','2019-05-27','H3','A3','T1');
  92. Insert into service values ('S12','2019-08-11','H3','A9','T1');
  93. Insert into service values ('S13','2019-08-17','H4','A2','T3');
  94. Insert into service values ('S14','2019-12-14','H4','A4','T3');
  95. Insert into service values ('S15','2025-01-25','H5','A1','T4');
  96.  
  97. -- Create table serviceteam
  98. create table serviceteam(
  99. teamid char(2), -- A unique identifier for the team
  100. teamname varchar(20), -- The name of the hangar
  101. teamlevel numeric(1), -- The seniority level of the team
  102. primary key (teamid)); -- Setting teamid as the primary key
  103.  
  104. -- Insert statements for serviceteam
  105. Insert into serviceteam values ('T1','Sydney Rabbitohs','5');
  106. Insert into serviceteam values ('T2','Melbourne Rebels','3');
  107. Insert into serviceteam values ('T3','Queensland Reds','5');
  108. Insert into serviceteam values ('T4','Team TRU','4');
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /******* TASK 2: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  115. select hangarlocation
  116. from hangar
  117. where hangarstoragecap <16
  118. or hangarstoragecap >30;
  119.  
  120.  
  121.  
  122. /******* TASK 3: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  123.  
  124. select aircraftid, aircraftpurdate
  125. from aircraft
  126. where aircraftseatcap > 150
  127. and month(aircraftpurdate) = 10
  128. or year(aircraftpurdate) in (2014,2016)
  129. order by aircraftseatcap desc;
  130.  
  131.  
  132. /******* TASK 4: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  133.  
  134. select a.aircraftid 'AircraftID'
  135. from aircraft a, aircrafttype aa
  136. where aa.aircrafttypeid=a.aircrafttypeid
  137. and aa.aircrafttypename like 'Airbus%'
  138. order by aircraftid;
  139.  
  140.  
  141.  
  142. /******* TASK 5: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  143.  
  144. select distinct a.aircraftid, aa.aircrafttypename
  145. from aircraft a, aircrafttype aa, service s
  146. where aa.aircrafttypeid = a.aircrafttypeid
  147. and a.aircraftid = s.aircraftid
  148. and s.hangarid = 'H4';
  149.  
  150.  
  151.  
  152. /******* TASK 6: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  153.  
  154. select *
  155. from service
  156. where (hangarid = 'H1'
  157. or hangarid = 'H3')
  158. and servicedate in
  159. (select servicedate
  160. from service
  161. where month(servicedate) between 07 and 09);
  162.  
  163. select *
  164. from service
  165. where month(servicedate) between 07 and 09
  166. and hangarid in
  167. (select hangarid
  168. from hangar
  169. where hangarid = 'H1'
  170. or hangarid = 'H3');
  171. -- This one
  172. select *
  173. from service
  174. where month(servicedate) between 07 and 09
  175. and hangarid in
  176. (select hangarid
  177. from hangar
  178. where hangarlocation like '%NSW');
  179.  
  180. /******* TASK 7: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  181.  
  182. select distinct s.*
  183. from service s join hangar h
  184. on s.hangarid=h.hangarid
  185. where month(s.servicedate) between 07 and 09
  186. and h.hangarlocation like '%NSW';
  187.  
  188.  
  189.  
  190. /******* TASK 8: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  191.  
  192. select distinct s.aircraftid
  193. from aircraft a, service s, hangar h, serviceteam st
  194. where a.aircraftid = s.aircraftid
  195. and h.hangarid = s.hangarid
  196. and st.teamid = s.teamid
  197.  
  198. and (a.aircrafttypeid = '22' -- like % airbus?
  199. or a.aircrafttypeid = 'B3') -- like % airbus?
  200.  
  201. and s.servicedate = '2019-12-20'
  202.  
  203. and (h.hangarstoragecap < 10
  204. or h.hangarstoragecap > 20)
  205.  
  206. and st.teamlevel = 5
  207.  
  208. order by s.aircraftid desc;
  209. -- need subqueries?
  210.  
  211. -- Write a query to print all the ids of all Airbus 220 or Boeing 737 aircrafts
  212. -- that are to be serviced on 20th of December, 2019
  213. -- at a hangar with a storage capacity of either less than 10 or over 20
  214. -- by a team whose seniority level is 5.
  215. -- Sort the results by id of the aircraft in descending order
  216.  
  217. /******* TASK 9: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  218.  
  219. select distinct count(s.aircraftid)
  220. from service s join aircraft a
  221. on s.aircraftid = a.aircraftid
  222. where year(aircraftpurdate) > 2017
  223. or aircraftseatcap != 104; -- this one
  224.  
  225.  
  226. /******* TASK 10: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  227.  
  228. select st.teamid, st.teamlevel, count(s.teamid)
  229. from serviceteam st, service s
  230. where st.teamid=s.teamid
  231. and (st.teamlevel = 1
  232. or st.teamlevel = 3)
  233.  
  234. -- and count(s.teamid) < 4
  235. group by s.teamid
  236. order by s.teamid desc;
  237. -- Write a query to print the team number, their seniority level and number of services they have performed.
  238. -- Only include teams that have a seniority level of either 1 or 3
  239. -- and have less than 4 services performed.
  240. -- Sort the results by the number of services in descending order.
  241. -- Please note that if a particular team has not serviced at all, you have to include them in the results as well.
  242.  
  243.  
  244. /******* TASK 11: AFTER THIS COMMENT, INSERT YOUR QUERY BELOW ***********/
  245.  
  246. -- Write a query to print the aircraft ids and the number of times they have been serviced,
  247. -- if they have more than average number of services.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement