P. 1
Oracle Queries Solution

Oracle Queries Solution

|Views: 340|Likes:
Published by yallamk

More info:

Published by: yallamk on May 11, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/09/2012

pdf

text

original

Database Systems

Instructors: Hao-Hua Chu Winston Hsu Fall Semester, 2007

Assignment 3: SQL Queries Questions
1. The following relations keep track of airline flight information: Flights(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time, price: integer) Aircraft(aid: integer, aname: string, cruisingrange: integer) Certified(eid: integer, aid: integer) Employees(eid: integer, ename: string, salary: integer) Note that the Employees relation describes pilots and other kinds of employees aswell; every pilot is certified for some aircraft, and only pilots are certified to fly.Write each of the following queries in SQL. a. Find the names of aircraft such that all pilots certified to operate them earn more than $80,000. b. For each pilot who is certified for more than three aircraft, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified. c. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles to Honolulu. d. For all aircraft with cruisingrange over 1000 miles, find the name of the aircraft and the average salary of all pilots certified for this aircraft. e. Find the names of pilots certified for some Boeing aircraft. f. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago. g. Identify the routes that can be piloted by every pilot who makes more than $100,000. h. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles but are not certified on any Boeing aircraft. i. A customer wants to travel from Madison to New York with no more than two changes of flight. List the choice of departure times from Madison if the customer wants to arrive in New York by 6 p.m. j. Compute the difference between the average salary of a pilot and the average salary of all employees (including pilots). k. Print the name and salary of every nonpilot whose salary is more than the average salary for pilots.

