I have home work on SQL codes and I need help to do it.

profilefzdaham
hw6-sql3-2016.zip

HW6-SQL3-2016.doc

(SQL)

Write the SQL codes needed to complete each of tasks listed below. The data are in a file called Order.mdb. Run your SQL codes on the given DB, and paste the query results immediately following the corresponding SQL statement: code, result; code, result.

Note: One of the tables is named “Order”. Please note that “Order” is also an SQL reserved word; so the table Order, when being referred to in its full name, must be enclosed in a pair of brackets as in the form [Order]. || 1-3: 2 pts each; 4-8: 3 pts each; 9: 4 points.

1. List all items that sold for less than cost in the month of December. Hint: Use the function MONTH(date) to return the month of a date.

2. List the inventory category and total quantity (by each category) ordered in the year 2015 (in descending order of total quantity). Hint: Use the function YEAR(date) similar to MOMTH(date).

3. List the Spring products ordered by florists that had a list price over $200. [Watch: (1) Join MANY tables!! (2) same field name appear in multiple tables; (3) same field name in multiple tables could mean different things!!]

4. [This problem has TWO questions] List the customer ID and total orders for each customer, in descending order of total orders. (Compute once for total number of orders, and once for total dollar amount – so two queries)

5. List the item ID and description of each product that has not been ordered during the last quarter of any year. [Please be EXACT in implementing the logic AS IS STATED – do NOT do your “substitution” to anything that is not EXACTLY stated in the problem; i.e., “not been ordered during the last quarter” EXACTLY means it states; it is NOT “those ordered outside last quarter” – because those ordered outside Q4 could also be ordered in Q4]

6. List the names of the employees who did not sell any items from the garden shop during the period from June to July. [Note: Same note as that for the previous question]

7. List the manager name and total sales revenue attributable to each manager. (The list should be in descending order by total sales revenue.) 【Self-join!】

8. (Brand-new) List the item ID and description of those items whose price is higher than the average price of item in its category. Display also the average price of the categories and name them CatAvg (so the columns will be: itemID, description, listPrice, CatAvg). Hint: The same logic as the Week 15 demo “List restaurants whose sales higher than the average sales in its own city” – passing a parameter from outer query into the inner query.

9. BONUS. Use the “Restaurants” database, write ONE query (with subquery) to list

a. Restaurant’s ID , state , Type of service , annual sales ;

b. Average sales of the restaurant’s state and type (simultaneously, i.e., the average of a specific state and specific type);

c. For those restaurants whose sales are greater than that in “b”

Hint: Also need to pass parameters into subquery.

One query

order2016.mdb

