Docsity

Database Management Systems Homework 3 (Fall 2008) - SQL and Relational Algebra Exercises , Assignments of Computer Science

Toggle sidebar
bg1
Database Management Systems (COP 5725)
(Fall 2008)
Instructor: Dr. Markus Schneider
TAs:
Mingsong Chen
Wenjie Yuan
Homework 3
Name:
UFID:
Email Address:
Pledge (Must be signed according to UF Honor Code)
On my honor, I have neither given nor received unauthorized aid in doing this assignment.
_______________________________________________Signature
For scoring use only:
Maximum Received
Exercise 1 40
Exercise 2 30
Exercise 3 30
Total 100
pf3
pf4

Partial preview of the text

Download Database Management Systems Homework 3 (Fall 2008) - SQL and Relational Algebra Exercises and more Assignments Computer Science in PDF only on Docsity!

Database Management Systems (COP 5725)

(Fall 2008)

Instructor: Dr. Markus Schneider

TAs:

Mingsong Chen Wenjie Yuan

Homework 3

Name: UFID: Email Address:

Pledge (Must be signed according to UF Honor Code)

On my honor, I have neither given nor received unauthorized aid in doing this assignment.

_______________________________________________Signature

For scoring use only:

Maximum Received

Exercise 1 40

Exercise 2 30

Exercise 3 30

Total 100

Exercise 1 (SQL) [40 points]

Given the following relations:

Customers (cid, cname, city, discnt) Agents (aid, aname, city, percent) Products (pid, pname, city, quantity, price) Orders (ordno, month, cid, aid, pid, qty, dollars)

Write the following queries in SQL:

  1. For each agent taking an order, list the product pid and the total quantity ordered by all

customers FROM that agent.

  1. We say that a customer x orders a product y in an average quantity A if A is avg(qty) for all

orders rows with cid=x and pid=y. Is it possible in a single SQL statement to retrieve cid values of customers who order all the products that they receive in average quantities (by product) of at least 300?

  1. Get aid values of agents not taking orders FROM any customer in Duluth for any product in

Dallas.

  1. Get aid values of agents who order at least one common product for each customer who is

based in Duluth or Kyoto.

  1. Get cid values of customers who make orders only through agent a03 or a05.
  2. Get pid values of products that are ordered by all customers in Dallas.
  3. Find the average, over all agents, of the maximum dollar sales made by each agent
  4. Write an SQL query to retrieve cid values for customers who place at least one order, but

only through agent a04. On the same line with each cid, your query should list the total dollar amount of orders placed.

  1. Write an SQL query to get aid and percent values of agents who take orders FROM all

customers who live in Duluth. The aid values should be reported in order by decreasing percent. (Note that if percent is not retrieved in the SELECT list, we cannot order by these values.)

  1. Write an SQL query to get pid values of products ordered by at least one customer who lives

in the same city as the agent taking the order.

Exercise 3 (Relational Algebra and Calculus) [30 points]

Consider the following schema:

Suppliers( sid: integer, sname: string, address: string) Parts( pid: integer, pname: string, color: string) Catalog( sid: integer, pid: integer, cost: real)

The key fields are underlined, and the domain of each field is listed after the field name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid together form the key for Catalog. The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in relational algebra, tuple relational calculus, and domain relational calculus:

  1. Find the sid s of suppliers who supply some red or green part.
  2. Find the sid s of suppliers who supply some red part or are at 221 Packer Street.
  3. Find the sid s of suppliers who supply some red part and some green part.
  4. Find the sid s of suppliers who supply every red part.
  5. Find the sid s of suppliers who supply every red or green part.
  6. Find the sid s of suppliers who supply every red part or supply every green part.
  7. Find pairs of sid s such that the supplier with the first sid charges more for some part than the supplier with the second sid
  8. Find the pid s of parts supplied by at least two different suppliers
  9. Find the pid s of the most expensive parts supplied by suppliers named Yosemite Sham.
  10. Find the pid s of parts supplied by every supplier at less than $200. (If any supplier either does not supply the part or charges more than $200 for it, the part is not selected.)