Adding Case Study 3 repo

This commit is contained in:
Noah L. Schrick 2022-09-21 16:31:07 -05:00
commit 6c266a8e58
4 changed files with 240 additions and 0 deletions

67
README.md Normal file
View File

@ -0,0 +1,67 @@
# Case Study 2
## Instructions
- Answer Exercises 2.40 through 2.60 found at the end of Chapter 2, as well as the additional "2.61" question given.
- Use both the INVENTORY and WAREHOUSE tables to answer Questions 2.40 through 2.55.
- Use both the CATALOG\_SKU\_2016 and CATALOG\_SKU\_2017 tables to answer Questions 2.56 through 2.60.
- Deliver a single sql file, along with a document that contains all the content from the sql file.
- Each query should start with a comment line that looks like:
/* *** CS3-\<CH.Q> *** \*/
Example for Question 2.40:
/* *** CS3-2.40*** */
- Do not include the result table unless specifically directed to.
- Include at least one line of white space between answers.
## Questions 2.40 - 2.61
2.40) Write a SQL statement to display the SKU, SKU_Description, WarehouseID,
WarehouseCity, and WarehouseState for all items stored in the Atlanta, Bangor, or
Chicago warehouse. Do not use the IN keyword.
2.41) Write a SQL statement to display the SKU, SKU_Description, WarehouseID,
WarehouseCity, and WarehouseState for all items stored in the Atlanta, Bangor, or
Chicago warehouse. Use the IN keyword.
2.42) Write a SQL statement to display the SKU, SKU_Description, WarehouseID,
WarehouseCity, and WarehouseState of all items not stored in the Atlanta, Bangor, or
Chicago warehouse. Do not use the NOT IN keyword.
2.43) Write a SQL statement to display the SKU, SKU_Description, WarehouseID, WarehouseCity, and WarehouseState of all items not stored in the Atlanta, Bangor, or Chicago warehouse. Use the NOT IN keyword.
2.44) Write a SQL statement to produce a single column called ItemLocation that combines the SKU_Description, the phrase “is located in,” and WarehouseCity. Do not be concerned with removing leading or trailing blanks.
2.45) Write a SQL statement to show the SKU, SKU_Description, and WarehouseID for all items stored in a warehouse managed by Lucille Smith. Use a subquery.
2.46) Write a SQL statement to show the SKU, SKU_Description, and WarehouseID for
all items stored in a warehouse managed by Lucille Smith. Use a join, but do not use
JOIN ON syntax.
2.47) Write a SQL statement to show the SKU, SKU_Description, and WarehouseID for all items stored in a warehouse managed by Lucille Smith. Use a join using JOIN ON syntax.
2.48) Write a SQL statement to show the WarehouseID and average QuantityOnHand of all items stored in a warehouse managed by Lucille Smith. Use a subquery.
2.49) Write a SQL statement to show the WarehouseID and average QuantityOnHand of all items stored in a warehouse managed by Lucille Smith. Use a join, but do not use JOIN ON syntax.
2.50) Write a SQL statement to show the WarehouseID and average QuantityOnHand of all items stored in a warehouse managed by Lucille Smith. Use a join using JOIN ON syntax.
2.51) Write a SQL statement to show the WarehouseID, WarehouseCity, WarehouseState, Manager, SKU, SKU_Description, and QuantityOnHand of all items stored in a warehouse managed by Lucille Smith. Use a join using JOIN ON syntax.
2.52) Write a SQL statement to display the WarehouseID, the sum of QuantityOnOrder, and the sum of QuantityOnHand, grouped by WarehouseID and QuantityOnOrder. Name the sum of QuantityOnOrder as TotalItemsOnOrder and the sum of Quantity OnHand as TotalItemsOnHand. Use only the INVENTORY table in your SQL statement.
2.53) Explain why you cannot use a subquery in your answer to Review Question 2.52.
2.54) Explain how subqueries and joins differ.
2.55) Write a SQL statement to join WAREHOUSE and INVENTORY and include all rows of WAREHOUSE in your answer, regardless of whether they have any INVENTORY. Include all columns of both tables, but do not repeat the join column.
2.56) Write a SQL statement to display the SKU, SKU_Description, and Department of all SKUs that appear in either the Cape Codd 2016 catalog (either in the printed catalog or on the Web site) or the Cape Codd 2017 catalog (either in the printed catalog or on the Web site) or both.
2.57) Write a SQL statement to display the SKU, SKU_Description, and Department of all SKUs that appear in either the Cape Codd 2016 catalog (only in the printed catalog itself) or the Cape Codd 2017 catalog (only in the printed catalog itself) or both.
2.58) Write a SQL statement to display the SKU, SKU_Description, and Department of all SKUs that appear in both the Cape Codd 2016 catalog (either in the printed catalog or on the Web site) and the Cape Codd 2017 catalog (either in the printed catalog or on the Web site).
2.59) Write a SQL statement to display the SKU, SKU_Description, and Department of all SKUs that appear in both the Cape Codd 2016 catalog (only in the printed catalog itself) and the Cape Codd 2017 catalog (only in the printed catalog itself).
2.60) Write a SQL statement to display the SKU, SKU_Description, and Department of all SKUs that appear in only the Cape Codd 2016 catalog (either in the printed catalog or on the Web site) and not in the Cape Codd 2017 catalog (either in the printed catalog or on the Web site).
2.61) Suppose that I consider changing the primary key of SKU_DATA table to “Buyer”. Write correlated subqueries to display any data that indicates that this change is not justifiable.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,173 @@
/* *** CS3-2.40 *** */
SELECT i.SKU, i.SKU_Description, w.WarehouseID, w.WarehouseCity, w.WarehouseState
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND
(w.WarehouseCity = 'Atlanta') OR
(w.WarehouseCity = 'Bangor') OR
(w.WarehouseCity = 'Chicago')
;
/* *** CS3-2.41 *** */
SELECT i.SKU, i.SKU_Description, w.WarehouseID, w.WarehouseCity, w.WarehouseState
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND w.WarehouseCity IN ('Atlanta', 'Bangor', 'Chicago')
;
/* *** CS3-2.42 *** */
SELECT i.SKU, i.SKU_Description, w.WarehouseID, w.WarehouseCity, w.WarehouseState
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND
NOT (w.WarehouseCity = 'Atlanta') AND
NOT (w.WarehouseCity = 'Bangor') AND
NOT (w.WarehouseCity = 'Chicago')
;
/* *** CS3-2.43 *** */
SELECT i.SKU, i.SKU_Description, w.WarehouseID, w.WarehouseCity, w.WarehouseState
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND w.WarehouseCity NOT IN ('Atlanta', 'Bangor', 'Chicago')
;
/* *** CS3-2.44 *** */
SELECT (RTRIM(i.SKU_Description) + ' is located in ' + LTRIM(w.WarehouseCity)) AS ItemLocation
FROM INVENTORY i, WAREHOUSE w
;
/* *** CS3-2.45 *** */
SELECT SKU, SKU_Description, WarehouseID
FROM INVENTORY
WHERE WarehouseID IN
(SELECT WarehouseID
FROM WAREHOUSE
WHERE Manager = 'Lucille Smith')
;
/* *** CS3-2.46 *** */
SELECT i.SKU, i.SKU_Description, w.WarehouseID
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND w.Manager = 'Lucille Smith'
;
/* *** CS3-2.47 *** */
SELECT SKU, SKU_Description, w.WarehouseID
FROM INVENTORY i
JOIN WAREHOUSE w ON i.WarehouseID = w.WarehouseID
WHERE Manager = 'Lucille Smith'
;
/* *** CS3-2.48 *** */
SELECT WarehouseID, AVG(QuantityOnHand) AS AVGQuantityOnHand
FROM INVENTORY
WHERE WarehouseID IN
(SELECT WarehouseID
FROM WAREHOUSE
WHERE Manager = 'Lucille Smith'
)
GROUP BY WarehouseID
;
/* *** CS3-2.49 *** */
SELECT i.WarehouseID, AVG(i.QuantityOnHand) AS AVGQuantityOnHand
FROM INVENTORY i, WAREHOUSE w
WHERE i.WarehouseID = w.WarehouseID
AND w.Manager = 'Lucille Smith'
GROUP BY i.WarehouseID
;
/* *** CS3-2.50 *** */
SELECT i.WarehouseID, AVG(QuantityOnHand) AS AVGQuantityOnHand
FROM INVENTORY i
JOIN WAREHOUSE w ON i.WarehouseID = w.WarehouseID
WHERE Manager = 'Lucille Smith'
GROUP BY i.WarehouseID
;
/* *** CS3-2.51 *** */
SELECT w.WarehouseID, WarehouseCity, WarehouseState, Manager, SKU, SKU_Description, QuantityOnHand
FROM WAREHOUSE w
JOIN INVENTORY i ON w.WarehouseID = i.WarehouseID
WHERE Manager = 'Lucille Smith'
;
/* *** CS3-2.52 *** */
SELECT WarehouseID, SUM(QuantityOnOrder) AS TotalItemsOnOrder, SUM(QuantityOnHand) AS TotalItemsOnHand
FROM INVENTORY
GROUP BY WarehouseID, QuantityOnOrder
;
/* *** CS3-2.53 *** */
-- All desired results are in the same table, however there are aggregate functions involved.
-- SQL does not allow performing an aggregate function on expressions containing a subquery.
-- Likewise, subqueries are not allowed in aggregate functions.
/* *** CS3-2.54 *** */
-- Subqueries only retrieve items from the top table, not any other table.
-- Subqueries return results "up", but do not combine or merge any tables.
-- Joins combine tables, and all columns are accessible since the tables are joined together.
/* *** CS3-2.55 *** */
SELECT *
FROM WAREHOUSE w
LEFT JOIN INVENTORY i ON w.WarehouseID = i.WarehouseID
;
/* *** CS3-2.56 *** */
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2016
UNION
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2017
;
/* *** CS3-2.57 *** */
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2016
WHERE CatalogPage IS NOT NULL
UNION
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2017
WHERE CatalogPage IS NOT NULL
;
/* *** CS3-2.58 *** */
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2016
INTERSECT
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2017
;
/* *** CS3-2.59 *** */
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2016
WHERE CatalogPage IS NOT NULL
INTERSECT
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2017
WHERE CatalogPage IS NOT NULL
;
/* *** CS3-2.60 *** */
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2016
EXCEPT
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2017
;
/* *** CS3-2.61 *** */
SELECT d.Buyer, d.SKU
FROM SKU_DATA d
WHERE d.Buyer IN
(SELECT t.Buyer
FROM SKU_DATA t
WHERE d.Buyer = t.Buyer
AND d.SKU <> t.SKU
)
;
-- Shows that changing the primary key to Buyer is not justifiable: Buyer is non-unique.