0% found this document useful (0 votes)
2K views6 pages

DB Managment Ch7 Problems

The document contains 26 SQL queries that analyze data from tables representing customers, invoices, invoice lines, and products. The queries count records, calculate sums, find minimum/maximum values, and join tables to find relationships between entities. The queries are designed to return specific summaries and aggregations, with the output structured to match example figures provided for each problem.
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)
2K views6 pages

DB Managment Ch7 Problems

The document contains 26 SQL queries that analyze data from tables representing customers, invoices, invoice lines, and products. The queries count records, calculate sums, find minimum/maximum values, and join tables to find relationships between entities. The queries are designed to return specific summaries and aggregations, with the output structured to match example figures provided for each problem.
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/ 6

Database Management Ch7 Problems Garcia1

9. Write a query to count the number of invoices.

SELECT COUNT(*) AS Number_of_Invoices


FROM INVOICE;

10. Write a query to count the number of customers with a balance of more than
$500.

SELECT C.CUST_CODE, SUM(I.TOTAL_AMOUNT) AS Total_Balance


FROM CUSTOMER C
JOIN INVOICE I ON C.CUST_CODE = I.CUST_CODE
GROUP BY C.CUST_CODE
HAVING SUM(I.TOTAL_AMOUNT) > 500;

11. Generate a listing of all purchases made by the customers, using the output
shown in Figure P7.11 as your guide. Sort the results by customer code, invoice
number, and product description.

SELECT C.CUST_CODE, I.INV_NUMBER, L.LINE_NUMBER, L.LINE_UNITS,


L.LINE_PRICE, P.PROD_DESC
FROM CUSTOMER C
JOIN INVOICE I ON C.CUST_CODE = I.CUST_CODE
JOIN LINE L ON I.INV_NUMBER = L.INV_NUMBER
JOIN PRODUCT P ON L.PROD_CODE = P.PROD_CODE
ORDER BY C.CUST_CODE, I.INV_NUMBER, P.PROD_DESC;

12. Using the output shown in Figure P7.12 as your guide, generate a list of
customer purchases, including the subtotals for each of the invoice line numbers.
The subtotal is a derived attribute calculated by multiplying LINE_UNITS by
LINE_PRICE. Sort the output by customer code, invoice number, and product
description. Be certain to use the column aliases as shown in the figure.

SELECT C.CUST_CODE, I.INV_NUMBER, L.LINE_NUMBER, L.LINE_UNITS,


L.LINE_PRICE, L.LINE_UNITS * L.LINE_PRICE AS LINE_SUBTOTAL,
P.PROD_DESC
FROM CUSTOMER C
JOIN INVOICE I ON C.CUST_CODE = I.CUST_CODE
Database Management Ch7 Problems Garcia2

JOIN LINE L ON I.INV_NUMBER = L.INV_NUMBER


JOIN PRODUCT P ON L.PROD_CODE = P.PROD_CODE
ORDER BY C.CUST_CODE, I.INV_NUMBER, P.PROD_DESC;

13. Write a query to display the customer code, balance, and total purchases for
each customer. Total purchase is calculated by summing the line subtotals (as
calculated in Problem 12) for each customer. Sort the results by customer code, and
use aliases as shown in Figure P7.13

SELECT C.CUST_CODE, C.BALANCE, SUM(L.LINE_UNITS * L.LINE_PRICE) AS


TOTAL_PURCHASE
FROM CUSTOMER C
JOIN INVOICE I ON C.CUST_CODE = I.CUST_CODE
JOIN LINE L ON I.INV_NUMBER = L.INV_NUMBER
GROUP BY C.CUST_CODE
ORDER BY C.CUST_CODE;

14. Modify the query in Problem 13 to include the number of individual product
purchases made by each customer. (In other words, if the customer’s invoice is
based on three products, one per LINE_NUMBER, you count three product
purchases. Note that in the original invoice data, customer 10011 generated three
invoices, which contained a total of six lines, each representing a product purchase.)
Your output values must match those shown in Figure P7.14, sorted by customer
code.

SELECT C.CUST_CODE, C.BALANCE, SUM(L.LINE_UNITS * L.LINE_PRICE) AS


TOTAL_PURCHASE,
COUNT(L.LINE_NUMBER) AS PRODUCT_PURCHASES
FROM CUSTOMER C
JOIN INVOICE I ON C.CUST_CODE = I.CUST_CODE
JOIN LINE L ON I.INV_NUMBER = L.INV_NUMBER
GROUP BY C.CUST_CODE
ORDER BY C.CUST_CODE;

15. Use a query to compute the total of all purchases, the number of purchases, and
the average purchase amount made by each customer. Your output values must
match those shown in Figure P7.15. Sort the results by customer code.
Database Management Ch7 Problems Garcia3

SELECT C.CUSTOMER_CODE, SUM(L.LINE_UNITS * L.LINE_PRICE) AS


TOTAL_PURCHASE,
COUNT(DISTINCT L.INVOICE_NUMBER) AS NUMBER_OF_PURCHASES,
AVG(L.LINE_UNITS * L.LINE_PRICE) AS AVG_PURCHASE
FROM CUSTOMER C
INNER JOIN INVOICE I ON C.CUSTOMER_CODE = I.CUSTOMER_CODE
INNER JOIN LINE L ON I.INVOICE_NUMBER = L.INVOICE_NUMBER
GROUP BY C.CUSTOMER_CODE
ORDER BY C.CUSTOMER_CODE;

16. Create a query to produce the total purchase per invoice, generating the results
shown in Figure P7.16, sorted by invoice number. The invoice total is the sum of the
product purchases in the LINE that corresponds to the INVOICE.

SELECT L.INVOICE_NUMBER, SUM(L.LINE_UNITS * L.LINE_PRICE) AS


