0% found this document useful (0 votes)
237 views5 pages

SQL Practice Questions

This document provides answers to practice questions related to SQL. It includes questions about identifying highest counts by category from a table, converting tables between normal forms, writing queries to select and group data from multiple tables, and using window functions to calculate metrics and ranks across data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
237 views5 pages

SQL Practice Questions

This document provides answers to practice questions related to SQL. It includes questions about identifying highest counts by category from a table, converting tables between normal forms, writing queries to select and group data from multiple tables, and using window functions to calculate metrics and ranks across data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

SQL Practice Questions ANSWER

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

UnitID StudentI Date TutorID Topic Room Grade Book TutEmail


D
U1 St1 23.02.03 Tut1 GMT 629 4.7 Deumlich Tut1@fhbb.ch
U2 St1 18.11.02 Tut3 Gin 631 5.1 Zehnder Tut3@fhbb.ch
U1 St4 23.02.03 Tut1 GMT 629 4.3 Deumlich Tut1@fhbb.ch
U5 St2 05.05.03 Tut3 PhF 632 4.9 Dummlers Tut3@fhbb.ch
U4 St2 04.07.03 Tut5 AVQ 621 5.0 SwissTop Tut5@fhbb.ch
o

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

f. Using the table vn5018r.deliver_date_item_output_format_dec18 calculate the deliver it


percentage as sum(total_deliver_it_units)/sum(total_units). Calculate the percentage for each of
the reporting_level_1 category at month- reporting_level_0- reporting_level_1 level.

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.

END OF Module 1 Practice Questions


Module 2 Practice Questions:
a. Rank the salaries of all the employees within each department using the table below and
then pick the top ranked (highest salaried) employee within each department. Write a single
query for the same and show the output table
employee_id full_name department salary
100 Mary Johns Sales 1000
101 Sean Moldy IT 1500
102 Peter Dugan Sales 2000
103 Lilian Penn Sales 1700
104 Milton Kowarsky IT 1800
105 Mareen Bisset Accounts 1200
106 Airton Graue Accounts 1100
107 John Joe Sales 1100
108 Cherry Quir IT 1600
109 Jijo James Sales 2100
110 Jean Justin Sales 1800
111 Paul Chris IT 1900
112 Samuel Jackson Accounts 1300
113 Jovin Jolly Accounts 1200

b. Calculate the Deliver IT % as ratio of sum of total_deliver_it_units to total_units using the


cdc table vn5018r.deliver_date_item_output_format_dec18. Calculate the percentage at all
levels of product hierarchy (From overall level till reporting_level_4 level) and produce a
single resultant table. Obtain the resultant table using the Union All method as well as
grouping sets method and identify the difference between the 2 methods.
c. Convert the cdc table vn5018r.have_date_item_level_sku_corr_output_format_dec18
which is at asin-date level to asin level by picking only the latest instance of each asin
based on the date. Implement in a single query through CTE and window function. Window
Function can be used to rank all the date instances of each ASIN. Check the level of the
original source table and resultant ASIN level table and verify.
d. From the below yearly employee sales table, obtain the total sales for each year without
rolling up the table to year level and by using window functions. Write down the resultant
output table.
year sales_employee sale_amount
2016 John 350
2016 David 425
2017 Melwin 225
2017 George 570
2017 Jack 325
2018 James 260
2018 Jill 780
e. From the Month – ASIN level cdc table
vn5018r.P1M_Amz_Top_100K_All_Exclusions_consolidated obtain the monthly
reporting_level_1 (L1) hierarchy level % distribution of ASINs by writing a single query using
CTEs. The % distribution of an L1 is the number of ASINs mapped under an L1 for a given
month divided by the total number of ASINs present in that month.

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

END OF Module 2 Practice Questions

You might also like