Vous êtes sur la page 1sur 10

Relational Algebra

Example 1:
Example 2:

Consider the relation schemas as follows.


works(person name, company name, salary);
lives(person name, street, city);
located in(company name, city);
managers(person name, manager name);
where manager name refers to person name.
1. Find the names of the persons who work for company ’FBC’ (company name=’FBC’).

2. List the names of the persons who work for company ’FBC’ along with the cities they live in.

3. Find the persons who work for company ’FBC’ with a salary of more than 10000. List the names of these
persons along with the streets and cities where they live.

4. Find the names of the persons who live and work in the same city.

5.Find the names of the persons who live in the same city and on the same street as their managers.

6.Find the names of the persons who do not work for company ’FBC’.

7. Find the persons whose salaries are more than the salary of everybody who work for company ’SBC’.
8. Find the names of the companies that is located in every city where company ’SBC’ is located in.

Relational algebra (another solution):

Example 3:
Consider the following tables:
Example 4:

Consider the following tables:


student(id, name)
enrolledIn(id, code)
subject(code, lecturer)

1. What are the names of students enrolled in cp3020?


Solution: name ( cp3020=code (student enrolledIn))

2. Which subjects is Hector taking?

Solution: code( (student


name=Hector enrolledIn))

3. Who teaches cp1500?

Solution: lecturer ( code=cp1500 (subject))

4. Who teaches cp1500 or cp3020?

Solution: lecturer ( code=cp1500 OR code=cp3020(subject))


5. Who teaches at least two different subjects?

Solution: For this query we have to relate subject to itself. To disambiguate the relation,
we will call the subject relation R or S.

(
lecturer R.lecturer = S.lecturer AND R.code <> S.code (R S))

6. What are the names of students in cp1500 or cp3010?

Solution: name ( code=cp1500 (student enrolledIn)) name ( code=cp3010 (student enrolledIn))

7. What are the names of students in both cp1500 and cp1200?

Solution: name ( code=cp1500 (student enrolledIn)) name ( code=cp3010 (student enrolledIn))

8. What are the names of students in at least two different subjects?

Solution: For this query we have to relate enrolledIn to itself. To disambiguate the relation,
we will call the enrolledIn relation R orS.

name (student ( (R
R.id = S.id AND R.code <> S.code S)))

9. What are the codes of all the subjects taught?

Solution: code (subject)

10. What are the names of all the students?

Solution: name (student)

11.What are the names of all the students in cp1500?

Solution: name ( code=cp1500 (student enrolledIn))

12.What are the names of students taking a subject taught by Roger.

Solution: name ( lecturer=Roger (student enrolledIn subject))

13. What are the names of students who are taking a subject not taught by Roger?

Solution: name ( (student


lecturer <> Roger enrolledIn subject))
Example 5:
Consider the following tables:
movie(movieName, whenMade)
star(starName, age)
studio(studioName, where)
produces(studioName, movieName)
starsIn(starName, movieName)

1. When was the movie Sneakers made?

Solution: whenMade( (movie))


movieName = Sneakers

2. Who stars in Sneakers?

Solution: starName( (starsIn))


movieName = Sneakers

3. Which stars that are over 40 appear in Sneakers?

Solution: (
starName age > 40 AND movieName = Sneakers (star starsIn))

4. Which stars do not appear in Sneakers?

Solution: starName(star) - starName ( (starsIn)))


movieName = Sneakers

5. Which studio produces Sneakers?

Solution: studioName ( movieName = Sneakers (produces))

6. What are the names of stars who star in movies produced by studios located inBurbank?

Solution: starName( where = Burbank(studio produces starsIn))

Example 6:

Consider the following tables:

Employee(Fname,Minit,Lname,SSn,Bdate,Adress,Sex,Salary,Super_ssn,Dno)
Department(Dname,Dnumber,Mgr_ssn,Mgr_start_date)
Dept_Locations(Dnumber,Dlocation)
Project(Pname,Pnumber,Plocation,Dnum)
Works_On(Essn,Pno,Hours)
Dependent(Essn,Dependent_name,Sex,Bdate,Relationship)

Vous aimerez peut-être aussi