You are on page 1of 7

ANSWER SHEET

STUDENT NAME (1 POINT) :

HW MAJOR – SQL QUERIES

PART A (PK) ANSWER (FK)


1. Region (RegionID) + USState (RegionID)
2. USState (StateID) +
3. Customer (CustID) +
4. Customer (CustID) +
5. SalesOrder (SOID) +
6. Product (ModelID) +
7. Product (ModelID) +

PART B ANSWERS (9 POINTS EACH)


1. For each customer state, output the state abbreviation (CustState) and the average household
income for all customers in that state. Order results alphabetically by state abbreviation and be sure
that the average household income column includes a meaningful column name. (Hint: You only
need to use one table for this query since you are not asked to give the actual name of the state.)
(Remember: only copy the top 10 rows – do not worry about the Headers or Copy with Headers).

SQL Query Syntax Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 1 of 7


2. Output the customer name (Fname and Lname), marital status, state (use the actual state
name), and household income for all customers located in Colorado (CO), Utah (UT), Washington
(WA), and Wyoming (WY) who have a household income of under $119,500 and are married.
Order results alphabetically by last name. Your query should not include the OR operator.
(Remember: only copy the top 10 rows – do not worry about the Headers or Copy with Headers).

SQL Query Syntax Output of the Query*

3. For all orders placed on August 9, 2019 and valued at $1,125 or less, show the customer’s name
(first and last), the order number, and the order total. Results should be put in order from the largest
value to the smallest. (Remember: only copy the top 10 rows – do not worry about the Headers or
Copy with Headers).

SQL Query Syntax Output of the Query*

4. For each state, what percentage of the state’s population purchased a DustBunny vacuum
(assume everyone in the customer table has purchased a DustBunny vacuum)? Your output should
include the state name, population, number of purchasers from that state, and the percentage
(show as a decimal). Order the output from the highest percentage to the lowest and make sure
each column has a usefully descriptive name. (Remember: only copy the top 10 rows – do not
worry about the Headers or Copy with Headers).

SQL Query Syntax Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 2 of 7


5. Show the five orders that included the highest number of items (e.g., if an order included 2 of one
type of vacuum and 1 of another, that equates to three items). For each order, show the customer
first name, customer last name, number of items, the order date, and the total value of the order.
Order results from highest number of items to lowest.

SQL Query Syntax Output of the Query*

6. Which states’ customers have an average household income over $120,000? Output the name
of each state that meets this requirement, the name of the region where the state is located, and
the states’ customers’ average household income (HHI); order the output in descending order from
highest HHI to lowest and be sure each column has a meaningful name.

SQL Query Syntax Output of the Query*

7. How many units of each vacuum model were sold during the period from August 11-20, 2019,
and what was the total revenue generated by those sales? Order results from the product with the
highest revenue to the lowest, include the ModelName in your output, and be sure that all columns
have meaningful names. Your output should only include vacuum models (not warranties).

SQL Query Syntax Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 3 of 7


8. What is the most popular product for customers who own pets? Answer the question by
outputting the name of the most popular product and the number of units sold to pet owners as well
as the total amount of revenue generated from the sale of those units to pet owners. Be sure your
columns have meaningful names and that your query only outputs the one most popular product.

SQL Query Syntax Output of the Query*

*If the query returns more than 10 lines, include only the first 10 lines of output.

PART C ANSWERS (5 POINTS EACH)

1. Assume you wanted to add another state to the database, the state of Nevernia. Nevernia has a
state code of NA, is in region 2, and has a population of 40,000. Nevernia is a small state. Include
in your submission (1) the query that you would use to add this additional record to the database
and (2) the output of a query that shows the StateIDS, RegionID, Population, and StateName of all
states with a population under 1,000,000 AS WELL AS the query you used to generate this output.

INSERT INTO Query Syntax (1)

SELECT Query Syntax (2) Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 4 of 7


2. Vivian Dunn (CustID 936) was recently married. Vivian has formally requested a name change
and also a marital status change. Change the Customer table in your personal database so that
Vivian Dunn’s last name is now Bryant and her marital status is changed from ‘single’ to ‘married.’
Run the following query to verify Vivian’s Customer ID and to get acquainted with all the customers
in the Customer table with the Lname “Dunn” or “Bryant” (note: only one last name—Vivian’s—of
the 6 records should change when you run your UPDATE query):

SELECT *
FROM Customer
WHERE Lname = 'Dunn' OR Lname = 'Bryant';

Include in your submission (1) the query that you would use to change (update) Vivian’s record in
the database and (2) the output of a query that shows Vivian’s updated record AS WELL AS the
query you used to generate this output.

UPDATE Query Syntax (1)

SELECT Query Syntax (2) Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 5 of 7


PART D ANSWERS (10 POINTS)

 Design an original query you think could help lead to important insight for the company.
 The query should not be something “obvious” (e.g., do not count the number of products the
company sells), should include more than one table, and should be ordered meaningfully.

Describe what the query will output (i.e., what are you trying to show with the query?).
Describe how DustBunny might use this query to gain insight into their business.

SQL Query Syntax Output of the Query*

*If the query returns more than 10 lines, include only the first 10 lines of output. Do not about
copying the Column Headers.

ANSWER SHEET HW Major – SQL Queries Page 6 of 7


EXTRA CREDIT ANSWERS (2 POINTS EACH)

1. For each order placed on August 18th, 2019, how many unit items (a unit item is 1 vacuum
cleaner; two of the same model would be 2 unit items; one of one model and one of another would
also be 2 unit items) were purchased? Output only those orders that included two or more unit
items, include in your output the order number and the number of unit items, and order your output
by order number (lowest to highest). (Hint: you will need two tables, SalesOrder and OrderLine.)
(Remember: only copy the top 10 rows – do not worry about the Headers or Copy with Headers).

SQL Query Syntax Output of the Query*

2. What is the total value of all orders placed for each region? The output should include the name
of the region as well as the total value of all orders placed there. Order results alphabetically by
region. (Hint: the USState table and the Customer table will need to be part of your query.) Based
on these results, to which region do you think the company marketed most heavily during the early
part of August 2019?

SQL Query Syntax Output of the Query*

3. Which three customers bought the highest number of MadHare units during the period covered
by the database? Your output should include first name, last name, and the number of units
purchased and should include information only about the three customers with the highest number
of MadHare units. Be sure your query references the Model table (i.e., do not look up the model
number for MadHare, instead include in your query content that figures out which records refer to
‘MadHare’).

SQL Query Syntax Output of the Query*

ANSWER SHEET HW Major – SQL Queries Page 7 of 7

You might also like