13
Exam #1 (October 11, 2012) 100 points Name ______________________________ For each of the following questions: Write relational algebra (or draw query trees) and SQL commands to answer the following questions NOTE: Your relational algebra must be optimized!! PART I – Use the Company Database (40 points) 1) For each project that has more than two employees worked on and controlled by the ‘Administration’ Department, list the project name and the total hours per week (by all employees) spent on that project. (10 points) select pname, sum(hours) from department D, project, works_on where dname = 'Administration' and dnumber =dnum and pno = pnumber

Makeup Exam #1 (October 28, 2009) - University of …student.bus.olemiss.edu/files/conlon/mis408/Old Exams... · Web viewA_Bal from Customer) T_Cust) Where C.balance > T_Cust.A_Bal

  • Upload
    phamnhu

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Exam #1 (October 11, 2012) 100 points Name ______________________________

For each of the following questions:Write relational algebra (or draw query trees) and SQL commands to answer the following questions

NOTE: Your relational algebra must be optimized!!

PART I – Use the Company Database (40 points)

1) For each project that has more than two employees worked on and controlled by the ‘Administration’ Department, list the project name and the total hours per week (by all employees) spent on that project. (10 points)

select pname, sum(hours)from department D, project, works_onwhere dname = 'Administration' and dnumber =dnumand pno = pnumbergroup by pnamehaving count(essn) > 2order by pname;

PNAME SUM(HOURS)--------------- ----------Computerization 55 Newbenefits 55

2) List the name of each employee whose salary is higher than $30,000 per year and worked on every project located in Houston. – use division (10 points)

select fname, lname, salary, ssnfrom employeewhere salary > 30000and not exists((select pnumber from project where plocation = 'Houston')minus(select pno from works_on where essn = ssn));

FNAME LNAME SALARY SSN --------------- --------------- ---------- ---------Franklin Wong 40000 333445555

3) List the employee names, department names, and the salaries of the employees who have the lowest salary in each department. (10 points)

select E.fname, E. lname, D.dname, E.sala ry, Min_Salfrom employee E, department D, (select dno, min(salary) Min_Sal from employee group by DNO) T_MaxSal where E.salary = Min_Sal and E.dno = D.dnumber and E.dno = T_MaxSal.dno;

FNAME LNAME DNAME SALARY MIN_SAL--------------- --------------- --------------- ---------- ----------Joyce English Research 25000 25000 Ahmad Jabbar Administration 25000 25000 Alicia Zeleya Administration 25000 25000 James Borg Headquarters 55000 55000

4) For each employee who does not have any dependents, retrieve the name, his/her supervisor’s name, and the project name(s) and project locations he/she worked on. (10 points)

select E1.fname Emp_First, E1.lname Emp_Last, E2.fname Super_First, E2.lname Super_Last, pname, plocationfrom Employee E1, Employee E2, Department, Project, works_onwhere E1.superssn = E2.ssnand E1.dno = Dnumber and dnumber = dnum and pnumber = pnoand E1.ssn = essnand E1.ssn not in (select essn from dependent);

EMP_FIRST EMP_LAST SUPER_FIRST SUPER_LAST PNAME PLOCATION --------------- --------------- --------------- --------------- --------------- ---------------Ramesh Narayan Franklin Wong ProductZ Houston Joyce English Franklin Wong ProductY Sugarland Joyce English Franklin Wong ProductX Bellaire Ahmad Jabbar Jennifer Wallace Newbenefits Stafford Alicia Zeleya Jennifer Wallace Newbenefits Stafford Ahmad Jabbar Jennifer Wallace Computerization Stafford Alicia Zeleya Jennifer Wallace Computerization Stafford

7 rows selected

PART II – Use the department store database (60 points)

1) List the names of each customers in Oxford who havey not ordered any products in Years 2011-2012. (15 points)

Select first_name, Last_NameFrom Customer Where CusID not in (select CustId from order);

2) List the names of customers who placed more than two orders (in 2011-2012) OR purchased products produced in Tupelo, MS. (15 points)

Select firstName, LastNameFrom Customer C, (select custId, Count(OID) C_Ord from Order_Table group by CustId having C_Ord > 2) T_OrdWhere C.CustId = T_Ord.CustIdUnionSelect firstName, LastNameFrom Customer C, Product, Order_Details D, Order_Table OWhere ProducedIn = ‘Tupelo’And ProductId = PIDAnd O.OID = D.OIDAnd O.CustId = C.CustID;

3) List the names of customers who ordered every product produced in Oxford, MS (use division). (15 points)

Select fname, lname from customerwhere not exists ((select productId from product where Producedin = ‘Oxford, MS’) Minus (select PID from order_details D, Order_table O where O.OID = D.OID and O.CustId = Customer.CustID))

4) List the names of customers who have the balance higher than the average balance of all of the customers. (15 points)

Select FirstName, LastName From Customer C,

(Select Avg(Balance) A_Bal from Customer) T_Cust)Where C.balance > T_Cust.A_Bal.

CustomerCustID

Last Name

First Name

Street City State Zip Code

Balance

C1 Block Jane 345 Randolf Circle Oxford MS 38655 $3.00C2 Hamilton Jim 123 Lamar Tupelo MS 38804 $5.00C3 Williams John 225 Redbuds Oxford MS 38655 $6.00C5 Brown Carol 729 Washington Chicago IL 60615 $7.00C6 White Tim 295 Dogwood University MS 38677 $4.00C7 Swan Jerry 539 Cedar Clinton MS 36772 $5.00C8 Twain Mark 443 Jackson Batesville MS 38611 $2.00C9 Jane Doe 742 University Av Oxford MS 38655 $8.00

ProductProductID ProduceName Price

($)ProducedIn

P1 27” Monitor 300 Oxford, MSP2 2 TB Hard Drive 150 Tupelo, MSP3 8 GB Ram 50 Memphis, TNP4 Keyboard 30 Chicago, ILP5 Mouse 20 Oxford, MS

Order_Table (Years 2011-2012)OID CustID Date_Ordered101 C1 5/3/12102 C2 2/7/12103 C3 5/6/11104 C1 3/2/12105 C2 1/2/11106 C3 5/3/12

Order_DetailsOID PID Qty_Ordered101 P1 2101 P5 1102 P3 3102 P2 5103 P3 5104 P2 2104 P2 1105 P3 3105 P4 5106 P1 2106 P5 4