Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW3: Chapters 4&5 Chapter 4: How to retrieve data from two or more tables Exerci

ID: 3790181 • Letter: H

Question

HW3: Chapters 4&5

Chapter 4: How to retrieve data
from two or more tables

Exercises

1.       Write a SELECT statement that joins the Categories table to the Products table and returns these columns: category_name, product_name, list_price.

Sort the result set by category_name and then by product_name in ascending sequence.

2.       Write a SELECT statement that joins the Customers table to the Addresses table and returns these columns: first_name, last_name, line1, city, state, zip_code.

Return one row for each address for the customer with an email address of allan.sherwood@yahoo.com.

3.       Write a SELECT statement that joins the Customers table to the Addresses table and returns these columns: first_name, last_name, line1, city, state, zip_code.

Return one row for each customer, but only return addresses that are the shipping address for a customer.

4.       Write a SELECT statement that joins the Customers, Orders, Order_Items, and Products tables. This statement should return these columns: last_name, first_name, order_date, product_name, item_price, discount_amount, and quantity.

Use aliases for the tables.

Sort the final result set by last_name, order_date, and product_name.

5.       Write a SELECT statement that returns the product_name and list_price columns from the Products table.

Return one row for each product that has the same list price as another product.
Hint: Use a self-join to check that the product_id columns aren’t equal but the list_price columns are equal.

Sort the result set by product_name.

6.       Write a SELECT statement that returns these two columns:

category_name       The category_name column from the Categories table

product_id             The product_id column from the Products table

Return one row for each category that has never been used. Hint: Use an outer join and only return rows where the product_id column contains a null value.

7.       Use the UNION operator to generate a result set consisting of three columns from the Orders table:

ship_status             A calculated column that contains a value of SHIPPED or NOT SHIPPED

order_id                 The order_id column

order_date              The order_date column

If the order has a value in the ship_date column, the ship_status column should contain a value of SHIPPED. Otherwise, it should contain a value of NOT SHIPPED.

Sort the final result set by order_date.

Chapter 5

How to code summary queries

Exercises

1.       Write a SELECT statement that returns these columns:

The count of the number of orders in the Orders table

The sum of the tax_amount columns in the Orders table

2.       Write a SELECT statement that returns one row for each category that has products with these columns:

The category_name column from the Categories table

The count of the products in the Products table

The list price of the most expensive product in the Products table

Sort the result set so the category with the most products appears first.

3.       Write a SELECT statement that returns one row for each customer that has orders with these columns:

The email_address column from the Customers table

The sum of the item price in the Order_Items table multiplied by the quantity in the Order_Items table

The sum of the discount amount column in the Order_Items table multiplied by the quantity in the Order_Items table

Sort the result set in descending sequence by the item price total for each customer.

4.       Write a SELECT statement that returns one row for each customer that has orders with these columns:

The email_address from the Customers table

A count of the number of orders

The total amount for each order (Hint: First, subtract the discount amount from the price. Then, multiply by the quantity.)

Return only those rows where the customer has more than 1 order.

Sort the result set in descending sequence by the sum of the line item amounts.

5.       Modify the solution to exercise 4 so it only counts and totals line items that have an item_price value that’s greater than 400.

6.       Write a SELECT statement that answers this question: What is the total amount ordered for each product? Return these columns:

The product name from the Products table

The total amount for each product in the Order_Items (Hint: You can calculate the total amount by subtracting the discount amount from the item price and then multiplying it by the quantity)

Use the ROLLUP operator to include a row that gives the grand total.

7.       Write a SELECT statement that answers this question: Which customers have ordered more than one product? Return these columns:

The email address from the Customers table

The count of distinct products from the customer’s

Explanation / Answer

Chapter 4

1.
SELECT C.CATEGORY_NAME, P.PRODUCT_NAME, P.LIST_PRICE
FROM CATEGORY C, PRODUCT P
WHERE P.CATEGORY_NAME = C.CATEGORY_NAME
ORDER BY C.CATEGORY_NAME, P.PRODUCT_NAME;

2.
SELECT C.FIRST_NAME, C.LAST_NAME,
A.LINE1, A.CITY, A.STATE, A.ZIP_CODE
FROM CUSTOMERS C, ADDRESS A
WHERE C.ADDRESS_ID = A.ADDRESS_ID
AND C.EMAIL_ADDRESS = 'allan.sherwood@yahoo.com'
GROUP BY C.FIRST_NAME, C.LAST_NAME;