CID Name Address City State Zip Category
1 Celine Dion 143 Canada Way Boston MA 62302 FLO
2 Warren Hill 324 East 7th Boston MA 62301 GOV
3 Leon Redbone 2934 Simon Dr. Springfield IL 12364 FLO
4 Stephen C. Chapman 12 Oakwood Suite 234 Boston MA 62310 GOV
5 Marvin Matthews 234 Sandy Ln # 128 Springfield IL 12354 WALK
6 Jim Brickman 787 Sureway Boston MA 62320 WALK
7 Ray Boltz 333 Ziptree Boston MA 62301 WALK
8 Charlie Peacock 23 Paris Ln Ft. Worth TX 76132 FLO
EID Name Address City State Zip Phone Specialty ManagerID
1 Kaethe Oswald PO Box 23239 Boston MA 62345 5555552390 I
2 Joan Switcher PO Box 30202 Springfield IL 12334 5555552111 J
3 Katie Hayes PO Box 390, Boston, MA, 23923 Boston MA 62344 5555552310 K
4 Smitty Clark PO Box 302, Springfield, IL, 23940 Springfield IL 12344 5555552321 L
10 John Samual PO Box 1243, Boston, MA 75234 Boston MA 62388 5555551223 A 1
11 Susan Smith PO Box 434, Boston, MA 12345 Springfield IL 12355 5555552354 B 1
12 Carla Jones PO Box 11, Springfield, IL, 12323 Springfield IL 12345 5555551231 C 2
13 Kevin Alexander PO Box 10340, Springfield, IL, 12433 Springfield IL 12344 5555559993 D 2
14 Samantha Technick PO Box 123, Boston, MA, 23423 Boston MA 62311 5555551303 E 3
15 Louise King PO Box 2340, Boston, MA, 20304 Boston MA 62321 5555559394 F 3
16 Joe Chicken PO Box 239403, Boston, MA, 23203 Boston MA 62332 5555552930 G 4
17 Diana Gonzales PO Box 4930293, Boston, TX 73849 Boston MA 62312 5555552233 H 4
ItemID Description ListPrice Cost QuantityOnHand Category
1223 Dog Food 1.75 1.25 3 DOG
1224 Dog Brush 3.75 2.75 20 DOG
1225 Dog Toothbrush 4.1 3.5 2 DOG
1300 Cat Food 1.5 0.5 2 CAT
1302 Cat Toy 3.75 2.75 25 CAT
1502 Spade 4.5 3.75 15 GAR
1504 Rake 5.99 4.8 10 GAR
3445 Hammer 3.0 2.5 23 TOOL
3446 Hammer 4.25 3.75 15 TOOL
8766 Wrench 4.75 4.1 18 TOOL
8776 Wrench 5.5 4.75 12 TOOL
9765 Archway 275.0 216.0 2 FLO
Category Description StoreLocation Season ManagerID
CAT Cat Item 123 SPRING 1
DOG Dog Item 342 SPRING 2
FLO Florist Supplies 234 SPRING 3
GAR Garden Items 351 SUMMER 4
TOOL Tools 351 SUMMER 4
OrderID OrderDate CID EID
12200 2016-01-15 8 10
12305 2016-02-14 6 12
12306 2015-02-23 7 16
12456 2016-03-01 1 12
12457 2015-03-07 1 17
12458 2016-03-12 8 12
12459 2015-06-02 2 11
12470 2015-06-12 6 13
12471 2014-06-17 5 13
12472 2014-06-20 4 17
12480 2014-06-28 3 11
12500 2014-07-15 1 14
12501 2014-07-15 4 15
12502 2014-11-15 6 17
12503 2015-11-23 5 11
12504 2015-12-03 3 10
12602 2014-12-30 1 15
12948 2014-12-30 3 16
OrderID ItemID Quantity SalePrice
12200 1223 11 1.75
12305 1224 1 3.75
12306 1504 2 6.0
12456 1300 1 1.0
12457 8776 1 5.0
12458 1223 2 2.0
12459 1300 2 1.0
12470 1224 2 3.75
12470 1225 1 4.1
12471 1224 1 3.5
12500 1224 2 4.0
12500 8776 3 5.0
12501 1223 3 2.0
12502 1224 2 2.0
12502 3445 1 2.8
12503 1300 1 1.0
12504 8776 1 5.0
12602 9765 1 270.0
12948 1223 10 1.75
12948 1224 1 3.5

Restaurants-Rev16.accdb

