0% found this document useful (0 votes)
715 views4 pages

Assignment of Relational Algebra With Solutions

The document describes example queries using relational algebra on relations like BOOKS, STUDENTS, AUTHORS. It lists queries to: 1) List the year and title of each book 2) List information about students whose major is CS 3) Describe composed queries using relational algebra operations like selection, projection, renaming, and set operations.

Uploaded by

shahzaibali39390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
715 views4 pages

Assignment of Relational Algebra With Solutions

The document describes example queries using relational algebra on relations like BOOKS, STUDENTS, AUTHORS. It lists queries to: 1) List the year and title of each book 2) List information about students whose major is CS 3) Describe composed queries using relational algebra operations like selection, projection, renaming, and set operations.

Uploaded by

shahzaibali39390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ECS-165A WQ’11 52

Example Queries

Assume the following relations:

BOOKS(DocId, Title, Publisher, Year)


STUDENTS(StId, StName, Major, Age)
AUTHORS(AName, Address)
borrows(DocId, StId, Date)
has-written(DocId, AName)
describes(DocId, Keyword)

• List the year and title of each book.


πYear, Title(BOOKS)

• List all information about students whose major is CS.


σMajor = ’CS’(STUDENTS)

• List all students with the books they can borrow.


STUDENTS × BOOKS

• List all books published by McGraw-Hill before 1990.


σPublisher = ’McGraw-Hill’∧Year<1990(BOOKS)

Dept. of Computer Science UC Davis 3. Relational Model and Relational Algebra


ECS-165A WQ’11 53

• List the name of those authors who are living in Davis.


πAName(σAddress like ’%Davis%’(AUTHORS))

• List the name of students who are older than 30 and who are
not studying CS.
πStName(σAge>30(STUDENTS)) −
πStName(σMajor=’CS’(STUDENTS))

• Rename AName in the relation AUTHORS to Name.


ρAUTHORS(Name, Address)(AUTHORS)

Dept. of Computer Science UC Davis 3. Relational Model and Relational Algebra


ECS-165A WQ’11 54

Composed Queries (formal definition)

• A basic expression in the relational algebra consists of either


of the following:
– A relation in the database
– A constant relation
(fixed set of tuples, e.g., {(1, 2), (1, 3), (2, 3)})

• If E1 and E2 are expressions of the relational algebra, then the


following expressions are relational algebra expressions, too:
– E1 ∪ E2
– E1 − E2
– E1 × E2
– σP (E1) where P is a predicate on attributes in E1
– πA(E1) where A is a list of some of the attributes in E1
– ρx(E1) where x is the new name for the result relation
[and its attributes] determined by E1

Dept. of Computer Science UC Davis 3. Relational Model and Relational Algebra


ECS-165A WQ’11 55

Examples of Composed Queries

1. List the names of all students who have borrowed a book and
who are CS majors.
πStName(σSTUDENTS.StId=borrows.StId
(σMajor=’CS’(STUDENTS) × borrows))

2. List the title of books written by the author ’Silberschatz’.


πTitle(σAName=’Silberschatz’
(σhas-written.DocId=BOOKS.DocID(has-written×BOOKS)))
or
πTitle(σhas-written.DocId=BOOKS.DocID
(σAName=’Silberschatz’(has-written) × BOOKS))

3. As 2., but not books that have the keyword ’database’.


. . . as for 2. . . .
− πTitle(σdescribes.DocId=BOOKS.DocId
(σKeyword=’database’(describes) × BOOKS))

4. Find the name of the youngest student.


πStName(STUDENTS) −
πS1.StName(σS1.Age>S2.Age(ρS1(STUDENTS)×ρS2(STUDENTS)))

5. Find the title of the oldest book.


πTitle(BOOKS) −
πB1.Title(σB1.Year>B2.Year(ρB1(BOOKS) × ρB2(BOOKS)))

Dept. of Computer Science UC Davis 3. Relational Model and Relational Algebra

You might also like