0% found this document useful (0 votes)
388 views5 pages

Querying Using Relational Algebra

The Author relation gives the author's first and last names and address. Each first name/last name pair is unique. The Book relation gives the isbn, title, copyright date, publication date, and recommended list price. Since a book can have multiple authors, the relation BookAuthor matches up authors (identified by name) with books (identified by ISBN). The Bookstore relation gives the name (key) and address of a book store. The Stock relation gives the bookstore name, the price, and the ISBN number of the book. listPrice and storePrice are real numbers. copyright and pubDate are integers (representing the year). All other attributes are strings

Uploaded by

Carlo Hutchinson
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)
388 views5 pages

Querying Using Relational Algebra

The Author relation gives the author's first and last names and address. Each first name/last name pair is unique. The Book relation gives the isbn, title, copyright date, publication date, and recommended list price. Since a book can have multiple authors, the relation BookAuthor matches up authors (identified by name) with books (identified by ISBN). The Bookstore relation gives the name (key) and address of a book store. The Stock relation gives the bookstore name, the price, and the ISBN number of the book. listPrice and storePrice are real numbers. copyright and pubDate are integers (representing the year). All other attributes are strings

Uploaded by

Carlo Hutchinson
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/ 5

COSC 304 - Introduction to Database Systems

Lab 1: Querying using Relational Algebra


This lab has a practice component and an assignment component. To skip directly to the assignment,
click here.

Relational Algebra Practice


In this lab, we will practice writing queries in relational algebra. The database schema we will use stores information
on books:
Book (isbn, pubDate, listPrice, publisher, title, copyright)
Author (firstName, lastName, address)
BookAuthor (isbn, firstName, lastName)
Bookstore (name, address)
Stock (storeName, isbn, storePrice, quantity)

The Author relation gives the author's first and last names and address. Each first name/last name pair is unique.
The Book relation gives the isbn, title, copyright date, publication date, and recommended list price. Since a book
can have multiple authors, the relation BookAuthor matches up authors (identified by name) with books (identified by
ISBN). The Bookstore relation gives the name (key) and address of a book store. The Stock relation gives the
bookstore name, the price, and the ISBN number of the book. listPrice and storePrice are real numbers. copyright
and pubDate are integers (representing the year). All other attributes are strings.
Questions: (Write the answer in relational algebra)
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

1.
2.
3.
4.
5.
6.
7.
8.
9.

Return all books (ISBN only) with a list price over $50.
Return all books (ISBN only) with a publish date before Jan. 11, 2001 or whose publisher is 'GenCo'.
Return all addresses (both for authors and bookstores).
Return all authors that have not published a book.
Return the list of books written by Joe Smith.
Return the list of books written by Joe Smith that cost less than $40.
Find all authors (firstName, lastName) who have written books that have been published after Feb. 15, 1990.
Find all authors who have written more than one book.
Find pairs of books with different ISBNs but the same title. A pair should be listed only once; e.g., list (i,j) but
not (j,i).
10. List all the books (ISBN only) that 'All Books' sells that 'Some Books' also sells.
11. List all the books (ISBN only) that 'All Books' sells that 'Some Books' sells for less.
12. Open question: You suggest an English question, and let's try answer it using relational algebra. You do not
have to have an answer to your own question, but hopefully, you think you can answer it. Also, note that we
cannot answer all questions using the subset of relational algebra that we have studied.
Answers:
1.
2.
3.
4.
5.
6.
7.
8.

isbn(listPrice > 50(Book))


isbn(pubDate < '2001-01-11' OR publisher = 'GenCo'(Book))
address (Author) address (Bookstore)
firstName, lastName(Author) - firstName, lastName(BookAuthor)
isbn(firstName = 'Joe' and lastName = 'Smith'(BookAuthor))
isbn(firstName = 'Joe' and lastName = 'Smith' and listPrice < 40(BookAuthor Book))
firstName, lastName(pubDate > '1990-02-15'(BookAuthor Book))
B1.firstName, B1.lastName(B1.firstName = B2.firstName AND B1.lastName = B2.lastName AND B1.isbn !=
B2.isbn(BookAuthor B1 X BookAuthor B2))
With a join:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

B1.firstName, B1.lastName(BookAuthor B1 B1.firstName = B2.firstName AND B1.lastName = B2.lastName AND B1.isbn


!= B2.isbn BookAuthor B2)
9. B1.isbn, B2.isbn(B1.title = B2.title AND B1.isbn != B2.isbn AND B1.isbn < B2.isbn(Book B1 X Book B2))
With a join:
B1.isbn, B2.isbn(Book B1 B1.title = B2.title AND B1.isbn != B2.isbn AND B1.isbn < B2.isbn Book B2)
10. isbn(storeName = 'All Books'(Stock)) isbn(storeName = 'Some Books'(Stock))
OR
S1.isbn(S1.storeName = 'All Books' AND S2.storeName = 'Some Books' and S1.isbn=S2.isbn(Stock S1 X Stock S2))
OR
S1.isbn(S1.storeName = 'All Books' (Stock S1) S1.isbn=S2.isbn S2.storeName = 'Some Books'(Stock S2))
11. S1.isbn(S1.isbn = S2.isbn AND S2.storePrice < S1.storePrice AND S1.storeName = 'All Books' AND S2.storeName = 'Some
Books'(Stock S1 X Stock S2))
OR
S1.isbn(S1.storeName = 'All Books'(Stock S1) S1.isbn = S2.isbn AND S2.storePrice < S1.storePrice (S2.storeName =
'Some Books'(Stock S2))
Note: Cross-products ('X') should be replaced with joins for efficiency.
Challenge questions:
1. Find all the books published by Harper Collins that are in-stock at Chapters.
Answer:
isbn(storeName='Chapters' AND quantity > 0(Stock publisher='Harper Collins'(Book)))
2. List the addresses of stores that have pairs of books with the same title and different ISBNs.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Lab 1 - Relational Algebra Assignment


Home

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like