INVOICE_TOTAL
FROM LINE L
GROUP BY L.INVOICE_NUMBER
ORDER BY L.INVOICE_NUMBER;

17. Use a query to show the invoices and invoice totals in Figure P7.17. Sort the
results by customer code and then by invoice number.

SELECT I.CUSTOMER_CODE, I.INVOICE_NUMBER, SUM(L.LINE_UNITS *


L.LINE_PRICE) AS INVOICE_TOTAL
FROM INVOICE I
INNER JOIN LINE L ON I.INVOICE_NUMBER = L.INVOICE_NUMBER
GROUP BY I.CUSTOMER_CODE, I.INVOICE_NUMBER
ORDER BY I.CUSTOMER_CODE, I.INVOICE_NUMBER;

18. Write a query to produce the number of invoices and the total purchase amounts
by customer, using the output shown in Figure P7.18 as your guide. Note the results
are sorted by customer code. (Compare this summary to the results shown in
Problem 17.)
Database Management Ch7 Problems Garcia4

SELECT I.CUSTOMER_CODE, COUNT(DISTINCT I.INVOICE_NUMBER) AS


NUMBER_OF_INVOICES,
SUM(L.LINE_UNITS * L.LINE_PRICE) AS TOTAL_PURCHASE_AMOUNT
FROM INVOICE I
INNER JOIN LINE L ON I.INVOICE_NUMBER = L.INVOICE_NUMBER
GROUP BY I.CUSTOMER_CODE
ORDER BY I.CUSTOMER_CODE;

19. Write a query to generate the total number of invoices, the invoice total for all of
the invoices, the smallest of the customer purchase amounts, the largest of the
customer purchase amounts, and the average of all the customer purchase amounts.
Your output must match Figure P7.19.

SELECT COUNT(DISTINCT I.INVOICE_NUMBER) AS


TOTAL_NUMBER_OF_INVOICES,
SUM(L.LINE_UNITS * L.LINE_PRICE) AS TOTAL_INVOICE_AMOUNT,
MIN(L.LINE_UNITS * L.LINE_PRICE) AS MIN_PURCHASE_AMOUNT,
MAX(L.LINE_UNITS * L.LINE_PRICE) AS MAX_PURCHASE_AMOUNT,
AVG(L.LINE_UNITS * L.LINE_PRICE) AS AVG_PURCHASE_AMOUNT
FROM INVOICE I
INNER JOIN LINE L ON I.INVOICE_NUMBER = L.INVOICE_NUMBER;

20. List the balances of customers who have made purchases during the current
invoice cycle—that is, for the customers who appear in the INVOICE table. The
results of this query are shown in Figure P7.20, sorted by customer code.

SELECT C.CUSTOMER_CODE, C.BALANCE


FROM CUSTOMER C
INNER JOIN INVOICE I ON C.CUSTOMER_CODE = I.CUSTOMER_CODE
ORDER BY C.CUSTOMER_CODE;

21. Provide a summary of customer balance characteristics for customers who made
purchases. Include the minimum balance, maximum balance, and average balance,
as shown in Figure P7.21

SELECT MIN(BALANCE) AS MIN_BALANCE,


MAX(BALANCE) AS MAX_BALANCE,
Database Management Ch7 Problems Garcia5

AVG(BALANCE) AS AVG_BALANCE
FROM CUSTOMER
WHERE CUST_CODE IN (
SELECT DISTINCT CUST_CODE
FROM INVOICE
);

22. Create a query to find the balance characteristics for all customers, including the
total of the outstanding balances. The results of this query are shown in Figure 7.22.

SELECT MIN(BALANCE) AS MIN_BALANCE, MAX(BALANCE) AS


MAX_BALANCE, AVG(BALANCE) AS AVG_BALANCE, SUM(BALANCE) AS
TOTAL_BALANCE
FROM CUSTOMER;

23. Find the listing of customers who did not make purchases during the invoicing
period. Sort the results by customer code. Your output must match the output
shown in Figure P7.23.

SELECT CUSTOMER.*
FROM CUSTOMER
LEFT JOIN INVOICE ON CUSTOMER.CUST_CODE = INVOICE.CUST_CODE
WHERE INVOICE.INVOICE_DATE IS NULL
ORDER BY CUSTOMER.CUST_CODE;

24. Find the customer balance summary for all customers who have not made
purchases during the current invoicing period. The results are shown in Figure
P7.24.

SELECT COUNT(*) AS NUM_CUSTOMERS, MIN(BALANCE) AS


MIN_BALANCE, MAX(BALANCE) AS MAX_BALANCE, AVG(BALANCE) AS
AVG_BALANCE, SUM(BALANCE) AS TOTAL_BALANCE
FROM CUSTOMER
WHERE CUST_CODE NOT IN (
SELECT DISTINCT CUST_CODE FROM INVOICE
);
Database Management Ch7 Problems Garcia6

25. Create a query that summarizes the value of products currently in inventory.
Note that the value of each product is a result of multiplying the units currently in
inventory by the unit price. Sort the results in descending order by subtotal, as
shown in Figure P7.25.

SELECT PRODUCT.DESCRIPTION, PRODUCT.UNIT_PRICE,


SUM(PRODUCT.IN_STOCK * PRODUCT.UNIT_PRICE) AS SUBTOTAL
FROM PRODUCT
GROUP BY PRODUCT.DESCRIPTION, PRODUCT.UNIT_PRICE
ORDER BY SUBTOTAL DESC;

26. Find the total value of the product inventory. The results are shown in Figure
P7.26.

SELECT SUM(PRODUCT.IN_STOCK * PRODUCT.UNIT_PRICE) AS


TOTAL_VALUE
FROM PRODUCT;

You might also like