SQL Practice - Cosmetics Database
SQL Practice - Cosmetics Database
Transaction
TransactionID TransactionDateTime CustomerID Subtotal Tax Discount TotalBill
22000001 2022-04-08 12:35:29.123 1001 159.00 15.90 31.80 143.10
22000002 2022-04-08 02:32:21.173 1002 20.00 2.00 1.00 21.00
22000003 2022-04-08 16:31:19.829 1003 89.99 9.00 9.00 89.99
22000004 2022-04-09 03:15:10.321 1002 355.00 35.50 17.75 372.75
22000005 2022-04-09 07:53:23.100 1002 75.00 7.50 3.75 78.75
22000006 2022-04-10 13:13:22.543 1004 269.97 27.00 13.50 283.47
22000007 2022-04-15 12:59:41.539 1001 89.99 9.00 18.00 80.99
22000008 2022-04-28 19:12:09.103 1005 42.00 4.20 8.40 37.80
Customer
CustomerID CustomerName City MemberLevel MemberDiscount
1001 NULL Manila 3 0.2
1002 BTS Seoul 1 0.05
1003 BollyWood New Delhi 2 0.1
1004 James Brown Seoul 1 0.05
1005 Wingroup Manila 3 0.2
TransactionDetails
TransactionID ProductID Quantity ProductSubtotal
22000001 AC1001 1 75
22000001 AC1002 2 84
22000002 CZ1001 1 20
22000003 BE1002 1 89.99
22000004 AC1001 1 75
22000004 BE1001 2 240
22000004 CZ1001 2 40
22000005 AC1001 1 75
22000006 BE1002 3 269.97
22000007 BE1002 1 89.99
22000008 AC1002 1 42
Product
ProductID ProductName ItemCategory BoxPrice
AC1001 Smoothy Lotion 75.00
AC1002 Silky Lotion 42.00
BE1001 Smelly Perfume 120.00
BE1002 Fragrant Perfume 89.99
CZ1001 Clean Soap 20.00
SET 1 (CRUD)
1. Give the CREATE script for the table Transaction (no keys).
a. Use Identity
2. Give the CREATE script for the table Customer (no keys).
a. Use Identity
b. Set Default City to Hanoi, and Default MemberLevel to 0.
3. Give the CREATE script for the table TransactionDetails (no keys).
a. Set Quantity to NOT NULL
4. Give the CREATE script for the table Product (no keys).
5. Give the ALTER TABLE script to add 1 primary key and a foreign key (to Customer) in
Transaction.
a. Choose CASCADE for update and SET DEFAULT for delete
6. Give the ALTER TABLE script to add 1 primary key in Customer.
7. Give the ALTER TABLE script to add the primary key and 2 foreign keys (to Product and to
Transaction) in TransactionDetails.
a. Choose CASCADE for update and SET DEFAULT for delete
8. Give the ALTER TABLE script to add 1 primary key in Product.
9. Give the INSERT INTO script for the 1st line in Transaction.
10. Give the INSERT INTO script for the 1st line in Customer.
11. Give the INSERT INTO script for the 1st line in TransactionDetails.
12. Give the INSERT INTO script for the 1st line in Product.
13. Give the DELETE script to delete all transactions from CustomerID 1001.
14. Give the DELETE script to delete the all customers with Member Level 3.
15. Give the DELETE script to delete all transactions.
16. Give the DELETE script to delete products whose Boxprice is less than 50.
17. Update the Customer Name of 1001 to “Son Goku”.
18. Update the Quantity of TransactionID 22000001 and Product ID AC1001 to 5.
19. Update the Product Name of CZ1001 to “Cleaner”.
20. Update the MemberDiscount rate of all Member Levels = 3 to 0.25.
21. Update all cities from Manila to “Metro Manila”.
22. Update the tax column to be equal to 15% of the Subtotal (instead of 10%)
23. Update all Perfumes to increase their price +10.
24. Update all Transaction Total bills to get 20 (USD) discount if the total bill is more than 100.
SET 2 (SELECT)
25. Give a query that shows only the transaction ID and the total Bill of all transactions.
26. Give a query that shows only the 1st three rows of Transaction.
27. Give a query to show all transactions after April 8.
28. Give a query that shows the transaction ID and Customer ID arranged by decreasing TotalBill.
29. Give a query showing all transactions who have a discount more than 10 (USD).
30. Give a query showing all distinct CustomerIDs who ordered in April 2022.
31. Give a query showing all CustomerIDs who live in Manila OR have MemberLevel=2.
32. Give a query showing all distinct CustomerIDs who made a transaction with tax less than 10
(USD).
33. Give a query showing all CustomerIDs whose subtotal is less than 100 and made a transaction
before April 15, 2022.
34. Give all Customer Names that starts with B.
35. Give all Customer Names that ends with a consonant (A,E,I,O,U are vowels, not consonants).
36. Give all Customer Names that has the letter O in its name.
37. Give all Customer Names who lives in a city that has the letter i.
38. Give all products whose Boxprice is between 50 and 100 USD.
39. Arrange all the products by decreasing BoxPrice.
40. Arrange all transactions by increasing Discounts
41. Arrange all customers by City (alphabetically), then by Customer Name (alphabetically).
42. Arrange all customers by City (alphabetically), then by decreasing MemberLevel.
43. Arrange all customers by decreasing MemberLevel, then by city (alphabetically).
44. Arrange all transactions by CustomerID (increasing), then by TotalBill (decreasing).
SET 4 (SELECT with aggregate functions MIN,MAX,SUM,COUNT,AVG and GROUP BY, 2 points)
83. Give a table showing the transaction ID and Total Bill of all transactions which bought lotion.
84. Give the 2 transactions with the largest total bill and performed by a customer with Member
level 2.
85. Give the 3 transactions with the smallest tax and which involves more than 1 item (Hint: Sum of
Quantity for each transactionID).
86. Give the total Tax of all transactions per city, arranged by decreasing Tax.
87. Give the average TotalBill of all transactions done by a customer with Member Level 3, arranged
by increasing CustomerID.
88. Give the average tax of all transactions for each Member Level, arranged by decreasing Member
Level.
89. (HAVING 4+1pt) Give the average Tax paid of all transactions which has more than 1 item in the
transaction, grouped by the number of items.
90. (HAVING 4+1pt) Give the sum of all tax paid in all cities which has TotalBills summing up greater
than 100 (USD), grouped by City, arranged by decreasing sum.
Others (3 points)
91. This database would be better of to have a 5th table (by breaking down one of these tables into
2 smaller tables). What columns will this 5th table have?
ERD practice (all groups can answer these)
2. Construct the schema (table names + attributes only) for the following ERD and then draw lines
that will signify the foreign keys from one table to another. (3 points)