You are on page 1of 10

Practice Exam 1

Question Format

• Write an SQL or RA query


• Write or pick the answer to an SQL or RA query
• Are these two queries equivalent?
• Quiz 1 style
• What constraints hold on this schema?
• Which query is correct?
• What is largest/smallest size of answer to this query?
Additional Practice Problems
• For studying, I recommend the practice problems from the cow book.
Some of these are easy but good for reminding yourself about the material.
Try some of the harder relational algebra and SQL queries yourself before
looking at the answers.
• Download the “solution manual”
on: http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/supporting
_material.htm
• Chapter 1 1.1 and 1.3.
Chapter 3 odd numbered questions 3.1-3.9
Chapter 4 (only RA/SQL answers, we didn’t cover relationa/domain
calculus) 4.1, 4.3, 4.5.
• Chapter 5 odd numbered questions 5.1-5.5
Given two rela-ons R1 and R2, where R1 contains N1 tuples, R2 contains N2 tuples, and N2 > N1 > 0,
give the minimum and maximum possible sizes (in tuples) for the resul-ng rela-on produced by each
of the following rela-onal algebra expressions. In each case, state any assump-ons about the
schemas for R1 and R2 needed to make the expression meaningful:

(1) R1∪R2
(2) R1∩R2
(3) R1−R2
(4) R1×R2
(5) σa=5(R1)
(6) πa(R1)
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• Write query that finds all suppliers (sid) who supply some red part.

• Write a query that finds all suppliers (sname) who only supply red
parts.
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• If there are 10 suppliers and 10 parts, how small or large could


catalog be?

• If we drop the constraints, how small or large could catalog be?

• How large or small could the result of a query that finds all suppliers
(sid) who supply some red part be?
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• Write SQL query that finds the supplier who supplies the most
expensive widget (pname = widget).

• Can this query be written in RA?


Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• Are these queries equivalent?


1. πsid (Suppliers X Parts)
2. select sid
from Suppliers, Parts
3. select distinct sid
from Suppliers, Parts
4. select sid from Suppliers
5. select distinct sid from Suppliers
• Are these queries equivalent?
1. πsid (Suppliers) - πsid (Catalog)
2. select distinct sid from Supplier where sid not in (select sid from Catalog)
3. select distinct sid from Supplier where not exist
(select * from Catalog where Supplier.sid =Catalog.sid)
4. select sid from Supplier, Catalog where Supplier.sid = Catalog.sid
group by sid
having count(*) = 0
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• Are these queries equivalent?


1. πsid (Catalog)
2. πsid (Catalog X Suppliers)
3. select distinct sid
from Catalog
4. select distinct sid from Catalog, Suppliers where Catalog.sid = Suppliers.sid
• Does your answer change if we drop the constraints?
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost) sid FK Supplies, pid FK Parts

• Write a query to find suppliers that supply every part

• Does your answer change if we drop the constraints?

You might also like