SQL Practice Questions
SQL Practice Questions
KEY
Module 1 Practice Questions:
a. Using the cdc hive month-ASIN level table
vn5018r.P1M_Amz_Top_100K_All_Exclusions_consolidated identify the
reporting_level_4 with the highest number of ASINs over all the months combined (ASIN
is an Amazon Item and the above table is at ASIN level and reporting_level_4 is the
subcategory that contains the ASINs. reporting_level_4 is subcategory,
reporting_level_3 is category, reporting_level_2 is department, reporting_level_1 is
super department and reporting_level_0 is division)
b. Identify the month for which the reporting_level_4 identified above has the highest
number of ASINs
c. Using the same table, for each month, identify the reporting_level_0 that has the highest
number of reporting_level_4’s under it (Refer to Module 2 topics – CTE & Window
Functions to solve this question) and obtain the output table at month-reporting_level_0
level having the reporting_level_0s with the highest number of reporting_level_4s
d. Below is the student-topic level table with students and their grades in different topics.
Convert this 1NF table to 3NF
e. Write a SQL statement to make a list with order no, purchase amount, customer name and their
cities for those orders whose order amount between 500 and 2000. Use the below 2 tables and illustrate
the output table
orders: tableA
Order_no Purch_amt Ord_date Customer_id Salesman_id
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3006 5001
Customers: tableB
Customer_id Cust_name City Grade Salesman_id
3002 Nick Rimando New York 100 5001
3005 Graham Zusi California 200 5002
3001 Brad Guzan London 300 5005
3004 Fabian Johns Paris 300 5006
3007 Brad Davis New York 200 5001
3009 Geoff Camero Berlin 100 5003
3008 Julian Green London 300 5002
3003 Jozy Altidor Moscow 200 5007
g. Write a query in SQL to display those employees who contain a letter ‘z’ to their first name and
display their department and city using the below tables. Also illustrate the output table
Departments: tableA
Department_ID Department_Name Location_ID
10 Administration 1700
20 Marketing 1800
30 Purchasing 1700
40 Human Resources 2400
50 Shipping 1500
60 IT 1400
70 Public Relations 2700
Employees: tableB
Employee_ID First_Name Department_ID
100 Zack 10
101 Zohan 10
102 Jim 20
103 Jill 30
104 Jejo 30
105 Zaakir 40
106 Yacob 50
Locations: tableC
Location_ID City
1700 Venice
1800 Rome
1900 Tokyo
2000 London
2100 New York
2200 Paris
2300 Beijing
h. Convert the item level(ASIN level) table vn5018r.p1m_100k_final_dec18 to date-asin level by using
the dates present in the table vn5018r.evaluation_dates_dec18 such that every asin is present
across every date. Save the result into another table.
i. For all the date – asin combinations (date-asin level table) created in the above table obtain the
instock and publish flags and have_it flag from the date-item level flags table
vn5018r.top_100K_instock_published_dec18 and create a 1/0 flag column called not_in_catelogue
to tag all the rows that do not obtain any flag from the flags table. The join key would be
catlg_item_id & calendar_date.
j. Create the same date-asin level table with flags as created above (question i.) for another new list
using these three, new list tables vn5018r.p1m_100k_final_dec18_unadj (asin level table),
vn5018r.evaluation_dates_dec18_unadj (dates table) and
vn5018r.top_100K_instock_published_dec18_unadj (item-date level flags), using the same join key.
Once created, unify this resultant date-asin level flags table with the above (question i.) date-asin
level flags table with a flag to denote if the row is part of the original list or new list.
f. From the below table obtain the total units sold at month-year-company level, at year-
company level and at company level using grouping sets. On the rolled-up table, rank the
year-company combinations and the companies based on the units sold, with a flag
indicating the level which is ranked. Write the resultant output table.
company_nam year month day units_sold
e
X 2016 January Sunday 4200
X 2016 January Friday 3250
X 2016 January Saturday 2425
X 2016 February Tuesday 1450
X 2016 February Saturday 6300
X 2017 January Thursday 4300
X 2017 January Monday 1350
X 2017 February Wednesday 1000
X 2017 February Sunday 4700
X 2017 February Tuesday 1800
Y 2016 January Sunday 4230
Y 2016 January Friday 3251
Y 2016 January Saturday 2426
Y 2016 February Tuesday 1451
Y 2016 February Saturday 6301
Y 2017 January Thursday 4301
Y 2017 January Monday 1351
Y 2017 February Wednesday 1001
Y 2017 February Sunday 4701
Y 2017 February Tuesday 1801