Docsity

Relational Algebra (PDF), Summaries of Computer Science

Toggle sidebar
bg1
Practice Queries
CS 4604 (Fall 2008)
September 02, 2008
Consider the following tables.
student enrolledIn subject
id | name id | code code | lecturer
------------------ -------------------- ---------------------
1234 | joe 1234 | cs1500 cs1500 | curtis
4000 | hector 1234 | cs1200 cs2001 | dave
2000 | ling 1234 | cs2001 cs3010 | curtis
4000 | cs3010 cs2001 | olivier
4000 | ma3000 ma3000 | roger
Figure out which relational algebra operations were used to obtain each of the following tables.
1.
name
---------
joe
hector
ling
2.
lecturer
---------
curtis
dave
olivier
roger
3.
code | lecturer
---------------------
cs3010 | curtis
cs1500 | Curtis
There are two ways to get this table. Try to list both. Hint: Use an OR in the selection
condition for one method.
pf3
pf4
pf5

Partial preview of the text

Download Relational Algebra (PDF) and more Summaries Computer Science in PDF only on Docsity!

Practice Queries

CS 4604 (Fall 2008)

September 02, 2008

Consider the following tables.

student enrolledIn subject id | name id | code code | lecturer


1234 | joe 1234 | cs1500 cs1500 | curtis 4000 | hector 1234 | cs1200 cs2001 | dave 2000 | ling 1234 | cs2001 cs3010 | curtis 4000 | cs3010 cs2001 | olivier 4000 | ma3000 ma3000 | roger

Figure out which relational algebra operations were used to obtain each of the following tables.

name

joe hector ling

lecturer

curtis dave olivier roger

code | lecturer

cs3010 | curtis cs1500 | Curtis

There are two ways to get this table. Try to list both. Hint : Use an OR in the selection

condition for one method.

id | name

1234 | joe 4000 | hector

There are three ways to get this table. Hint : How about using the difference operator?

id | name | id | code

1234 | joe | 1234 | cs 1234 | joe | 1234 | cs 1234 | joe | 1234 | cs 1234 | joe | 4000 | cs 1234 | joe | 4000 | ma

id | name | id | code

1234 | joe | 1234 | cs 1234 | joe | 1234 | cs 1234 | joe | 1234 | cs

id | name | code

1234 | joe | cs 1234 | joe | cs 1234 | joe | cs

id | code

1234 | cs 1234 | cs 1234 | cs

id | name | code | lecturer

4000 | hector | cs3010 | curtis 4000 | hector | ma3000 | roger

name | lecturer

joe | curtis hector | curtis

Solution : Many ways are possible. Here is one.

student student.id = enrolledIn.id enrolledIn

Solution : Many ways are possible. Here is one.

name=joe(student^ enrolledIn)

Solution :

id,code(^ name=joe (student^ enrolledIn)

id=1234(enrolledIn)

Solution : ( name=hector(student)) enrolledIn subject

Solution : name, lecturer( lecturer=curtis (subject enrolledIn student))

Formulating Queries in Relational Algebra

Give the following queries in the relational algebra using the relational schema

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

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

2. Which subjects is Hector taking?

3. Who teaches cs1500?

4. Who teaches cs1500 or cs3020?

5. Who teaches at least two different subjects?

6. What are the names of students in cs1500 or cs3010?

7. What are the names of students in both cs1500 and cs1200?

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

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

10. What are the names of all the students?

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

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

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

Solution : For this query we have to relate enrolledIn to itself. To disambiguate the

relation, we will call the enrolledIn relation R or S.

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

Solution : code(subject)

Solution : name(student)

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

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

Solution : name( lecturer < > Roger(student enrolledIn subject))

Acknowledgement

Thanks to Curtis Dyreson for providing the problems