Download presentation
Presentation is loading. Please wait.
Published byRosamund Walters Modified over 8 years ago
1
In-Class SQL Query Exercises For the Plumbing Supply Store Database
2
Copyright © 2016, Jonathan Eckstein Single-Table Queries Without Filtering Write SELECT queries showing the following: a)The name of each product b)All days on which order have been placed, with no duplicate entries in the case of more than one order placed on the same day c)The entire PRODUCT table d)For each product, the product ID, product name, and the ratio of the number of units on order to the number of units in stock.
3
Copyright © 2016, Jonathan Eckstein Single-Table Queries With Filtering a)Show the name and unit price of all products priced below $50 (note: SQL treats currency amounts like any other number, so you should not use a $ sign or quotes in your query.) b)Show the product name, units on order, and units in stock for all products for which the number of units on order is at least 40% of the number of units in stock c)Show the same information as in part (b), but with the additional restriction that the number of units on order is no more than 10 d)Show the first name, last name, city, and state of all customers outside New Jersey (NJ) e)Show the first name, last name, city, and state of all customers who are outside New Jersey or have the first name “Robert” (or both).
4
Copyright © 2016, Jonathan Eckstein Simple Inner Joins Write queries using INNER JOIN for the following tasks: a)Show the entirety of each combination of a record from the ORDERDETAIL table and a record from the PRODUCT table for which the ProductID fields match. b)For same combinations of ORDERDETAIL and PRODUCT records as in part (a), show the entirety of the ORDERDETAIL record, but only the product name and unit price fields of the PRODUCT record. c)Show the same information as in part (b), but only for items costing at least $1000. d)Produce the same output as in part (c), but with an additional column showing the unit price of the product multiplied by the quantity ordered. e)For all combinations of a record from the ORDERDETAIL table and a record from the PRODUCT table that have matching ProductID fields and also have a quantity ordered that is at least half the number of units in stock, show the the quantity ordered, the product name, and the number of units in stock.
5
Copyright © 2016, Jonathan Eckstein A Cartesian Join Write a query that shows the first name, last name, and product name for all possible combinations of a record from the CUSTOMER table and record from the PRODUCT table, without regard to whether the customer has ever ordered the product.
6
Copyright © 2016, Jonathan Eckstein Inner Joins through WHERE Write queries not using INNER JOIN for the following tasks: a)Show the entirety of each combination of a record from the ORDERDETAIL table and a record from the PRODUCT table for which the ProductID fields match. b)For same combinations of ORDERDETAIL and PRODUCT records as in part (a), show the entirety of the ORDERDETAIL record, but only the product name and unit price fields of the PRODUCT record. c)Show the same information as in part (b), but only for items costing at least $1000. d)Produce the same output as in part (c), but with an additional column showing the unit price of the product multiplied by the quantity ordered. e)For all combinations of a record from the ORDERDETAIL table and a record from the PRODUCT table that have matching ProductID fields and also have a quantity ordered that is at least half the number of units in stock, show the quantity ordered, the product name, and the number of units in stock.
7
Copyright © 2016, Jonathan Eckstein More WHERE-Style Inner Joins a)Write a query that shows, for each row of the ORDERDETAIL table, the first and last name of the corresponding customer, the date of the order, the quantity ordered, and the name of the product. Use only WHERE conditions to join the tables. b)Create a query producing the same output using MS Access design view. Switch to SQL view and compare the contents of the FROM clause to what you wrote in part (a). c)Perform the same query as part (a), using WHERE to join tables, but only include rows for which the quantity ordered is at least 5. d)Repeat part (c), but include all rows for which the quantity order is at least 5 or the unit price is over $1500
8
Copyright © 2016, Jonathan Eckstein Aggregation without Grouping a)Show the total number of units of held in stock (summed across all products) b)Show the total value of inventory held, with each unit of inventory is valued at its unit price c)Show the total value of inventory held in products whose price is below $50 d)Show the total value of inventory held in products whose price is between $100 and $750 (inclusive) e)Show the number of products whose unit price is under $200.
9
Copyright © 2016, Jonathan Eckstein Aggregation with Grouping a)For each customer, show the Customer ID, first name, last name, and the total number of orders placed b)Show the same information about customers as in part (a), but count only orders placed since April 28, 2013; if a customer has place no orders since that time, they should not appear in the output c)Show the same information as in part (c), but also include the date of each customer’s most recent order d)For each product, show its name and average quantity ordered when it appears in orders placed by customers from New Jersey (state “NJ”).
10
Copyright © 2016, Jonathan Eckstein Criteria after Aggregation a)Show the name and total revenue for each product for which you have at least $2,000 in revenue b)Show the name and total revenue between April 22 and 27 (inclusive) for each product for which you have at least $1,500 in revenue between those dates c)Show the first name, last name, and number of orders for all customers who have placed at least 3 orders d)Show the first name, last name, and number of orders placed on or after April 25, 2013, for all customers who have at least 2 such orders.
11
Copyright © 2016, Jonathan Eckstein Simple Sorting a)Show the name, unit price, units in stock, and units on order for each product, sorted alphabetically by product name b)Show the same information as in part (a), but sorted from the largest number of units in stock to the smallest. For products with the same number of units in stock, sort the rows of output from the largest number of units on order to the smallest c)Show the same information as in parts (a) and (b), and also the inventory position for each product, which is the sum of the units in stock and the units on order. Sort the output from largest to smallest inventory position d)Show the same information as in part (c), but only for products with a unit price of at least $1,000
12
Copyright © 2016, Jonathan Eckstein Sorting Data from Multiple Tables a)Show the name of each product and the total amount of revenue it has generated. The output should be sorted by revenue, with the highest-revenue products first; products that have never been ordered need not appear in the output b)Show the same information as in part (a), but only for items that have generated at least $1,000 of revenue c)Show the same information as part (a), but including only revenue from orders placed April 27, 2013 or later; products not ordered since that date need not appear in the output d)Show the same information as part (a), but also show the total number of units ordered for each product. Sort the output from the largest to the smallest total number of units ordered; within products with the same total number of units ordered, sort from the largest to smallest total revenue.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.