FranchiseeID FirstName LastName HomeAddress HomeCity HomeState HomeZip HomePhone CellPhone CreditRating
F001 James Antonucci 8990 SW 71 Avenue Miami FL 33176 3057778888 3055909090 AA
F002 Dottie Balchunas 400 Canopy Blvd Pensacola FL 32504 8502221111 8502229898 B
F003 Steve Spann 5900 West Highway Boca Raton FL 33427 5619991199 5618998899 a
F004 Benjamin Grauer 4 Grove Road Orlando FL 32801 4074448888 4076662266 C
F005 Megan Miller 903 SW 1 Street Vero Beach FL 32960 4073330033 4074449900 B
F006 Jessica Kinzer 1239 College Drive Gainesville FL 32601 3529990044 3529998811 C
F007 Carlos Portu 9500 Reseda Blvd Northridge CA 91324 8187878778 8188899877 AA
F008 Carol Wong 9888 Century Bl Los Angeles CA 90401 3102104100 3102114111 AA
RestaurantID Address City State Zipcode Phone AnnualSales SquareFootage DateOpened TypeofService Visa/MasterCard AmericanExpress DiscoverCard FranchiseeID
R0001 2345 SW 98 Street Miami FL 33133 3054448787 ¤ 575,000.00 2000 1998-06-01 Table Service true false true F001
R0002 3487 Main Highway Pensacola FL 32504 8508865555 ¤ 350,000.00 1000 1999-04-15 Table & Take-out true false false F002
R0003 89 Turnberry Drive Orlando FL 32801 4075559999 ¤ 750,000.00 2500 2000-01-01 Table Service true true true F004
R0004 4598 SW 136 Street Miami FL 33156 3054444444 ¤ 200,000.00 750 1998-03-01 Take-out true false false F001
R0005 9000 Biscayne Blvd Tallahassee FL 32301 9042221111 ¤ 800,000.00 2200 2000-12-01 Table & Take-out true true true F003
R0006 2 State Street Boca Raton FL 33427 5614441100 ¤ 175,000.00 750 2001-05-01 Take-out false false false F003
R0007 8990 SE 2 Avenue Miami FL 33133 3057877889 ¤ 500,000.00 1800 2001-03-31 Table Service true true true F001
R0008 298 W 75 Terrace Vero Beach FL 32960 4072229999 ¤ 350,000.00 2850 2000-06-01 Table & Take-out true false false F005
R0009 1000 Grand Avenue Gainesville FL 32601 3526667788 ¤ 700,000.00 1000 1999-05-31 Take-out true false false f006
R0010 6767 NW 75 Street Miami FL 33120 3058878877 ¤ 750,000.00 1200 1999-04-03 Table Service true true false f005
R0011 7890 Concord St Orlando FL 32801 ¤ 675,000.00 1300 2005-05-05 Table & Take-out false false false F004
R0012 979 Alba Dr Orlando FL 32801 ¤ 555,000.00 2000 2006-06-06 Table & Take-out false false false F004
R0013 7050 Biscayne Blvd Tallahassee FL 32301 ¤ 580,000.00 1800 2007-07-07 Table Service false false false F002
R0014 1550 Grand Avenue Gainesville FL 32601 ¤ 480,000.00 2200 2008-08-08 Table & Take-out false false false F006
R0015 9999 Reseada Ave Northridge CA 91324 ¤ 366,000.00 1900 1991-01-01 Take-out false false false F002
R0016 19010 Plummer St Northridge CA 91324 ¤ 876,000.00 2222 2006-01-01 Table Service false false false F002
R0017 8989 Tampa Ave Northridge CA 91324 ¤ 888,000.00 2345 1999-12-30 Table & Take-out false false false F004
R0018 8765 Corbin Ave Northridge CA 91324 ¤ 654,000.00 1987 1998-12-12 Table & Take-out false false false F007
R0019 1777 Main St Santa Monica CA 90401 ¤ 765,000.00 2000 2001-01-01 Table & Take-out false false false F008
R0020 135 Lincoln Santa Monica CA 90406 ¤ 399,000.00 1100 2006-06-06 Take-out false false false F008
SELECT DISTINCTROW * FROM Franchisees;
SELECT DISTINCTROW * FROM Restaurants;
SELECT DISTINCTROW * FROM [Query1-ex];
SELECT Restaurants.RestaurantID, Restaurants.Address, Restaurants.City, Restaurants.State, Restaurants.Zipcode, Franchisees.FranchiseeID, Franchisees.FirstName, Franchisees.LastName, Franchisees.HomePhone, Restaurants.AnnualSales, Restaurants.SquareFootage, Restaurants.TypeofService FROM Franchisees INNER JOIN Restaurants ON Franchisees.FranchiseeID=Restaurants.FranchiseeID;
SELECT restaurantID, annualsales, city FROM Restaurants AS R WHERE annualsales> (select avg(annualsales) from restaurants where city = r.city group by city) ORDER BY city;