Question 1: Select All Data in a Table
Again, to complete the following 10 questions. When the questions for this assignment do not specify which rows to show in the results, assume that all rows should be shown. Similarly, when the questions do not specify which columns to show in the results, assume that all columns should be shown. When the questions specify that only certain columns should be included in the results, this does not imply that other columns cannot be used in the query (they should simply not be part of the output).
1) Select all data in a table:
Use the OrderDetails table and create a query that displays all the data in this table.
2) Select specific columns:
Use the OrderDetails table and create a query that displays ProductID and Quantity only.
3) Select rows using WHERE:
3.1) Use the OrderDetails table and create a query that displays order line items (rows):
a) with OrderID 10251.
b) with unit prices more than $200.
c) that are not for product 38.
d) with unit prices more than $50 and quantity more than 25.
e) with unit prices between 50 and 75 (inclusive of both 50 and 75).
f) with line item total, UnitPrice*Quantity*(1?Discount), of more than $6,000.
g) with quantity more than 100 or unit price more than 200.
3.2) Use the OrderHeaders table and create a query that displays all orders (rows):
a) placed in 2019 (you can assume that there is no later orders).
b) that were shipped after they were required.
c) that have not yet been shipped (shipped date is null).
3.3) Use the OrderDetails table and create a query that displays:
a) all unique (distinct) products sold (only show Product IDs in the results).
b) all unique products sold with unit prices minus discounts of more than $50.
3.4) Sort (order by) and Select Top (note SQLite uses limit):
Use the products table and create a query that shows:
a) products in descending order sorted by UnitsInStock.
b) the first five products in the products table.
c) the bottom 15 products in terms of UnitsInStock.
3.5) Like and wildcards:
Use the Customers table and create a query that shows all customers with:
a) "manager" in the ContactTitle.
b) company names that start with F.
4) Aliases and Case:
Create new columns (fields) in result sets.
a) Use the Products table and create a query that shows ProductName, UnitsInStock, UnitsOnOrder, ReorderLevel, and a calculated field named UnitsAvailable defined as UnitsInStock + UnitsOnOrder.
Question 2: Select Specific Columns
b) Use the Products table and create a query that shows ProductName, UnitsInStock, UnitsOnOrder, ReorderLevel, the calculated field UnitsAvailable (copy and paste the query you created in 4a), and a new calculated field named ReorderNeeded that displays “Yes” if UnitsAvailable < ReorderLevel and otherwise “No”.
5) Create views:
a) Copy and paste the query in 4a and create a view called ProductAvailability based on this query.
b) Using the ProductAvailability view from 5a, create a query that only shows products that need to be ordered (that have UnitsAvailable less than the ReorderLevel).
6) Group and aggregate (e.g., count, avg, sum):
a) Using the Products table, create a query that shows for each supplier: the SupplierID and the number of products associated with the supplier (name this field NumberOfItems).
b) Using the OrderDetails table, create a query that shows for each order the OrderID and the total quantity sold (name this field TotalQuantity).
c) Using the OrderDetails table show for each product: the ProductID, the average sales unit price (name this field AverageUnitPrice; you can simply calculate the average for each product across the different order detail rows and you do not need to adjust the average for the quantity sold in each order), the total quantity sold (name this field SumOfQuantitySold), and the number of times it has been sold (name this field NumberOfSales).
7) Join tables:
a) Using the OrderHeaders and OrderDetails tables, show OrderID (from the OrderHeaders table), OrderDate, ProductID, and Quantity. You can assume that all OrderHeaders have OrderDetails and that all OrderDetails have OrderHeaders.
b) Copy and paste query 7.a and only show orders placed in 2019.
8) Join, Where, Group, and Having:
a) Copy and paste query 7.b, but only show each ProductID and for each product show a new calculated field named SumOfQuantitySoldIn2019 defined as the total quantity sold in 2019. Store this query as a view called ProductSales2019.
b) Copy and paste query 7.b., but only show products with SumOfQuantitySoldIn2019 greater than 100.
c) Create the same output as 8.b, but use the ProductSales2019 view (from 8.a) instead of the OrderHeaders and OrderDetails tables.
9) Left join (same as left outer join):
a) Using the Products table and ProductSales2019 view show for all products, the quantity sold in 2019. Display the (i) ProductID and (ii) ProductName for all products in the Products table, and (iii) SumOfQuantitySoldIn2019 (include null values for products not sold in 2019).
b) Using the Products table and ProductSales2019 view, show ProductID and ProductName for all Products that were not sold in 2019.
10) Inner joins: Join three tables:
Using the OrderDetails, OrderHeaders, and Products tables, determine for each product the average price received in 2019 and compare this amount to the list price (the unit price in the Products table). In your output display (i) ProductID, (ii) AveragePriceReceived (i.e., average sales unit price in 2019 for each product where sales unit price is the OrderDetails unit price after discounts), (iii) UnitPrice from the products table, and (iv) a new calculated field that calculates the percentage difference between AveragePriceReceived and UnitPrice in the products table. Name this field PercentPriceDifference