Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(Spring2011) Instructor:Dr.MarkusSchneider
TA:GaneshViswanathan
Homework2Solutions
Topicscovered:SQL&RelationalAlgebra Due:Tuesday02/15/2011beforeclass
For scoring use only: Maximum Exercise I Exercise II Exercise III Exercise IV Exercise V Total 10 16 30 20 24 100 Received
Instructions:
You must attempt all questions. Attach SQL spool logs wherever applicable. Provide a hard-copy of your completed homework at the beginning of class on the due date shown above. Answers will be graded only for correctness and quality. Partial queries (SQL/RA) will only be graded if accompanied with a textual explanation of the attempt made in solving the query. Here is the grading breakdown: E-I: 5 x 2 points each. Total = 10 points. E-II: (A) 4 points, (B) 6 x 2 points each = 12 points. Total = 16 points. E-III: 12 x 2.5 points each. Total = 30 points. E-IV: 10 x 2 points each. Total = 20 points. E-V: 8 x 3 points each. Total = 24 points. Grand total = 100 points.
Exercise II (SQL - I)
Consider an agricultural database being built for a small garden that gets planted in Spring and is picked in Summer. The sunlight attribute refers to the percentage of a 24 hour day that the location gets sunlight and the plant optimally wants sunlight. The water attribute refers to the percentage of average rainfall that makes it to the root level for a location or is optimal for a plant. The planted (average expected) and picked (actual) weight is in kilograms. The picked amount is the number of items (one carrot, one beet, an ear of corn, one tomato, one radish) picked. (A) Create the following instance of the database schema in Oracle SQL. Refer to the class webpage for instructions on connecting to the CISE Oracle database. Use appropriate data types and formatting (e.g., for date) to match the provided schema. Provide the spool log file.
Location -------Name Sunlight ----------East .28 North .17 West .38 South .45 Plant ----Name Sunlight ----------Carrot .26 Beet .44 Corn .44 Tomato .42 Radish .28 Gardener -------GardenerID Name ---------- ---0 Mother 1 Father 2 Tim 3 Erin
LocationID ---------0 1 2 3
| | | | | |
Age --36 38 15 12
PlantID ------0 1 2 3 4
PlantFK ------0 0 1 2 2 3 4
Planted ------GardenerFK LocationFK Date1 ---------- ------------0 0 04-18-2010 1 1 04-14-2010 0 2 04-18-2010 1 3 04-14-2010 2 2 04-19-2010 3 3 04-25-2010 2 0 04-30-2010
Seeds ----28 14 36 20 12 38 30
PlantFK ------0 0 2 2 3 4
Picked -----GardenerFK LocationFK Date2 ---------- ------------2 0 08-18-2010 3 1 08-16-2010 1 3 08-22-2010 2 2 08-28-2010 3 3 08-22-2010 2 0 07-16-2010
Amount -----28 12 52 18 15 23
The required tables are created and the constraints are applied. Custom format the DATE using the command: SQL> ALTER SESSION SET NLS_DATE_FORMAT='mm-dd-yyyy';
(B) Now answer the following queries using the tables created. 1. Write a valid SQL statement that calculates the total weight of all ears of corn that were picked from the garden. Show the result set. SQL> SELECT SUM(Picked.Weight) AS corn_weight FROM Picked, Plant WHERE Picked.PlantFK = Plant.PlantID and Plant.Name = Corn;
CORN_WEIGHT ----------17.54
2. Using a multiplicative conversion factor of 2.2 to convert kilograms to pounds, write a valid SQL statement that shows the weight of the picked crop in total pounds per plant type. Show the result set. SQL> SELECT P.Name, SUM(2.2*Pc.Weight) AS Pounds FROM Plant P, Picked Pc WHERE P.PlantID=Pc.PlantFK GROUP BY P.Name;
NAME POUNDS ---------- ---------Carrot 7.348 Corn 38.588 Radish 1.144 Tomato 8.448
3. Write a valid SQL statement that would produce a result set like the following:
NAME ---------Tim Tim Tim NAME ---------Radish Carrot Corn PICKED_DAT AMOUNT ---------- ---------07-16-2010 23 08-18-2010 28 08-28-2010 18
SQL> SELECT Gardener.Name, Plant.Name, Picked.Picked_Date, Picked.Amount FROM Picked, Gardener, Plant WHERE Plant.PlantId = Picked.PlantFK AND Gardener.GardenerId = Picked.GardenerFK AND Picked.GardenerFK = 2 ORDER BY Picked.Picked_Date; 4. Run the following valid VIEW SQL statement to produce a table in memory: CREATE VIEW weights AS SELECT plant.name, picked.weight as picked_weight, picked.amount as amount, plant.weight as weight FROM picked, plant WHERE picked.plantfk = plant.plantid; This command creates the following view:
NAME PICKED_WEIGHT AMOUNT WEIGHT ---------- ------------- ---------- ---------Carrot 2.32 28 .08 Carrot 1.02 12 .08 Corn 12.96 52 .26 Corn 4.58 18 .26 Tomato 3.84 15 .16 Radish .52 23 .02 6 rows selected.
Note that view can be used just like a table in a query. Now using this view, write a valid SQL statement to find the variation between picked and expected weight for each plant, to produce a result set like the following:
NAME EXPECTED_WEIGHT PICKED_WEIGHT VARIANCE ---------- --------------- ------------- ---------Corn 13.52 12.96 -.56 Corn 4.68 4.58 -.1 Carrot .96 1.02 .06 Radish .46 .52 .06
2.24 2.4
2.32 3.84
.08 1.44
SQL> SELECT name, (amount * weight) AS expected_weight, picked_weight, (picked_weight-(amount * weight)) AS variance FROM weights ORDER BY variance; 5. Let us create a Carrot Planting Water-Analysis Report. Write a valid SQL statement that would produce a result set like the following:
NAME ---------Carrot Carrot Carrot Carrot NAME NEEDED AVAILABLE VARIANCE ---------- ---------- ---------- ---------East .82 .8 -.02 North .82 .84 .02 West .82 .48 -.34 South .82 .66 -.16
SQL> SELECT plant.name, location.name, plant.water AS needed, location.water AS available, location.water - plant.water AS variance FROM plant, location WHERE plant.name='Carrot'; 6. Write a valid SQL statement that calculates the average number of items produced per seed planted for each plant type (Plant Name). Output the average rounded to two decimal points. Show the result set. SQL> SELECT Plant.Name, CAST(SUM(Picked.Amount)/SUM(Planted.Seeds) AS NUMBER(4,2)) AS YIELD FROM Plant , Planted, Picked 2 WHERE Planted.PlantFK = Picked.PlantFK AND Planted.LocationFK = Picked.LocationFK 3 AND Plant.PlantID=Picked.PlantFK GROUP BY Plant.Name; Or, AVG() can be used.
NAME YIELD ---------- ---------Carrot .95 Corn 2.19 Radish .77 Tomato .39
WHERE E.cname = C.name AND C.fid = F.fid AND F.fname = Henry Hudson ); 3. SELECT C.name FROM Class C WHERE C.room = E107 OR C.name IN (SELECT E.cname FROM Enrolled E GROUP BY E.cname HAVING COUNT (*) >= 5); 4. SELECT DISTINCT S.sname FROM Student S WHERE S.snum IN (SELECT E1.snum FROM Enrolled E1, Enrolled E2, Class C1, Class C2 WHERE E1.snum = E2.snum AND E1.cname <> E2.cname AND E1.cname = C1.name AND E2.cname = C2.name AND C1.meets at = C2.meets at); 5. SELECT DISTINCT F.fname FROM Faculty F WHERE NOT EXISTS (( SELECT * FROM Class C ) EXCEPT (SELECTC1.room FROM Class C1 WHERE C1.fid = F.fid )); 6. SELECT DISTINCT F.fname FROM Faculty F WHERE 5 > (SELECT COUNT (E.snum) FROM Class C, Enrolled E WHERE C.name = E.cname AND C.fid = F.fid); 7. SELECT S.level, AVG(S.age) FROM Student S GROUP BY S.level; 8. SELECT S.level, AVG(S.age) FROM Student S WHERE S.level <> JR GROUP BY S.level; 9. SELECT F.fname, COUNT(*) AS CourseCount
FROM Faculty F, Class C WHERE F.fid = C.fid GROUP BY F.fid, F.fname HAVING EVERY ( C.room = E107); 10. SELECT DISTINCT S.sname FROM Student S WHERE S.snum IN (SELECT E.snum FROM Enrolled E GROUP BY E.snum HAVING COUNT (*) >= ALL (SELECT COUNT (*) FROM Enrolled E2 GROUP BY E2.snum )); 11. SELECT DISTINCT S.sname FROM Student S WHERE S.snum NOT IN (SELECT E.snum FROM Enrolled E ); 12. SELECT S.age, S.level FROM Student S GROUP BY S.age, S.level, HAVING S.level IN (SELECT S1.level FROM Student S1 WHERE S1.age = S.age GROUP BY S1.level, S1.age HAVING COUNT (*) >= ALL (SELECT COUNT (*) FROM Student S2 WHERE s1.age = S2.age GROUP BY S2.level, S2.age));
10
Catalog)
Suppliers) Catalog)
4) 5) 6) 7)
(sid, pid Catalog) / (pidcolor=red color=greenParts) R1(((sid, pid Catalog) / (pidcolor=red Parts)))
R2(((sid,
8) R1R2 R1(Catalog)
11
R1(Catalog) R2(Catalog) R1.pidR1.pid=R2.pidR1.sidR2.sid(R1 R2) R1(sid sname =Yosemite ShamSuppliers) R2(R1 Catalog) R3(R2) R4(1 sid, 2 pid, 3 cost)(R3.cost<R2.cost(R3 R2)) pid (R2 pid,costR4)
10)
12
Certified))
ename(aname=Boeing (Aircraft Certified Employees)) BonnToMadrid (from=Bonn to=Madrid (Flights)) aid(cruisingrange>distance(Aircraft BonnToMadrid)) flno(distance <cruisingrange salary>100,000(Flights ename(Employees
Aircraft Certified Employees))) R1(eid (cruisingrange >3000(Aircraft Certified)))
(R1 eid(aname=Boeing (Aircraft Certified)))) 6) The approach to take is first find all the employees who do not have the highest salary. Subtract these from the original list of employees and what is left is the highest paid employees. E1(Employees)
13
F(E1.salary>E2.salary) E2)
7) The approach taken is similar to the solution for the previous exercise. First find all the employees who do not have the highest salary. Remove these from the original list of employees and what is left is the highest paid employees. Remove the highest paid employees from the original list. What is left is the second highest paid employees together with the rest of the employees. Then find the highest paid employees of this new list. This is the list of the second highest paid employees. E1(Employees)
F(E1.salary>E2.salary) E2)
F(E1.salary>E5.salary) E5)
8) The approach behind this query is to first find the employees who are certified for at least three aircraft (they appear at least three times in the Certified relation). Then find the employees who are certified for at least four aircraft. Subtract the second from the first and what is left is the employees who are certified for exactly three aircraft. R1(Certif ied)
R2 R3)))
(R1.eid=R2.eid=R3.eid=R4.eid)(R1.aid_=R2.aid_=R3.aid_=R4.aid)(R
1 R2 R3 R4))) R5 R6 ***
14