Sei sulla pagina 1di 13

Table

of Contents
Introduction 1.1
SQL Interview 1.2
1. Interview #1 1.2.1
2. Interview #2 1.2.2
SQL Real World 1.3
1. School Attendance Pivot 1.3.1
2. School Absent 10 contiguous days 1.3.2
3. Inventory increamental load dimension and fact 1.3.3
Common Use 1.4
1. 1.4.1

1
Introduction

SQL Notes

Interview Questions, Real work Problems, and Random Queries


Currently most sql scripts will be written in TSQL (SQL Server).

2
1. Interview #1

Customer Order History Analysis

We have a set of customer order data and have been asked to provide an analysis of that
data. Before we produce this analysis we need to clean the data based on certain
qualifications specified below.

To create the analysis you may run multiple standard SQL statements, generate temporary
tables, and add tables.

Data Cleanup

ORDER data may have quality issues that are identifiable by the AMOUNT of the order. For
each DEPARTMENT a MIN and MAX amount is defined in DEPARTMENT_RANGE.
ORDER data that is outside of the specified range for the department should be ignored. For
example, an order of a book that costs $2500 would be ignored.

Profile (Per CUSTOMER qualification -> category)

Create a profile of customers based on order history with each customer listed in only one
category. The category ordering is defined below. This analysis will be run weekly.

Frequent Moderate Spender


If there are two or more “Books” orders with AMOUNTs greater than 100
Varied Spender

3
1. Interview #1

If the difference of any ORDER AMOUNTs is greater than 100


High Spender
If the most recent “Books” ORDER AMOUNT is greater than 200
Reader
If the most frequent order DEPARTMENT is “Books”

Create results with a customer only in one category giving priority to

1. Frequent Moderate Spender


2. Varied Spender
3. High Spender
4. Reader
5. NONE

Further Data Cleanup After deploying the profile we realize that there are rows that
represent the same order but from different sources. ORDER data comes from multiple
sources. An ORDER from one data source may also exist in another data source. To de-
duplicate this data we will use the combination of timestamp, customer, and product as a
unique value. Some data sources have a higher quality than others. When identifying which
row to use between two rows with the same timestamp/customer/product we should
prioritize those from a higher quality source. Quality is currently defined in the order:

1. WEBSITE

4
1. Interview #1

2. PHONE
3. POST

Download Solution

5
2. Interview #2

6
1. School Attendance Pivot

Objective:
Create a report, one school per row.

Input

Output

...
...
...

Note:
One Student might have multiple school(student 12082 has 2 schools), we will use the most
recent school the student attended

7
1. School Attendance Pivot

Droll Days: Total school days


Absent Days: Total absent days
Present Days: Total school days - Total absent days
Std_Number: Unique Identifier of a Student

Download Solution

8
2. School Absent 10 contiguous days

Objective:
find students missed school 10 days straight

Input

Question: Get all Students who absent 10 contiguous days with 'No Reason'

9
2. School Absent 10 contiguous days

Output

Download Solution

10
3. Inventory increamental load dimension and fact

Firm

Product

Style

Segment

Discipline

Given
ModelID 'DTCHONEY', (First 3 letter of ModelID: FirmID; Rest: StyleID)
FirmName 'DTC International',
SegmentID 'INTL'

we need use above given three to incremental load Firm,Style,Displine tables,


Product and Segment tables are reference tables doesn't require to insert.

11
3. Inventory increamental load dimension and fact

SegmentID & Product mapping:


INTL (I): Product_key are 31, 34, 36, 40, 41
Equity (E): Product_key are 31, 32, 33, 34, 35, 36, 37, 38
FixedIncome (F): Product_key are 31, 33, 34, 35, 39, 40, 41, 42

insert into the fact table ProductDisciplineSet_Key with corresponding products,


for example, if the segmentID is INTL, then we will have products 31, 34, 36, 40, 41, and
each product will be a row in fact table

the exact output suppose to be


Firm (inserted last row)

Style (inserted last row)

Discipline (inserted last row)

ProductDisciplineSet_Key(poplulated the whole table)

Download Solution
12
3. Inventory increamental load dimension and fact

13

Potrebbero piacerti anche