Algebra Questions and Answers
Algebra Questions and Answers
Question 1 Write the following queries using the relational algebra and show the result of each query as it applies to the tables below. You are not allowed to use any aggregates or the division operator. S(S#, SNAME, STATUS, CITY) //Supplier relation P(P#, PNAME, COLOR, WEIGHT, CITY) //Part relation J(J#, JNAME, CITY) //Job relation SPJ(S#, P#, J#, QTY) //Suppliers of Parts for Jobs relation
1. Find all suppliers with status 30 that supply part P2. For each supplier, output the name and the quantity of part P2 they supply. 2. Find and output the supplier numbers for all suppliers who either supply a Blue part or supply to a job in Athens. Question 2 You are given the following relational schema for family relations.: Person ( PID, PersonName, Sex, Cityofbirth ) Parent ( ParentID, ChildID ) // both ParentID and ChildID are foreign keys referring to Person.PID Write the following queries in relational algebra using only selection, projection, join, cartesian product, set union, intersection and dierence. a. Find the name of grandparents of all people who were born in New York city. b. Find the name of all people who were born in the same city as their father. Question 3 You are given the following relational schema for students. Student( SID, Sname, Curriculum ) Takes ( CourseID, SID, Semester, Grade ) //Semester is of the form Fall 98, Spring 99, etc. Write the following queries in relational algebra using only selection, projection, join, cartesian product, set union, intersection and dierence. a. Find the id of all students who took the same course in two dierent semesters. b. Find the name of all students who never got an F. Question 4 School Database: STUDENT( sid, lastname, rstname, major, entrydate ) 1
COURSE( course number, dept id, course name) DEPARTMENT( dept id, department name, address ) INSTRUCTOR( instructor id, rstname,lastname, dept id ) SECTION ( section id, section number, course number, dept id, semester, instructor id ) TRANSCRIPT( sid, section id, grade ) REQUIREMENT( req id, major, course number, dept id ) The above database contains information about a school. Information about students are stored in the STUDENTS relation. All courses oered by all departments are listed in the COURSE relation. The DEPARTMENT relation contains various departments in the school and INSTRUCTOR contains the dierent instructors from dierent departments. A course may be oered in multiple sections in a given semester. This information is stored in the SECTION relation. The TRANSCRIPT contains the information about when a student took a specic section of a course and his or her grade. If a student is currently taking a course, then the grade attribute will have a null value. Finally, all the courses students are required to take for a given major are listed in the REQUIREMENT relation. Given the above relational schema, write the following queries using relational algebra. Try to use compact expressions for your queries. 1. Find the last name, rst name and major of all students taking any section of the course 4380 oered by the Computer Science department in Fall 99. 2. Find the rst name and last name of all instructors who taught the course 4380 oered by the Computer Science department. 3. Find the number and department identier of all courses in which no student ever got an F. 4. Find the identier of all instructors who are teaching at least three sections of a course in the same semester. 5. Find the identier of all students who took all the required courses for their majors -even if they do not have a grade for some of the courses yet. Question 5 EMPLOYEE( ssn, rstname, lastname, title, salary, dept id, mgr id) DEPARTMENT( dept id, departmentname, totalbudget) Use the two relations given above to write the following queries using relational algebra: 1. Find the rst and last name of managers such that none of his/her employees work in the same department as their boss (i.e. the department of all employees is dierent than the department of their boss). 2. Find the name of all departments that has no employee working in them. Question 6 Given BOOKS( ISBN, Author, Title, Publisher, Publish Date, Pages, Notes ) STORE( Store Id, Store Name, Street, State, City, Zip ) STOCK( ISBN, Store Id, Price, Quantity ) Use the three relations above to write the following queries using relational algebra: 2
1. Find the identier of all stores that carry a non-zero quantity of every book in the BOOKS relation. 2. Find the name and address of all stores that do not carry any books by J.D. Salinger. Question 7 You are given the following set of relations for an Olympic database for events involving competition among individual athletes. All events in this database measure success by the shortest time (such as running). Athlete(id, name, birthdate, country, height, weight) Sports(id, name, description, category, mw) Participate(athlete, sport, pr) Records(type, sport, recordtime, athlete, date) Sports.category is one of running, swimming, Sports.mw refers to men or women events. Participate.pr is the personal record of an athlete for a sport. The pr of an athlete may be better than the current records (just not been set at a race). Records.type is one of WR/OR for World and Olympic records. Records.sport, Participate.sport are foreign keys pointing to Sports.id Records.athlete, Participate.athlete are foreign keys pointing to Athlete.id Write the following queries in Relational Algebra: 1. Find all athletes who participate in only one sport. Return their name. 2. Find all record holder athletes whose personal record time is greater than their record. Return the name of the athletes. 3. Find all athletes who are not record holders for a specic sport and are faster than at least one of the current record holders for this sport. Return the name of the athlete and the name of the sport. 4. Find all pairs of sports S1, S2 such that there does not exist an athlete that participate in both S1 and S2. Return pairs of sports ids. Do not return repetitive tuples such as (1,2) and (2,1). Question 8 In this question, you are given the following simple database of employees that work in specic departments. Each department has an inventory of items with specic quantity. EMPLOYEE(ssn, rst-name, last-name, address, date-joined, supervisor-ssn) DEPARTMENT(dept-no, name, manager-ssn) WORKS-IN(employee-ssn, dept-no) INVENTORY(dept-no, item-id, quantity) ITEMS(item-id, item-name, type)
Foreign keys: 1. EMPLOYEE.supervisor-ssn and WORKS-IN.employee-ssn point to EMPLOYEE.ssn. 2. WORKS-IN.dept no and INVENTORY.dept-no point to DEPARTMENT.dept no. 3. INVENTORY.item-id points to ITEMS.item-id.
1. You are given the below relational algebra query. Write an SQL query that returns the same answer as R5 below. Explain your answer by writing the meaning of this query in English. R1 := W ORKS IN [ssn, dept no] R2 := W ORKS IN [ssn2, dept no2] R3 := (EM P LOY EE ssn=ssn R1) R2 R4 := supervisorssn=ssn2 AND deptno=deptno2 R3 R5 := f irstname,lastname R4 2. You are given the below relational algebra query. Write an SQL query that returns the same answer as R5 below. Explain your answer by writing the meaning of this query in English. R1 := (itemid IT EM S ) (itemid IN V EN T ORY ) R2 := IT EM S itemid=itemid R1 R3 := type=CD R2 R4 := itemname R3 Question 9 You are given the simple relational database below that shows students, courses they took and courses they are required to take. Student(ssn, name, major, year) // lists all students Course (cid, name, description, department) // lists course information Oered(cid, semester, section, ins id, location) // lists all course oerings for dierent semesters Instructor(ins id, name, department) // lists all instructors Took(ssn, cid, semester, section, grade) // lists when a student took a course Required(major, cid) // lists which courses are required for a specic major Took.cid, Required.cid Oered.cid are foreign keys, they refer to the course.cid. Oered.ins id is a foreign key, refers to Instructor.ins id. Took.ssn is a foreign key, refers to Student.ssn. Write the following queries in relational algebra using only set union, intersection, dierence, select, project and Cartesian product operations:
1. Find the name of all instructors who have oered at least one course. 2. Find all students who have taken the same course (given by cid) twice. 3. Find the name of all courses (from Course) that have never been oered according to Oered. 4. Return pairs of student name and course names such that the student is required take this course for his/her listed major.
5. Use your answer from part d to nd all students who have taken all the required courses for their major. Question 10 You are given the below relational model. Artist( ArtistName, ArtistYear, Origin, ArtistTones, Biography ) SimilarArtist(FromArtistName, FromArtistYear,ToArtistName, ToArtistYear) Song( SongId, SongName, DateComposed ) Album( AlbumId, AlbumTitle, Duration, Rating, Review, ReleaseDate, AlbumTones, ArtistName, ArtistYear, LabelName ) Track( AlbumId, TrackId, Recommended, SongId ) LyricsBy( SongId, ArtistName, ArtistYear ) MusicBy( SongId, ArtistName, ArtistYear ) MajorArtist( ArtistName ArtistYear, LabelName ) Foreign keys: Album(ArtistName, ArtistYear) references Artist(ArtistName, ArtistYear) SimilarArtist(FromArtistName, FromArtistYear) references Artist(ArtistName, ArtistYear) SimilarArtist(ToArtistName, ToArtistYear) references Artist(ArtistName, ArtistYear) Track(AlbumId) references Album(AlbumId) Track(SongId) references Song(SongId) LyricsBy(SongId) references Song(SongId) MusicBy(SongId) references Song(SongId) LyricsBy(ArtistName, ArtistYear) references Artist(ArtistName, ArtistYear) MusicBy(ArtistName, ArtistYear) references Artist(ArtistName, ArtistYear) MajorArtist(ArtistName, ArtistYear) references Artist(ArtistName, ArtistYear) Write the following queries using relational algebra. 1. Find all albums with 20 or more tracks, return the title and the duration of the album. 2. Find all artists that are similar to the artist named Prince return the name and year founded of these artists. 3. Find all artists who have never contributed to a song (either with lyrics or music). Return the name of the artist. 4. Find all albums that feature a song by Prince (composed by or lyrics by). 5. Find artists who are considered major artists for all labels that they recorded with. Return name, origin and biography of the artist (Hint. Use Album and MajorArtist relations rst). 6. Find all songs that have never been recorded in an album. Return the name and dateComposed for these songs. 7. Find all albums such that all tracks in that album are recommended. If a track is recommended, then the Track.Recommended attribute has value y. Return the title of the album, duration and its rating. 8. Find all artists that have never recorded a song composed by themselves (use MusicBy relation only). Return the name, year of the artist.
Question 11 Given: ITEMS(iid, oid, name, location) BIDS(bidid, iid, buyid, amount, date) BUYERS(buyid, name, email) OWNERS(oid, name, email, status) Write the following queries in relational algebra. 1. Find all owners who never received bids of $25 or more for one their items. Return the name and email of owners. 2. Find all buyers who placed bids for the same amount on two dierent items. Return the name of the buyer, the amount and the name of the two items. Question 12 Consider the relations below: Passengers (Name, Address, Age) Reservations(Name, FlightNum, Seat) Flights(FlightNum, DepartCity, DestinationCity, DepartureTime, ArrivalTime, MinutesLate) 1. Get the names of passengers who had a reservation on a ight that was more than 30 minutes late. 2. Get the names of passengers who had reservations on all ights that were more than 60 minutes late. 3. Get the names of pairs of passengers who are the same age.