Docsity

COMPUTER 140 KimtayLab_Chapter5, Exercises of Information and Communications Technology (ICT)

Toggle sidebar
bg1
Exercises
KimTay Pet Supplies
1. For each invoice, list the invoice number and invoice date along with the
ID, first name, and last name of the customer for which the invoice was
created.
Answer:
SELECT INVOICE_NUM, INVOICE_DATE, CUSTOMER.CUST_ID,
FIRST_NAME, LAST_NAME
FROM INVOICES, CUSTOMER
WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID;
2. For each invoice placed on November 15, 2021, list the invoice number
along with the ID, first name, and last name of the customer for which the
invoice was created.
Answer:
SELECT INVOICE_NUM, CUSTOMER.CUST_ID, FIRST_NAME,
LAST_NAME
FROM INVOICES, CUSTOMER
WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID
AND INVOICE_DATE = '2021-11-15';
Oracle:
SELECT INVOICE_NUM, CUSTOMER.CUST_ID, FIRST_NAME,
LAST_NAME
FROM INVOICES, CUSTOMER
WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID
AND INVOICE_DATE = '15-NOV-2021';
3. For each invoice, list the invoice number, invoice date, item ID, quantity
ordered, and quoted price for each invoice line that makes up the invoice.
Answer:
pf3
pf4
pf5
pf8

Partial preview of the text

Download COMPUTER 140 KimtayLab_Chapter5 and more Exercises Information and Communications Technology (ICT) in PDF only on Docsity!

Exercises KimTay Pet Supplies

  1. For each invoice, list the invoice number and invoice date along with the ID, first name, and last name of the customer for which the invoice was created. Answer: SELECT INVOICE_NUM, INVOICE_DATE, CUSTOMER.CUST_ID, FIRST_NAME, LAST_NAME FROM INVOICES, CUSTOMER WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID;
  2. For each invoice placed on November 15, 2021, list the invoice number along with the ID, first name, and last name of the customer for which the invoice was created. Answer: SELECT INVOICE_NUM, CUSTOMER.CUST_ID, FIRST_NAME, LAST_NAME FROM INVOICES, CUSTOMER WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID AND INVOICE_DATE = '2021-11-15'; Oracle: SELECT INVOICE_NUM, CUSTOMER.CUST_ID, FIRST_NAME, LAST_NAME FROM INVOICES, CUSTOMER WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID AND INVOICE_DATE = '15-NOV-2021';
  3. For each invoice, list the invoice number, invoice date, item ID, quantity ordered, and quoted price for each invoice line that makes up the invoice. Answer:

SELECT I.INVOICE_NUM, INVOICE_DATE, ITEM_ID, QUANTITY,

QUOTED_PRICE

FROM INVOICES AS I, INVOICE_LINE AS IL

WHERE I.INVOICE_NUM = IL.INVOICE_NUM;

  1. Use the IN operator to find the ID, first name, and last name of each customer for which as invoice was created on November 15, 2021. Answer: SELECT CUST_ID, FIRST_NAME, LAST_NAME FROM CUSTOMER WHERE CUST_ID IN (SELECT CUST_ID FROM INVOICES); CORRECT ONE
    SELECT CUST_ID , FIRST_NAME, LAST_NAME FROM CUSTOMER WHERE CUSTOMER.CUST_ID IN (SELECT CUST_ID FROM INVOICES WHERE INVOICE_DATE = '2021- 11-15');
  2. Repeat Exercise 4, but this time use the EXISTS operator in your answer. Answer: SELECT CUST_ID, FIRST_NAME, LAST_NAME FROM CUSTOMER WHERE EXISTS (SELECT * FROM INVOICE_LINE, INVOICES WHERE INVOICES.CUST_ID = CUSTOMER.CUST_ID AND INVOICES.INVOICE_NUM = INVOICE_LINE.INVOICE_NUM); CORRECT ONE
    SELECT CUST_ID , FIRST_NAME, LAST_NAME