F ’ s el e g n A s o L ‘ = m orf. p m e T .C deifitreC .E SELECT FROM WHERE SELECT MIN FROM WHERE AND AND ) ’ o g a ci h C ‘ = ot. di e. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles and who are certified on some Boeing aircraft.A ( SELECT FROM WHERE DISTINCT e m a n e. n. A = di a.A .C deifitreC A tf ar cri A Ans: SELECT FROM WHERE l. C SELECT FROM WHERE DISTINCT MAX SELECT MIN FROM WHERE ( < y r al a s. C A tfarcriA . E SELECT FROM y r al a S g v A. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles.seman tfarcria rof sksa noitseuq eht tub .tfarcriA rof yek eht si dia taht evresbO ) ’ ul ul o n o H ‘ = ot.f . F ( ( > e g n a r g ni si u r c.C = die. E E s e e y ol p m E e m a n e. F ( .E = die. C = di a.E = die. C E seeyolpmE . Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles. p m e T SELECT FROM WHERE GROUP BY AS AND SELECT FROM WHERE AVG AND AS yr al a S g v A ) yr al a s.d F st h gil F di a.A tfarcriA :pmeT noitaler etaidemretni na gnisu yb noitacilpmoc siht htiw laed ew . A E seeyolpmE .e man AND AS AND 0 0 0 1 > e g n ar g ni si ur c.dia.E seeyolpmE p me T ) emana. DISTINCT e m a n a.b .c . A A tf ar cri A di a. C 1 E s e e y ol p m E ( die. F F st h gil F ) e c n at si d. A . F ’ s el e g n A s o L ‘ = m orf. but on at least two such aircrafts.C emana. A IN SELECT FROM WHERE NOT EXISTS ( di A.C deifitreC . A SELECT FROM WHERE GROUP BY HAVING COUNT 3 > ) *( di e.dia. A ( )) 0 0 0 0 8 < yr al a s.1E * .E = die.C di a. m. 1 E die.a .A tfarcriA .A = dia. A die. E AND AND LIKE ’ %gn ieo B ‘ e m a n a. e m a n.e .A . F ) e cir p.A di a. E( . A dia.C deifitreC ) e g n a r g ni si u r c.C die.

0 F m orf. F . 1 A E s e e y ol p m E e c n at si d. 1 A di a. E E s e e y ol p m E * SELECT DISTINCT FROM WHERE NOT EXISTS SELECT FROM WHERE AND NOT EXISTS SELECT FROM WHERE AND ) )dia. F F st h gil F st r a p e d. F SELECT FROM WHERE DISTINCT e m a n e. 0 F di a. 0 F UNION SELECT FROM WHERE ( UNION SELECT FROM WHERE AND AND AND AND AND AND AND AND AND AND AND AND AND ( (( o nl f.i . 1 F st h gil F . 0 F ’ kr o Y w e N ‘ = ot. C (( di e. 1 F > str a p e d. 2 F s e virr a. 1 F = ot. C = di a. 1 F = ot. 1 A 1 A tf ar cri A ( ) 0 0 0 3 > e g n ar g ni si ur c. 1 F s e virr a. F . 0 F 0 F st h gil F o nlf.h AND . 1 F ’ kr o Y w e N ‘ = ot. 0 F 1 F st h gil F .A tfarcriA * 0 0 0 0 0 1 > yr al a s. 1 F m orf. 0 F > str a p e d. 1 F ’ k r o Y w e N ‘ > < ot. 0 F st h gil F o nlf. 0 F AND ) ’ 0 0: 8 1 ‘ < s e virr a. 0 F ) ’ 0 0: 8 1 ‘ < s e virr a. 0 F 2 F st h gil F .A die. 2 F m orf.C = dia. 0 F > str a p e d. m o r f. 2 F s e virr a. 0 F ’ n o si d a M ‘ = m orf. E IN SELECT FROM WHERE EXISTS SELECT FROM WHERE AND AND NOT EXISTS SELECT FROM WHERE AND LIKE )) ’ % g ni e o B ‘ e m a n a. 0 F ’ n o si d a M ‘ = m orf. A C deifitreC . C = di a. 2 F = ot.)) ’ 0 0: 8 1 ‘ < s e virr a. F > e g n ar g ni si ur c.g . 0 F ’ n o si d a M ‘ = m orf. A A tf ar cri A ( C d eifitr e C di e. 1 F ’ k r o Y w e N ‘ > < ot. A di a.E ( ( F st h gil F ot. 0 F st h gil F o nlf. A di a. 1 F ’ kr o Y w e N ‘ > < ot. E SELECT FROM WHERE IN SELECT FROM WHERE AND ’ kr o Y w e N ‘ = ot.C = die.

C = die.C = die.emane.C deifitreC .E A tfarcriA . 1 E 1 E s e e y ol p m E ) C d eifitr e C di e. E . E 1 > ) *( ) 0 0 0 1 > e g n a r g ni si u r c.C deifitreC .E .1p me T )) C d eifitr e C SELECT FROM gva.1pmeT SELECT FROM WHERE IN SELECT DISTINCT FROM AS AVG AS AS di e. C EVERY AND ANY ) ’ g ni e o B ’ = e m a n a. 1 C A tfarcriA .E seeyolpmE e m a n e. C 2p me T gva ) 1 E s e e y ol p m E .die. 1 E( ( > y r al a s.n AND COUNT .E di a.k .2pmeT . A( ) 0 0 0 1 > e g n a r g ni si u r c.m . E E s e e y ol p m E AVG AS gva ) yr al a s. A = di a. A ( die. A ( .E . E ) 0 0 0 1 > e g n a r g ni si u r c. 1 E( ( ( SELECT FROM WHERE NOT IN SELECT DISTINCT FROM SELECT AVG FROM WHERE IN SELECT DISTINCT FROM ( di e.E di a.E . A = di a.die.C deifitreC .die.E A tfarcriA .E emane. A = di a. C e m a n e.emane.E di a. SELECT FROM WHERE GROUP BY HAVING SELECT FROM WHERE GROUP BY HAVING SELECT FROM WHERE GROUP BY HAVING EVERY EVERY AND AND AND die.E seeyolpmE emane. E ) ) 1 C d eifitr e C di e. A ( die. C e m a n e.E AND ) yr al a s.E seeyolpmE ( di e. E( SELECT FROM ) yr al a s.C = die. C ( di e.j l.E .gva. E E s e e y ol p m E yralas.

but at least one department with budget less than $5 million. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. g. 1 W( ) di d.W( W s kr o W ( < 0002 . An employee can work in more than one department. Ans: 2 D t p e D . Print the names and ages of each employee who works in both the Hardware department and the Software department. where the part-time and full-time employees add up to at least that many fulltime employees). Find the managerids of managers who manage only departments with budgets greater than $1 million. Find the managerids of managers who control the largest amounts. did: integer. e. W di d. E SELECT FROM WHERE . 2 D = di d.. 1 W = di e. d. Find the enames of managers who manage only departments with budgets larger than $1 million. the pct_time filed of the Works relation shows the percentage of time that a given employee works in a given department. 2 W = di e. E p m E ) e mit t c p.E . Find the managerids of managers who control more than $5 million. W = di d. b.b . 1 D = di d. 1 W di d.e. 1 W 1 W s kr o W ega. E di e. ename: string. age: integer. 2 D di d.2. salary: real) Works(eid: integer. budget: real. he or she controls the sum of all the budgets for those departments. W SELECT FROM GROUP BY HAVING COUNT SUM ’ er a w dr a H ‘ = e m a n d. pct_time: integer) Dept(did: integer. 2 W di e.emane. managerid: integer) Write the following queries in SQL: a. Consider the following relational schema. 2 W s kr o W . 1 D ’ er a wtf o S ‘ = e m a n d. Find the enames of managers who manage the departments with the largest budgets. 1 W s kr o W .a AND AND AND AND AND . If a manager manages more than one department. Emp(eid: integer. c. print the did together with the number of employees that work in that department. dname: string. h.E SELECT FROM WHERE )die. 1 D t p e D . di d. For each department with more than 20 full-time-equivalent employees (i. f.

W = di e. D = dir e g a n a m.D = die. E .2 D 2 D tpe D SELECT FROM WHERE ALL SELECT FROM WHERE AND ) di d. D D tpe D ) dir e g a n a m. D t p e D ( > y r al a s. D SELECT FROM DISTINCT SELECT DISTINCT FROM GROUP BY WHERE SELECT MAX FROM SUM AS t e g d u B p m et )t e g d u b. D p m et( ( = t e g d u B p m et.e . D p m et SELECT FROM WHERE HAVING GROUP BY AND ANY e m a n e. D SELECT FROM WHERE IN SELECT FROM WHERE dir e g a n a m. E p m E e m a n e. E E pmE e m a n e. 2 D( ( = t e g d u b.E D tpe D . D ( di e. D = dir e g a n a m. di E. 2 D( 2 D tpe D )) 2 D t p e D )t e g d u b. E direganam.f . D ( .h SELECT MAX FROM . D D tpe D ) dir e g a n a m. E SELECT FROM WHERE DISTINCT ALL SELECT FROM WHERE ( < 0000001 D tpe D di r e g a n a m.d . E EVERY ) 0 0 0 0 0 0 5 < t e g d u b. dir e g a n a m. 2 D tegdub . E SELECT FROM WHERE SELECT SUM FROM WHERE ( < 0000005 D tpe D di r e g a n a m. D ( ) 0 0 0 0 0 0 1 > t e g d u b.c . 2 D )t e g d u b. D W s kr o W . E t e g d u b.) D p m et )t e g d u B p m et. D AS D p m et ( dir e g a n a m. D di e. E E pmE e m a n e.g . D p m et ) di r e g a n a m. D( . W = di d.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->