Sunteți pe pagina 1din 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).

!. "ist the names of the persons who work for company FBC along with the cities they live in.

#. Find the persons who work for company FBC with a salary of more than 1$$$$. "ist the names of these persons along with the streets and cities where they live.

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

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

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

(. Find the persons whose salaries are more than the salary of every)ody who work for company *BC.

+. Find the names of the companies that is located in every city where company *BC is located in.

,elational alge)ra (another sol-tion).

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

cp#$!$ code

(st-dent

enrolled/n))

2. Which subjects is Hector taking? Solution.


code

name 0ector

(st-dent

enrolled/n))

#. 1ho teaches cp1&$$2


Solution.
lect-rer

code cp1&$$

(s-)3ect))

%. 1ho teaches cp1&$$ or cp#$!$2


Solution.
lect-rer

code cp1&$$ 4, code cp#$!$

(s-)3ect))

&. 1ho teaches at least two different s-)3ects2


Solution. For this 5-ery we have to relate subject to itself. 6o disam)ig-ate the relation, we will call the subject relation R or S.
lect-rer

R.lect-rer

S.lect-rer 789 R.code :; S.code

(R

S))

'. 1hat are the names of st-dents in cp1&$$ or cp#$1$2


Solution.
name

code cp1&$$

(st-dent

enrolled/n))

name

code cp#$1$

(st-dent

enrolled/n))

(. 1hat are the names of st-dents in )oth cp1&$$ and cp1!$$2


Solution.
name

code cp1&$$

(st-dent

enrolled/n))

name

code cp#$1$

(st-dent

enrolled/n))

+. 1hat are the names of st-dents in at least two different s-)3ects2


Solution. For this 5-ery we have to relate enrolledIn to itself. 6o disam)ig-ate the relation, we will call the enrolledIn relation R orS.
name

(st-dent

R.id

S.id 789 R.code :; S.code

(R

S)))

<. 1hat are the codes of all the s-)3ects ta-ght2


Solution.
code

(s-)3ect)

1$. 1hat are the names of all the st-dents2 Solution.


name

(st-dent)

11.1hat are the names of all the st-dents in cp1&$$2


Solution.
name

code cp1&$$

(st-dent enrolled/n))

1!.1hat are the names of st-dents taking a s-)3ect ta-ght )y ,oger.


Solution.
name

lect-rer ,oger

(st-dent

enrolled/n

s-)3ect))

1#. 1hat are the names of st-dents who are taking a s-)3ect not ta-ght )y ,oger2 Solution.
name

lect-rer :; ,oger

(st-dent

enrolled/n

s-)3ect))

Example 5:

Consider the following tables:


movie(movieName, whenMade) star(starName, age) studio(studioName, where) produces(studioName, movieName) starsIn(starName, movieName) 1. 1hen was the movie Sneakers made2 Solution.
whenMade

movieName = Sneakers

(movie))

!. 1ho stars in Sneakers2 Solution.


starName

movieName = Sneakers

(starsIn))

#. 1hich stars that are over %$ appear in Sneakers2 Solution.


starName

age >

! AND movieName = Sneakers

(star

starsIn))

%. 1hich stars do not appear in Sneakers2 Solution.


starName

(star) "

starName

movieName = Sneakers

(starsIn)))

&. 1hich st-dio prod-ces Sneakers2 Solution.


studioName

movieName = Sneakers

(produces))

'. 1hat are the names of stars who star in movies prod-ced )y st-dios located inBurbank2 Solution.
starName

where = #urbank

(studio

produces

starsIn))

Example 6:
Consider the following tables:
$mplo%ee(&name,Minit,'name,SSn,#date,(dress,Se),Salar%,Super*ssn,+no) +epartment(+name,+number,Mgr*ssn,Mgr*start*date) +ept*'ocations(+number,+location) ,roject(,name,,number,,location,+num) -orks*.n($ssn,,no,/ours) +ependent($ssn,+ependent*name,Se),#date,0elationship)

S-ar putea să vă placă și