You are on page 1of 12
Consider the Sailors-Boats-Reserves Database as below: sailors (sid, sname, rating, age) boats (bid, bname, color) reserves (sid, bid, date) 1. Find the colors of boats reserved by Sachin SELECT color FROM s, b, r WHERE rsid=s.sid AND r.bid=b.bid AND sname=sachin’ 2.Find all sailor id's of sailors who have a rating of at least 8 or reserved boat 103 (SELECT sid FROM s WHERE rating>=8) UNION (SELECT sid FROM r WHERE bid=103) 3. Find the names of sailors who have not reserved a boat whose name contains the string “storm”. Order the names in ascending order. SELECT sname FROM s sl WHERE sid NOT IN (SELECT sid FROM r,s WHERE rsid=s.sid AND sname LIKE ‘%storm%’) ORDER BY sl.sname 4, Find the sailor id's of sailors whose rating is better than some sailor called Bob. SELECT s1 sid FROM ssl, s 62 WHERE sl rating>s2 rating AND s2 sname="Bob (Alternatively: ) SELECT sid FROMs WHERE rating > any ( SELECT rating FROM s s2 WHERE s2.sname='Bob’)) Consider the Employee Database as below: Employee (emp_name, street, city, date_of_joining) Works(emp_name, company_name, salary) Company (company_name, city) Manager (emp_name, manager_name) 1.Modify the database so that “Deepa” lives in “Pune” UPDATE Employee SET city="Pune’ Where emp_name= Deepa 2.Give all employees of “XYZ corporation” a 10% rise in salary UPDATE Works SET salary=salary*1.10 WHERE company_name='XYZ corporation’ 3.Display all employees who joined in the month of “March” SELECT emp_nameFROM Employee WHERE date_of_joining =;MARCH' 4.Find all employees who earn more than average salary of all the employees of their company SELECT emp_name, salary FROM Employee.e, Works.w ON e.emp_name=w.emp_name WHERE w.salary>avg(w.salary) 5.List all employee who lives in the same city as their company city SELECT emp_name FROM Employee e, Company c, Works w WHERE e.empname = wempname &&w.company_name = c.company_name S&e.city = c.city Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer, fname: string, deptid: integer) ‘Find the names of all Juniors (level = JR) who are enrolled in a class taught by Metin SELECT DISTINCT S.Sname FROM Student S, Class C, Enrolled E, Faculty F WHERE S.snum = E.snum AND E.cname = C.name AND C.fid = F.fid AND F.fname = ‘Mr.nitin’ AND S.level =‘ JR 2.Find the names of faculty members for whom the combined enrollment of thecourses that they teach is less than five. 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 = Fefid 3.Pind the names of students not enrolled in any class.. SELECT DISTINCT $ sname FROM Student $ WHERE .snum NOT IN (SELECT E.snum FROM Enrolled E) 4Find the names of faulty member who teach in every room in which some cassis taut SELECT fname fd FROM faculty WHERE ffi in SELECT fd FROM class GROUP BY fd HAVING COUNT {SELECT COUNTIDISTNCT rom) FROM clas) Consider the following relations: 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) ‘Foreah ott cere for moethanthree ari fthe ed ad he menmum crusangct te actor ticelste cert SECT ceil cusnyane) FROM cert cateata WHERE cata GROLP BY ce KANG COUTTS ‘Find the names of l lots whose salary i ess than the prio of the cheapest route from los Angeles to Honolulu SELECTDISTINCT eename FROM employees ¢ WHERE e selay(SELECT MING pric) FROM fight f WHERE Ffrm=los Angeles’ AND fioc:honllu), 4 Find the names of plots cere for some Boeing ara, SELECT distinct eename FROM employees aircraft acertied c WHERE eetceceld AND cald-aad AND aaname= Dosing, 4Find thea’ of lace hat canbe used on routes fom Los Angals fo Chicago, SELECT aid FROM arcraft a WHERE a cruisnqrange> (SELECT MING tance) FROM fight fMHERE ffm Bangalore AND ttoo-Delh) EMPLOYEE(FName, Minit, LName, Ssn, Bdate, Address, Sex, Salary, super-ssn, Dno) DEPARTMENT(Dname, Dnumber, Mgr_ssn, Mgr_st_date) DEPART_LOCATIONS(Dnumber, Dlocation) PROJECT(Pname, Pnumber, Plocation, Dnum) WORKS_ON(Essn, Pno, Hours) DEPENDENT(Essn, Dependent_name, Sex, Bdate, Relationship) Find the names of employees who work on all the projects controlled by department number 5. DEPT5_PROJS < p(Pno) (mPnumber(oDnum=5(PROJECT))) EMP_PROJ < p(Ssn, Pno) (mEssn, Pno(WORKS_ON)) RESULT_EMP_SSNS <- EMP_PROJ + DEPT5_PROJS RESULT < ttLname, Fname(RESULT_EMP_SSNS * EMPLOYEE) List the names of all employees with two or more dependents. Strictly speaking, this query cannot be done in the basic (original) relational algebra. We have to use the AGGREGATE FUNCTION operation with the COUNT aggregate function. We assume that dependents of the same employee have distinct Dependent_name values. T1(Ssn, No_of_dependents)< Essn J COUNT Dependent_name(DEPENDENT) T2 <— oNo_of_dependents>2(T1) RESULT < mLname, Fname(T2 * EMPLOYEE) Retrieve the names of employees who have no dependents. This is an example of the type of query that uses the MINUS (SET DIFFERENCE) operation. ALL_EMPS < mSsn(EMPLOYEE) EMPS_WITH_DEPS(Ssn) <— mtEssn(DEPENDENT) EMPS_WITHOUT_DEPS < (ALL_EMPS -— EMPS_WITH_DEPS) RESULT < ttLname, Fname(EMPS_WITHOUT_DEPS * EMPLOYEE) © Omumbir, brame , balance brome , amoustt) otter ( crame ~ anamber Toormyca b Cranne Loonanunter, fund att » modo Se ea " Kamsow Tethy = Warnicon' ban Tr “ f } race ane = Tima” Obama, lumbar = Loan Wakacha PTO Fd the pone, of alt cWwhymens wile roe a oan at “Wve foryudge branch tot do A howe occouul of ony cue et. dune fra 1 Co, C 4 Cra ce = Ra CT Banner ama ba = RR Coa POTTOOT Sbeare)}} Cop oxFe J Srillcnenas find Hy IO fied ‘Hose patoness diet ong not largest = Rename accowd prilelion ao Kae Wwe Con compre. ach accout bale with ait ofr “tt BE ate tela = Wi bnlonca CAC Ky OIONAY eng ©) Une st differs to fued Hatrae wet found wn Se tantie, sep een i Wilference hohe Tha Catiwt) aod baton elatic = Wed ott cpl MG Uwe dole [ease 2) ee Quan Sloe, ILmode by the “tewpsidge i bine = “oy 2 BN AMARF> 1200 papergrid Date: / | las ¢fict-te toms of a chm lve have a {oan aud on Gent od bark. I ppt.) Tag Ais) i ams haa bud no Lown fom Se bovek T depts) - — Ubosrones)

You might also like