3.
SELECT C.FIRST_NAME, C.LAST_NAME,
A.LINE1, A.CITY, A.STATE, A.ZIP_CODE
FROM CUSTOMERS C, ADDRESS A
WHERE C.ADDRESS_ID = A.ADDRESS_ID
AND C.IS_SHIPPING_ADDRESS = 'Y'
GROUP BY C.FIRST_NAME, C.LAST_NAME;

4.
SELECT C.FIRST_NAME, C.LAST_NAME,
O.ORDER_DATE, P.PRODUCT_NAME,
P.ITEM_PRICE, OI.DISCOUNT_AMOUNT, OI.QUANTITY
FROM CUSTOMERS C
JOIN ORDER O ON C.ORDER_ID = O.ORDER_ID
JOIN ORDER_ITEM OI ON O.ITEM_ID = OI.ITEM_ID
JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID
ORDER BY C.LAST_NAME, O.ORDER_DATE, P.PRODUCT_NAME;

5.
SELECT P.PRODUCT_NAME, P.LIST_PRICE
FROM PRODUCT P
JOIN PRODUCT Q ON P.PRODUCT_ID <> Q.PRODUCT_ID
AND P.LIST_PRICE = Q.LIST_PRICE
ORDER BY P.PRODUCT_NAME;

6.
SELECT C.CATEGORY_NAME, P.PRODUCT_NAME
FROM CATEGORY C
OUTER JOIN PRODUCT P ON C.CATEGORY_ID = P.CATEGORY_ID
ORDER BY C.CATEGORY_NAME, P.PRODUCT_NAME;

Chapter 5
1.
SELECT COUNT(*) FROM ORDERS;
SELECT SUM(TAX_AMOUNT) FROM ORDERS;

2.
SELECT C.CATEGORY_NAME, COUNT(P.PRODUCT_NAME), MAX(P.LIST_PRICE)
FROM CATEGORY C
OUTER JOIN PRODUCT P ON C.CATEGORY_ID = P.CATEGORY_ID
GROUP BY C.CATEGORY_NAME
ORDER BY C.CATEGORY_NAME;

3.
SELECT C.EMAIL_ADDRESS,
SUM(OI.ITEM_PRICE * OI.QUANTITY) TOTAL_PRICE,
SUM(OI.DISCOUNT_AMOUNT * OI.QUANTITY) TOTAL_DISCOUNT
FROM CUSTOMERS C
JOIN ORDER O ON C.ORDER_ID = O.ORDER_ID
JOIN ORDER_ITEM OI ON O.ITEM_ID = OI.ITEM_ID
JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID
ORDER BY TOTAL_PRICE DESC;

4.
SELECT C.EMAIL_ADDRESS,
SUM((OI.ITEM_PRICE - OI.DISCOUNT_AMOUNT) * OI.QUANTITY) TOTAL_PRICE_AFTER_DISCOUNT,
COUNT(OI.*) TOTAL_ITEMS
FROM CUSTOMERS C
JOIN ORDER O ON C.ORDER_ID = O.ORDER_ID
JOIN ORDER_ITEM OI ON O.ITEM_ID = OI.ITEM_ID
JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID
GROUP BY C.EMAIL_ADDRESS HAVING COUNT(TOTAL_ITEMS) > 1
ORDER BY TOTAL_PRICE_AFTER_DISCOUNT DESC;

5.
SELECT C.EMAIL_ADDRESS,
SUM((OI.ITEM_PRICE - OI.DISCOUNT_AMOUNT) * OI.QUANTITY) TOTAL_PRICE_AFTER_DISCOUNT,
COUNT(OI.*) TOTAL_ITEMS
FROM CUSTOMERS C
JOIN ORDER O ON C.ORDER_ID = O.ORDER_ID
JOIN ORDER_ITEM OI ON O.ITEM_ID = OI.ITEM_ID
JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID
WHERE OI.ITEM_PRICE > 400
GROUP BY C.EMAIL_ADDRESS HAVING COUNT(TOTAL_ITEMS) > 1
ORDER BY TOTAL_PRICE_AFTER_DISCOUNT DESC;

6. No expertise on ROLLUP

7.
SELECT C.EMAIL_ADDRESS, COUNT(DISTINCT P.PRODUCT_NAME) DISTINCT_PRODUCTS
FROM CATEGORY C, PRODUCT P
WHERE P.CATEGORY_NAME = C.CATEGORY_NAME
GROUP BY C.EMAIL_ADDRESS HAVING DISTINCT_PRODUCTS > 1;