Add chapter 2 and 4 labs for 2436 and database prog, respectively
This commit is contained in:
parent
1f87bd00ad
commit
f8b2dca3a3
18 changed files with 666 additions and 160 deletions
102
labs/database-programming/lab-2/instructions.md
Normal file
102
labs/database-programming/lab-2/instructions.md
Normal file
|
@ -0,0 +1,102 @@
|
|||
# Lab 2
|
||||
|
||||
*UPDATED: 12 Feb. Clarified scenario 6, 7 and 8.*
|
||||
|
||||
This lab has you practice advanced queries against tables with aggregates, grouping and subqueries.
|
||||
|
||||
For each scenario:
|
||||
|
||||
- Include the scenario #
|
||||
- The SQL query (properly formatted)
|
||||
|
||||
*Note: DO NOT hard code any primary key IDs in your queries.*
|
||||
|
||||
This lab uses the `WideWorldImporters` database.
|
||||
|
||||
## Scenario 1
|
||||
|
||||
Get the lowest (as `LowestTemperature`) and highest temperature (as `HighestTemperature`) recorded for the vehicle temperatures (`Warehouse.VehicleTemperatures`).
|
||||
|
||||
Rows: 1
|
||||
|
||||
## Scenario 2
|
||||
|
||||
Gets the list of `SalesPersonPersonID` and their total sales order counts (as `TotalOrders`) from `Sales.Orders`.
|
||||
|
||||
Order the results by the highest total orders.
|
||||
|
||||
Rows: 10
|
||||
|
||||
## Scenario 3
|
||||
|
||||
Get the customer ID and total number of invoices (as `TotalInvoices`) from `Sales.Invoices` for the year 2015.
|
||||
|
||||
Group the invoices by the customer ID.
|
||||
|
||||
Order the results by the highest number of invoices descending followed by the customer ID.
|
||||
|
||||
Rows: 657
|
||||
|
||||
## Scenario 4
|
||||
|
||||
Get the order ID and total price (as `TotalOrderPrice`) from `Sales.Orders` for the customer `Debbie Molina` for the year 2016.
|
||||
The total order price is the `Quantity` x `UnitPrice` of the order lines associated with the order.
|
||||
|
||||
Order the results by the total price from highest to lowest.
|
||||
|
||||
Rows: 10
|
||||
|
||||
## Scenario 5
|
||||
|
||||
Get the customer ID and total orders (as `TotalOrders`) from `Sales.Orders` for all customers who have at least 20 orders in 2016.
|
||||
|
||||
Order the results by the total orders descending and then by customer ID.
|
||||
|
||||
Rows: 87
|
||||
|
||||
## Scenario 6
|
||||
|
||||
Get the stock item ID, stock item name from and total quantity sold of the top 50 stock items sold (based upon quantity) of all time.
|
||||
|
||||
Order the results by the total quantity descending.
|
||||
|
||||
This will require two queries. The first query will get the stock items and the total quantity sold from the `OrderLines` table. For example if stock item 1
|
||||
is on 3 orders with a quantity of 1, 3 and 5 then the total quantity is 9.
|
||||
|
||||
The second query will combine the stock item totals with the stock items from `Warehouse.StockItems` to get the top 50 items.
|
||||
|
||||
*Note: This is using `OrdersLines` and not `InvoiceLines`.*
|
||||
|
||||
*Note: This requires a subquery or CTE. A join by itself will not work.*
|
||||
|
||||
Rows: 50
|
||||
|
||||
## Scenario 7
|
||||
|
||||
Get the customer ID, customer name and total orders for the top 10 customers who have the LEAST number of orders.
|
||||
|
||||
Order the results by total orders and then by customer name.
|
||||
|
||||
This will require two queries. The first query will get the customers and their total orders.
|
||||
|
||||
The second query will combine the total order query with the remaining customer data to get the top 10.
|
||||
|
||||
*Note: Some customers may have no orders so they should show up in the top 10, if they exist.*
|
||||
|
||||
*Note: This will require either a subquery or a CTE.*
|
||||
|
||||
Rows: 10
|
||||
|
||||
## Scenario 8
|
||||
|
||||
Get the delivery method ID and name from `Application.DeliveryMethods` that are not used in the `Sales.Customers` table.
|
||||
|
||||
Order by the delivery method name.
|
||||
|
||||
This will require two queries. The first query should get the delivery methods that are not being used.
|
||||
|
||||
The second query should combine the unused methods with the remaining data needed to generate the desired results.
|
||||
|
||||
*Note: This will require either a subquery or a CTE.*
|
||||
|
||||
Rows: 9
|
73
labs/database-programming/lab-2/lab-2.sql
Normal file
73
labs/database-programming/lab-2/lab-2.sql
Normal file
|
@ -0,0 +1,73 @@
|
|||
USE WideWorldImporters
|
||||
|
||||
-- Scenario 1
|
||||
SELECT MIN(Temperature) AS LowestTemperature, MAX(Temperature) AS HighestTemperature
|
||||
FROM Warehouse.VehicleTemperatures
|
||||
|
||||
-- Scenario 2
|
||||
SELECT SalesPersonPersonID, COUNT(*) AS TotalOrders
|
||||
FROM Sales.Orders
|
||||
GROUP BY SalesPersonPersonID
|
||||
ORDER BY TotalOrders
|
||||
|
||||
-- Scenario 3
|
||||
SELECT CustomerID, SUM(InvoiceID) AS TotalInvoices
|
||||
FROM Sales.Invoices
|
||||
WHERE InvoiceDate BETWEEN '1/1/2015' AND '12/31/2015'
|
||||
GROUP BY CustomerID
|
||||
ORDER BY TotalInvoices, CustomerID
|
||||
|
||||
-- Scenario 4
|
||||
SELECT od.OrderID, ol.Quantity * ol.UnitPrice AS TotalOrderPrice
|
||||
FROM Sales.Orders od
|
||||
JOIN Sales.OrderLines ol ON ol.OrderID = od.OrderID
|
||||
JOIN Sales.Customers cu ON od.CustomerID = cu.CustomerID
|
||||
WHERE cu.CustomerName = 'Debbie Molina' AND od.OrderDate BETWEEN '1/1/2016' AND '12/31/2016'
|
||||
ORDER BY TotalOrderPrice
|
||||
|
||||
-- Scenario 5
|
||||
SELECT *
|
||||
FROM (SELECT CustomerID, COUNT(OrderID) AS TotalOrders
|
||||
FROM Sales.Orders
|
||||
WHERE OrderDate BETWEEN '1/1/2016' AND '12/31/2016'
|
||||
GROUP BY CustomerID) Results
|
||||
WHERE TotalOrders >= 20
|
||||
ORDER BY TotalOrders DESC, CustomerID
|
||||
|
||||
-- Scenario 6
|
||||
WITH
|
||||
Results
|
||||
AS
|
||||
(
|
||||
SELECT TOP 50
|
||||
ol.StockItemID, SUM(Quantity) TotalQuantity
|
||||
FROM Sales.OrderLines ol
|
||||
JOIN Warehouse.StockItems si ON ol.StockItemID = si.StockItemID
|
||||
GROUP BY ol.StockItemID
|
||||
ORDER BY TotalQuantity DESC
|
||||
)
|
||||
SELECT Results.StockItemID, StockItemName, TotalQuantity
|
||||
FROM Results JOIN Warehouse.StockItems ON WareHouse.Stockitems.StockItemID = Results.StockItemID
|
||||
ORDER BY TotalQuantity DESC
|
||||
|
||||
-- Scenario 7
|
||||
WITH
|
||||
Results
|
||||
AS
|
||||
(
|
||||
SELECT cu.CustomerID, COUNT(OrderID) OrderCount
|
||||
FROM Sales.Customers cu
|
||||
LEFT JOIN Sales.Orders od ON cu.CustomerID = od.CustomerID
|
||||
GROUP BY cu.CustomerID
|
||||
)
|
||||
SELECT TOP 10
|
||||
Results.CustomerID, CustomerName, OrderCount
|
||||
FROM Results
|
||||
JOIN Sales.Customers ON Results.CustomerID = Sales.Customers.CustomerID
|
||||
|
||||
-- Scenario 8
|
||||
SELECT DeliveryMethodID
|
||||
FROM Application.DeliveryMethods
|
||||
WHERE DeliveryMethodID NOT IN (SELECT DeliveryMethodID
|
||||
FROM Sales.Customers)
|
||||
ORDER BY DeliveryMethodName
|
Loading…
Add table
Add a link
Reference in a new issue