Answer: SELECT INVOICES.INVOICE_NUM, INVOICES.INVOICE_DATE, ITEM.ITEM_ID, ITEM.DESCRIPTION, ITEM.CATEGORY FROM INVOICES, INVOICE_LINE, ITEM WHERE INVOICES.INVOICE_NUM = INVOICE_LINE.INVOICE_NUM AND INVOICE_LINE.ITEM_ID = ITEM.ITEM_ID ORDER BY INVOICE_NUM;

  1. Use a subquery to find the sales rep ID, first name, and last name of each sales rep who represents at least one customer with a credit limit of $500. List each sales rep only once in the results. Answer: SELECT REP_ID, FIRST_NAME, LAST_NAME FROM SALES_REP WHERE EXISTS (SELECT * FROM CUSTOMER WHERE CREDIT_LIMIT > 500 AND CUSTOMER.REP_ID = SALES_REP.REP_ID); 10.Repeat Exercise 9, but this time do not use a subquery. Answer: SELECT CUSTOMER.CUST_ID, CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME FROM SALES_REP, CUSTOMER WHERE CUSTOMER.CREDIT_LIMIT > 500 AND CUSTOMER.REP_ID = SALES_REP.REP_ID; 11.Find the ID, first name, and last name of each customer that currently has an invoice on file for Wild Bird Food (25 lb). Answer: SELECT CUSTOMER.CUST_ID, CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME FROM CUSTOMER, INVOICES, INVOICE_LINE, ITEM

WHERE CUSTOMER.CUST_ID = INVOICES.CUST_ID

AND INVOICES.INVOICE_NUM = INVOICE_LINE.INVOICE_NUM

AND INVOICE_LINE.ITEM_ID = ITEM.ITEM_ID

AND ITEM.DESCRIPTION = 'Wild Bird Food (25 lb)'; 12.List the item ID, description, and category for each pair of items that are in the same category. (For example, one such pair would be item FS42 and item PF19, because the category for both items is FSH.) Answer: SELECT CATEGORY, ITEM_ID, DESCRIPTION FROM ITEM WHERE EXISTS (SELECT COUNT() FROM ITEM I WHERE I.CATEGORY = ITEM.CATEGORY GROUP BY CATEGORY HAVING COUNT() = 2) ORDER BY CATEGORY; 13.List the invoice number and invoice date for each invoice created for the customer James Gonzalez. Answer: SELECT INVOICE_NUM, INVOICE_DATE FROM INVOICES WHERE CUST_ID IN (SELECT CUST_ID FROM CUSTOMER WHERE FIRST_NAME = 'James' AND LAST_NAME = 'Gonzalez'); 14.List the invoice number and invoice date for each invoice that contains an invoice line for a Wild Bird Food (25 lb). Answer: SELECT INVOICES.INVOICE_NUM, INVOICES.INVOICE_DATE FROM INVOICES, INVOICE_LINE, ITEM

AND CUSTOMER.LAST_NAME = 'Gonzalez'; 17.List the invoice number and invoice date for each invoice that was created for James Gonzalez but that does not contain an invoice line for Wild Bird Food (25lb). Answer: SELECT DISTINCT INVOICES.INVOICE_NUM, INVOICES.INVOICE_DATE FROM INVOICES, INVOICE_LINE, ITEM, CUSTOMER WHERE INVOICES.INVOICE_NUM = INVOICE_LINE.INVOICE_NUM AND INVOICE_LINE.ITEM_ID = ITEM.ITEM_ID AND INVOICES.CUST_ID = CUSTOMER.CUST_ID AND ITEM.DESCRIPTION <> 'Wild Bird Food (25 lb)' AND CUSTOMER.FIRST_NAME = 'James' AND CUSTOMER.LAST_NAME = 'Gonzalez'; 18.List the item ID, description, unit price, and category for each item that has a unit price greater than the unit price of every item in category CAT. Use either the ALL or ANY operator in your query. ( Hint : Make sure you select the correct operator.) Answer: SELECT ITEM.ITEM_ID, ITEM.DESCRIPTION, ITEM.CATEGORY FROM ITEM WHERE ITEM.PRICE > ALL (SELECT PRICE FROM ITEM WHERE CATEGORY = 'CAT'); 19.For each item, list the item ID, description, units on hand, invoice number, and quantity ordered. All items should be included in the results. For those items that are currently not on an invoice, the invoice number and quantity

ordered should be left blank. Order the results by item ID. Answer: SELECT ITEM.ITEM_ID, ITEM.DESCRIPTION, ITEM.ON_HAND, INVOICE_LINE.INVOICE_NUM, INVOICE_LINE.QUANTITY FROM ITEM LEFT JOIN INVOICE_LINE ON (INVOICE_LINE.ITEM_ID = ITEM.ITEM_ID