SQL TEST
transactions
Id customer_id order_id transaction_date status vendor
1 422818 TEST000001 2018-01-01 00:00:10 SHIPPED Vendor A
2 181820 TEST000002 2018-01-01 00:10:10 SHIPPED Vendor A
3 999019 TEST000003 2018-01-02 03:18:01 CANCELLED Vendor A
4 1923192 TEST000004 2018-02-04 05:00:00 CANCELLED Vendor C
5 645532 TEST000005 2018-02-10 16:00:10 SHIPPED Vendor C
6 1101011 TEST000006 2018-02-11 11:00:11 SHIPPED Vendor C
7 1020000 TEST000007 2018-02-10 00:00:00 SHIPPED Vendor D
8 40111234 TEST000008 2018-03-11 06:30:11 SHIPPED Vendor D
9 1923192 TEST000009 2018-03-12 10:00:11 CANCELLED Vendor B
10 1101011 TEST000010 2018-03-12 15:30:12 SHIPPED Vendor B
11 999019 TEST000011 2018-03-15 12:30:45 CANCELLED Vendor A
12 645532 TEST000012 2018-04-01 09:30:22 SHIPPED Vendor A
13 650013 TEST000013 2018-04-01 10:50:37 SHIPPED Vendor C
14 777734 TEST000014 2018-04-02 13:45:19 SHIPPED Vendor D
PART 1
From the table above, write the SQL query to (preferably using MySQL syntax, but you can also
use SQL Server, Redshift SQL or ANSI standard syntax):
1. Show list of transactions occurring in February 2018 with SHIPPED status.
2. Show list of transactions occurring from midnight to 9 AM
3. Show a list of only the last transactions from each vendor
4. Show a list of only the second last transactions from each vendor
5. Count the transactions from each vendor with the status CANCELLED per day
6. Show a list of customers who made more than 1 SHIPPED purchases
7. Show the total transactions (volume) and category of each vendors by following these criteria:
a. Superb: More than 2 SHIPPED and 0 CANCELLED transactions
b. Good: More than 2 SHIPPED and 1 or more CANCELLED transactions
c. Normal: other than Superb and Good criteria
Order the vendors by the best category (Superb, Good, Normal), then by the biggest
transaction volume
Vendor Category Total Transaction
Vendor D Superb 3
... ... ...
8. Group the transactions by hour of transaction_date
Hour of the Day Total Transaction
00 3
03 1
05 1
... ...
9. Group the transactions by day and statuses as the example below
Date SHIPPED CANCELLED PROCESSING
2018-01-01 2 0 0
2018-01-02 0 1 0
2018-02-04 0 1 0
... ...
10. Calculate the average, minimum and maximum of days interval of each transaction (how
many days from one transaction to the next)
Average Interval Minimum Interval Maximum Interval
... day(s) … day(s) … day(s)
transaction_details
Id trx_id product_name quantity price
1 1 Beng beng 100 6000
2 1 Taro 80 5500
3 2 Beng Beng 70 6000
4 2 Taro 41 5500
5 2 Indomie Kari Ayam 12 3000
6 2 Indomie Ayam Bawang 20 3100
7 3 Indomie Ayam Bawang 30 3200
8 3 Indomie Kari Ayam 90 3300
9 3 Taro 100 5500
10 4 Beng Beng 40 6000
11 5 Teh Sariwangi Murni 50 8000
12 6 Indomie Kari Ayam 10 3000
13 6 Indomie Ayam Bawang 8 3100
14 6 Teh Sariwangi Murni 80 8000
15 6 Teh Hijau Cap Kepala Djenggot 15 9500
16 7 Coki-coki 70 1000
17 8 Bakmi Mewah 1500 13000
The new table, “transaction_details” contains the details of purchased item of each transaction in
“transactions” table.
PART 2
In reference to both tables, write an SQL query to:
1. Show the sum of the total value of the products shipped along with the Distributor
Commissions (2% of the total product value if total quantity is 100 or less, 4% of the total
product value if total quantity sold is more than 100)
Product Name Value (quantity x price) Distributor Commission
Taro x.000.000 x.000
Beng Beng x.000.000 x.000
… … ...
2. Show total quantity of “Indomie (all variant)” shipped within February 2018
total_quantity
xxx
3. For each product, show the ID of the last transaction which contained that particular product
Product Name Last Transaction ID
Beng beng 4
Coki-Coki 7
… …