F05Homework3Solution

advertisement
CSE 2337
Homework III
The answers provided here are my solutions. As I have mentioned in
class, there are multiple ways to write queries that will produce the same
results. Use these solutions as a guide to study for your Final Test.
Directions: Provide SQL commands for each of the following questions. Do not consult
any other person with respect to the solutions on this assignment. Please refer back to the
outline and review the academic dishonesty policy.
Use the Premiere Products database for the following questions.
1.
2.
3.
4.
5.
6.
7.
Provide a list of customers who have a credit limit in excess of $9999.00.
SELECT customer.customerName
FROM Customer
WHERE creditLimit > 9999.00
List the order number, order date, customer number and customer name for each
order in the database
SELECT o.orderNum, o.orderDate, o.CustomerNum, c.CustomerName
FROM order o, customer c
WHERE o.CustomerNum = c.CustomerNum
List the number and date of all orders that were placed on 10/20/2007 by a
customer whose rep number is 20.
SELECT o.ordernum, o.orderdate
FROM customer c, order o
WHERE c.customerNum = o.customerNum
AND o.orderDate = #10/20/2007#
AND c.repNum = 20
List the customer names and customer numbers of all customers in the database
SELECT customerName, customerNum
FROM customer
List all of the parts, including name and cost, in the part table in descending order
by cost.
SELECT description, price
FROM part
ORDER BY price desc
List all of the attributes from the customer table in descending order based on the
customer name.
SELECT *
FROM Customer
ORDER BY CustomerName Desc
List the number, name, street, and credit limit for every customer with a credit
limit of $10,000 or $15,000.
SELECT customerNum, CustomerName, street, creditLimit
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
FROM Customer
WHERE CreditLimit IN (10000, 15000)
Which customers are represented by Juan Perez?
SELECT customerName
FROM rep r, customer c
WHERE r.repnum = c.repnum
AND r.lastname = ‘Perez’
AND r.firstname = ‘Juan’
How many parts are held at warehouse 3?
SELECT count(description)
FROM part
WHERE warehouse = ‘3’
How many parts were ordered on order number21610?
SELECT count(partnum)
FROM OrderLine
WHERE orderNum = ‘21610’
List the order number for all parts that are held in either warehouse 2 or 3
SELECT ol.ordernum
FROM part p, orderline ol
WHERE p.partnum = ol.partnum
and p.warehouse = 2 or p.warehouse = 3
For each representative, list the sum of the balances of the customers that are
represented by that rep.
SELECT repNum, sum(balance)
FROM customer
group by repnum
List each customer name along with the name and address of its representative.
SELECT c.customerName, r.lastname, r.firstname, r.street
FROM customer c, rep r
WHERE c.repnum = r.repnum
List the names of the parts ever ordered by “All Season”.
SELECT p.description
FROM part p, orderlin ol, order o, customer c
WHERE p.partnum =ol.partnum
and ol.ordernum = o.ordernum
and o.customernum = c.customernum
and o.customerName = “All Season”
List the total cost of the parts ordered for each order.
SELECT orderNum, sum(quotedPrice * numordered)
FROM orderline
Group by ordernum
How many orders were placed on 10/20/2007?
SELECT count(orderdate)
FROM order
WHERE orderDate = #10/20/2007#
What is the average customer credit limit?
SELECT avg(creditlimit)
FROM customer
18. What is the total outstanding balance for all customers?
SELECT sum(balance)
FROM customer
19. List the name of each customer, each invoice, number and the sum of all the
items on that invoice.
SELECT c.customerName, o.ordernum, sum(ol.numOrdered *
ol.quotedPrice)
FROM Customer c, orders o, orderline ol
WHERE c.customerNum = o.customerNum
and o.ordernum = ol.ordernum
group by c.customerName, o.ordernum
20. List the name of all representatives who have sold an iron.
SELECT r.lastname, r.firstname
FROM rep r, customer c, orders o, orderline ol, part p
WHERE r.repnum = c.repnum
AND c.customerNum = o.customerNum
AND ol.ordernum = o.ordernum
AND ol.partnum = p.partnum
AND p.description = “IRON”
Download