0% found this document useful (0 votes)
158 views

Amazon SQL Test

Uploaded by

Tetiana Ivchyk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Amazon SQL Test

Uploaded by

Tetiana Ivchyk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Question 1 / 10

Order Sample Data:


ORDER_DAY ORDER_ID PRODUCT_ID QUANTITY PRICE
------------------ -------------------- ---------- ---------- ----------
01-JUL-11 O1 P1 5 5
01-JUL-11 O2 P2 2 10
01-JUL-11 O3 P3 10 25
01-JUL-11 O4 P1 20 5
02-JUL-11 O5 P3 5 25
02-JUL-11 O6 P4 6 20
02-JUL-11 O7 P1 2 5
02-JUL-11 O8 P5 1 50
02-JUL-11 O9 P6 2 50
02-JUL-11 O10 P2 4 10
Get me all products that got sold both the days and the number of times the product is
sold.

Desired output
PRODUCT_ID COUNT

P1 3
P2 2
P3 2

The following table has been created for you

CREATE TABLE ORDERS


(
ORDER_DAY DATE,
ORDER_ID VARCHAR2 (20),
PRODUCT_ID VARCHAR2 (10),
QUANTITY NUMBER,
PRICE NUMBER
);

Answer:
select PRODUCT_ID,count(1) from orders
group by PRODUCT_ID
having count(1)>1
Question 2 / 10

Order Sample data:

ORDER_DAY ORDER_ID PRODUCT_ID QUANTITY PRICE


------------------ -------------------- ---------- ---------- ----------
01-JUL-11 O1 P1 5 5
01-JUL-11 O2 P2 2 10
01-JUL-11 O3 P3 10 25
01-JUL-11 O4 P1 20 5
02-JUL-11 O5 P3 5 25
02-JUL-11 O6 P4 6 20
02-JUL-11 O7 P1 2 5
02-JUL-11 O8 P5 1 50
02-JUL-11 O9 P6 2 50
02-JUL-11 O10 P2 4 10
Get me products that was ordered on 02-Jul-11 but not on 01-Jul-11
desired output

P4
P5
P6

Answer:
select PRODUCT_ID from orders where ORDER_DAY = '02-JUL-11'
MINUS
select PRODUCT_ID from orders where ORDER_DAY = '01-JUL-11'

SELECT T1.Product_Id

From tblProduct AS T1

WHERE T1.Order_Day = '2011-07-02' AND T1.Product_Id NOT IN (SELECT T2.Product_Id

FROM tblProduct AS T2

WHERE T2.Order_Day = '2011-07-01')


Question 3 / 10

Order Sample data:


ORDER_DAY ORDER_ID PRODUCT_ID QUANTITY PRICE
------------------ -------------------- ---------- ---------- ----------
01-JUL-11 O1 P1 5 5
01-JUL-11 O2 P2 2 10
01-JUL-11 O3 P3 10 25
01-JUL-11 O4 P1 20 5
02-JUL-11 O5 P3 5 25
02-JUL-11 O6 P4 6 20
02-JUL-11 O7 P1 2 5
02-JUL-11 O8 P5 1 50
02-JUL-11 O9 P6 2 50
02-JUL-11 O10 P2 4 10

Get me highest sold Products (Qty* Price) on both days , desired output
DATE PRODUCT_ID SOLD_AMOUNT
01-JUL-11 P3 250
02-JUL-11 P3 125

Answer:
select q1.ORDER_DAY AS DATE1,q1.PRODUCT_ID,q1.QUANTITY*q1.PRICE AS SOLD_AMOUNT from orders q1 ,
(select PRODUCT_ID,sum(QUANTITY*PRICE) high_sold from orders group by PRODUCT_ID) q2,
(select max(high_sold) as max_sold from
(
select PRODUCT_ID,sum(QUANTITY*PRICE) high_sold from orders group by PRODUCT_ID
))
q3
where q2.high_sold=q3.max_sold and q1.PRODUCT_ID=q2.PRODUCT_ID

WITH CTE1 AS (

SELECT T1.Product_Id,T1.Order_Day, SUM(T1.Quantity * T1.Price) AS SumByDay FROM tblProduct AS T1

GROUP BY T1.Product_Id,T1.Order_Day

),

CTE2 AS (SELECT Order_Day,MAX(sumbyDay) AS SoldAmount

FROM CTE1

GROUP BY Order_Day)

SELECT T2.Order_Day,T1.Product_Id,T2.SoldAmount FROM CTE1 AS T1

JOIN CTE2 AS T2 ON T1.Order_Day = T2.Order_Day AND T1.SumByDay = T2.SoldAmount ORDER BY T2.SoldAmount DESC
Question 4 / 10

Order Sample data:


ORDER_DAY ORDER_ID PRODUCT_ID QUANTITY PRICE
------------------ -------------------- ---------- ---------- ----------
01-JUL-11 O1 P1 5 5
01-JUL-11 O2 P2 2 10
01-JUL-11 O3 P3 10 25
01-JUL-11 O4 P1 20 5
02-JUL-11 O5 P3 5 25
02-JUL-11 O6 P4 6 20
02-JUL-11 O7 P1 2 5
02-JUL-11 O8 P5 1 50
02-JUL-11 O9 P6 2 50
02-JUL-11 O10 P2 4 10
Get me for all products, total sales on 01-JUL-11 and 02-JUL-11 adjacent to each other

Desired Output:
P1 125 10
P2 20 40
P3 250 125
P4 0 120
P5 0 50
P6 0 100

Answer:
select PRODUCT_ID,nvl(max(decode(ORDER_DAY,'01-JUL-11',total_sales)),0) AS JULY1st_sales ,
nvl(max(decode(ORDER_DAY,'02-JUL-11',total_sales)),0) AS JULY2nd_sales
from
(

select PRODUCT_ID,ORDER_DAY,sum(QUANTITY*PRICE) total_sales


from orders group by PRODUCT_ID,ORDER_DAY
)X
group by PRODUCT_ID
order by PRODUCT_ID

WITH CTE1 AS (

SELECT T1.Product_Id,T1.Order_Day, SUM(T1.Quantity * T1.Price) AS SumByDay

FROM tblProduct AS T1

GROUP BY T1.Product_Id,T1.Order_Day

)
SELECT [Product_id]

,SUM(IIF([Order_Day]= '2011-07-01', SumByDay , 0))

,SUM(IIF([Order_Day]= '2011-07-02', SumByDay , 0))

FROM CTE1

GROUP BY [Product_id];

--Q4 (This is additional answer to Q4 just to show you know how to use PIVOT )

SELECT

Product_ID

, ISNULL([2011-07-01], 0) AS Day1Sale

, ISNULL([2011-07-02], 0) AS Day2Sale

FROM

(SELECT Product_Id, Order_Day, Quantity * Price AS Sale FROM tblProduct) AS SourceTable

PIVOT

SUM(Sale)

FOR Order_Day IN ([2011-07-01], [2011-07-02])

) AS PivotTable
Question 5 / 10

Order Sample data:

ORDER_DAY ORDER_ID PRODUCT_ID QUANTITY PRICE


------------------ -------------------- ---------- ---------- ----------
01-JUL-11 O1 P1 5 5
01-JUL-11 O2 P2 2 10
01-JUL-11 O3 P3 10 25
01-JUL-11 O4 P1 20 5
02-JUL-11 O5 P3 5 25
02-JUL-11 O6 P4 6 20
02-JUL-11 O7 P1 2 5
02-JUL-11 O8 P5 1 50
02-JUL-11 O9 P6 2 50
02-JUL-11 O10 P2 4 10
Get me all products daywise, that was ordered more than once
Desired output

01-JUL-11 P1

Answer:
select PRODUCT_ID,ORDER_DAY,count(1)
from orders group by PRODUCT_ID,ORDER_DAY
having count(1)>1
Question 6 / 10

Order Table:

Order Id Item Qty

O1 A1 5

O2 A2 1

O3 A3 3

Please provide SQL which will explode the above data into single unit level records as
shown below
Desired Output:

Order Id Item Qty

O1 A1 1

O1 A1 1

O1 A1 1

O1 A1 1

O1 A1 1

O2 A2 1

O3 A3 1

O3 A3 1

O3 A3 1

The following table has been created for you

CREATE TABLE SIMPLE_ORDERS(


ORDER_ID VARCHAR2(40)
, ITEM VARCHAR2(10)
, QUANTITY NUMBER
);
Answer:
with counter(val) as
(select 1 from dual union
select 2 from dual union
select 3 from dual union
select 4 from dual union
select 5 from dual )

select S.ORDER_ID,S.ITEM, 1 from SIMPLE_ORDERS S ,


counter O
where
O.val<=S.QUANTITY
SELECT "Forecast Month",
"Publish Date",
"FinancialPlanType",
"Subaccount",
ITN,
"Project ID",
"Resource Type",
"Details",
UNIT,
ROUND(VALS/12,2),
x.lvl,
"YR"
FROM STG_DFF_B
CROSS JOIN (SELECT LEVEL AS lvl -- Joins in 1 through 12.
FROM dual
CONNECT BY LEVEL <= 12) x
WHERE MONTHNUMBER is null;

SELECT s.Item, s.OrderID, 1 AS QTY, s.qty/s.QTY AS VALUE

FROM orders as s

INNER JOIN master.dbo.spt_values t ON t.type='P'

AND t.number BETWEEN 1 AND s.QTY


Question 7 / 10

Product
| PRODUCT_ID | PRODUCT_GROUP | PRODUCT_NAME |
| P1 | Book | Harry Potter 1 |
| P2 | Book | Harry Potter 2 |
| P3 | Electronics | Nikon 10 MPS |
| P4 | Electronics | Cannon 8 MPS |
| P5 | Electronics | Cannon 10 MPS |
| P6 | Video DVD | Pirates 1 |
| P7 | Video DVD | Pirates 2 |
| P8 | Video DVD | HP 1 |
| P9 | Video DVD | HP 2 |
| P10 | Shoes | Nike 10 |
| P11 | Shoes | Nike 11 |
| P12 | Shoes | Adidas 10 |
| P13 | Shoes | Adidas 09 |
| P14 | Book | God Father 1 |
| P15 | Book | God Father 2 |

Sales_fact

SNAPSHOT_DAY PRODUCT_ID SALES_AMT

20-JUL-11 P1 10
20-JUL-11 P2 5
20-JUL-11 P8 100
20-JUL-11 P3 5
20-JUL-11 P4 25
20-JUL-11 P5 15
20-JUL-11 P6 35
20-JUL-11 P7 5
20-JUL-11 P9 30
20-JUL-11 P10 8
20-JUL-11 P11 45

Glance_view_fact

SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS

20-JUL-11 P1 1000
20-JUL-11 P2 800
20-JUL-11 P8 700
20-JUL-11 P3 800
20-JUL-11 P4 500
20-JUL-11 P5 250
20-JUL-11 P6 10
20-JUL-11 P7 1000
20-JUL-11 P9 1500
20-JUL-11 P10 600
20-JUL-11 P12 670
20-JUL-11 P13 300
20-JUL-11 P14 230

Inventory_fact

SNAPSHOT_DAY PRODUCT_ID ON_HAND_QUANTITY

20-JUL-11 P1 100
20-JUL-11 P2 70
20-JUL-11 P8 90
20-JUL-11 P3 10
20-JUL-11 P4 30
20-JUL-11 P5 100
20-JUL-11 P6 120
20-JUL-11 P7 70
20-JUL-11 P9 90

Ad_spend_fact

SNAPSHOT_DAY PRODUCT_ID AD_SPENT

20-JUL-11 P1 10
20-JUL-11 P2 5
20-JUL-11 P8 100
20-JUL-11 P3 5
20-JUL-11 P4 25
20-JUL-11 P5 15
20-JUL-11 P6 35
20-JUL-11 P7 5
20-JUL-11 P9 30
20-JUL-11 P10 8
20-JUL-11 P11 45
Write a SQL that will give the top product by sales in each of the product groups and
additionally gather GV, Inventory, and ad spend measures also for the product.

Output Required:
Book P1 10 1000 100 0
Electronics P4 25 500 30 30
Shoes P11 45 0 0 0
Video DVD P8 100 700 90 0

Answer:
select * from
(
select P.PRODUCT_ID,P.PRODUCT_GROUP,S.SALES_AMT , rank() over(partition by P.PRODUCT_GROUP ORDER BY S.SA
LES_AMT DESC) AS RNK ,
nvl(gv.GLANCE_VIEWS,0),nvl(i.ON_HAND_QUANTITY,0) ,nvl(a.AD_SPENT,0)
from Product P inner join Sales_fact S on P.PRODUCT_ID=S.PRODUCT_ID
LEFT OUTER JOIN Glance_view_fact gv on P.PRODUCT_ID=gv.PRODUCT_ID
LEFT OUTER JOIN Inventory_fact i on P.PRODUCT_ID=i.PRODUCT_ID
LEFT OUTER JOIN Ad_spend_fact a on P.PRODUCT_ID=a.PRODUCT_ID
) XX WHERE RNK=1
Question 8 / 10

Product
+------------+---------------+----------------+
| PRODUCT_ID | PRODUCT_GROUP | PRODUCT_NAME |
+------------+---------------+----------------+
| P1 | Book | Harry Potter 1 |
| P2 | Book | Harry Potter 2 |
| P3 | Electronics | Nikon 10 MPS |
| P4 | Electronics | Cannon 8 MPS |
| P5 | Electronics | Cannon 10 MPS |
| P6 | Video DVD | Pirates 1 |
| P7 | Video DVD | Pirates 2 |
| P8 | Video DVD | HP 1 |
| P9 | Video DVD | HP 2 |
| P10 | Shoes | Nike 10 |
| P11 | Shoes | Nike 11 |
| P12 | Shoes | Adidas 10 |
| P13 | Shoes | Adidas 09 |
| P14 | Book | God Father 1 |
| P15 | Book | God Father 2 |
+------------+---------------+----------------+

Sales_fact

SNAPSHOT_DAY PRODUCT_ID SALES_AMT


------------------ ---------- ----------
20-JUL-11 P1 10
20-JUL-11 P2 5
20-JUL-11 P8 100
20-JUL-11 P3 5
20-JUL-11 P4 25
20-JUL-11 P5 15
20-JUL-11 P6 35
20-JUL-11 P7 5
20-JUL-11 P9 30
20-JUL-11 P10 8
20-JUL-11 P11 45
Glance_view_fact

SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS


------------------ ---------- ------------
20-JUL-11 P1 1000
20-JUL-11 P2 800
20-JUL-11 P8 700
20-JUL-11 P3 800
20-JUL-11 P4 500
20-JUL-11 P5 250
20-JUL-11 P6 10
20-JUL-11 P7 1000
20-JUL-11 P9 1500
20-JUL-11 P10 600
20-JUL-11 P12 670
20-JUL-11 P13 300
20-JUL-11 P14 230

Write a SQL to give all Products that have glance views but no sales.

Output Required:
P12
P13
P14

Answer:
select PRODUCT_ID from Glance_view_fact where PRODUCT_ID not in (
select PRODUCT_ID from Sales_fact)
Question 9 / 10

Product
+------------+---------------+----------------+
| PRODUCT_ID | PRODUCT_GROUP | PRODUCT_NAME |
+------------+---------------+----------------+
| P1 | Book | Harry Potter 1 |
| P2 | Book | Harry Potter 2 |
| P3 | Electronics | Nikon 10 MPS |
| P4 | Electronics | Cannon 8 MPS |
| P5 | Electronics | Cannon 10 MPS |
| P6 | Video DVD | Pirates 1 |
| P7 | Video DVD | Pirates 2 |
| P8 | Video DVD | HP 1 |
| P9 | Video DVD | HP 2 |
| P10 | Shoes | Nike 10 |
| P11 | Shoes | Nike 11 |
| P12 | Shoes | Adidas 10 |
| P13 | Shoes | Adidas 09 |
| P14 | Book | God Father 1 |
| P15 | Book | God Father 2 |
+------------+---------------+----------------+

Sales_fact

SNAPSHOT_DAY PRODUCT_ID SALES_AMT


------------------ ---------- ----------
20-JUL-11 P1 10
20-JUL-11 P2 5
20-JUL-11 P8 100
20-JUL-11 P3 5
20-JUL-11 P4 25
20-JUL-11 P5 15
20-JUL-11 P6 35
20-JUL-11 P7 5
20-JUL-11 P9 30
20-JUL-11 P10 8
20-JUL-11 P11 45
Write a SQL to give the sales of Electronics as a Percentage of Books
Output Required: 300

Answer:
SELECT E_SALES/B_SALES*100 FROM
(
select P.PRODUCT_GROUP,sum(S.SALES_AMT) AS E_SALES
from Product P inner join Sales_fact S on P.PRODUCT_ID=S.PRODUCT_ID
where P.PRODUCT_GROUP = 'Electronics'
group by P.PRODUCT_GROUP
) E,
(select P.PRODUCT_GROUP,sum(S.SALES_AMT) AS B_SALES
from Product P inner join Sales_fact S on P.PRODUCT_ID=S.PRODUCT_ID
where P.PRODUCT_GROUP = 'Book'
group by P.PRODUCT_GROUP
)B
where 1=1
Question 10 / 10

See a Phone Log table as below. It records all phone numbers that we dial in a given
day.

Table name is PHONE_LOG

SOURCE_PHONE_NUMBER DESTINATION_PHONE_NUMBER CALL_START_DATETIME


------------------- ------------------------ ----------------
1234 4567 01/07/2011 10:00
1234 2345 01/07/2011 11:00
1234 3456 01/07/2011 12:00
1234 3456 01/07/2011 13:00
1234 4567 01/07/2011 15:00
1222 7890 01/07/2011 10:00
1222 7680 01/07/2011 12:00
1222 2345 01/07/2011 13:00
Please provide an SQL query to display the source_phone_number and a flag where the
flag needs to be set to Y if first called number and last called number are the same and
N if the first called number and last called number are different.
For the above input data, desired output should be
Source Number Is_Match
1222 N
1234 Y
The following table is created for you

CREATE TABLE PHONE_LOG


(SOURCE_PHONE_NUMBER NUMBER
, DESTINATION_PHONE_NUMBER NUMBER
, CALL_START_DATETIME DATE
);
Answer:
SELECT DISTINCT
SOURCE_PHONE_NUMBER,
NVL2 (
NULLIF (
(SELECT DESTINATION_PHONE_NUMBER
FROM phone_log a
WHERE a.SOURCE_PHONE_NUMBER = pl.SOURCE_PHONE_NUMBER
AND a.CALL_START_DATETIME =
(SELECT MAX (CALL_START_DATETIME)
FROM phone_log b
WHERE b.SOURCE_PHONE_NUMBER =
pl.SOURCE_PHONE_NUMBER)),
(SELECT DESTINATION_PHONE_NUMBER
FROM phone_log a
WHERE a.SOURCE_PHONE_NUMBER = pl.SOURCE_PHONE_NUMBER
AND a.CALL_START_DATETIME =
(SELECT MIN (CALL_START_DATETIME)
FROM phone_log b
WHERE b.SOURCE_PHONE_NUMBER =
pl.SOURCE_PHONE_NUMBER))),
'N',
'Y')
AS flag
FROM phone_log pl
Below listed table definition with column name and one row of sample data.
1. Table employee contains information of all the employee in the organization (including drivers of
monorail).

| eid | ename | salary |


| 555 | Raheem | 235 |

2. Table monorail Contains the information aboubt each monorail.

| rid | rname | running_range |


| 1234 | Speedster | 454 |

3. Table route_info contains the description of route on which monorails used to


travel.

| route_no | origin | distance | cost |


| 4456 | Gwalior | Delhi | 543 |

4. Table assignment contains the data about which monorail is driven by which
employee. This can be a many-to-many relation.

| eid | rid |
| 101 | 4454 |

NOTE: Make all necessary assumptions.


Table structure repeated below for quick reference:

employee(eid,ename,salary)
monorail(rid,rname,running_range)
route_info(route_no,origin,destination,distance,cost)
assignment(eid,rid)

TABLE STRUCTURES:

create table employee


(
eid int,
ename varchar2(50),
salary int
)
;

create table monorail


(
rid int,
rname varchar2(50),
running_range int
);
create table route_info
(
route_no int,
origin varchar2(50),
destination varchar2(50),
distance int,
cost int
);

create table assignment


(
eid int,
rid int
);

PROBLEM 1: UNDERPAID EMPLOYEE :

Find the names of employees (“ename”) whose salary is less than the price (“cost”) of
cheapest route from ‘Bangalore’ to ‘Delhi’ (No need to consider route with connections).
SOLUTION 1: UNDERPAID EMPLOYEE :

select ename from employee where salary < (select min(cost) from route_info where origin='Bangalore' and destination='
Delhi');

PROBLEM 2: ONE STOP ROUTE:

Find one stop from ‘Bangalore’ to ‘Delhi’. The route will need to make one stop transfer
and don’t need to consider time and cost. Query should return two columns : first
section route_no and second section route_no.
SOLUTION 2: ONE STOP ROUTE:

select a.route_no as first_section_route,b.route_no as sec_section_route


from route_info a,route_info b where a.origin='Bangalore'
and b.destination='Delhi' and a.destination=b.origin

PROBLEM 3: HOW MANY MONORAILS DOES EACH DRIVE:

Provide all employee names (“ename”) along with status on whether they are driving
monorail or not (Y/N) and also the number of monorails driving (0 if they are not driving
monorail).
Note: The format of output should be like below
Raheem N 0
Nusrat Y 3
……….

SOLUTION 3: HOW MANY MONORAILS DOES EACH DRIVE:

select emp.ename, case when xx.cnt is NULL then 'N' else 'Y' end, nvl(xx.cnt,0)
from employee emp left outer join
(
select eid,ename,nvl(count(1),0) as cnt from
(select e.eid,e.ename,a.rid from employee e inner join assignment a
on e.eid=a.eid) x
group by x.eid,x.ename
) xx
on emp.eid=xx.eid

PROBLEM 4: CHEAPEST ROUTE TO DESTINATION:

List all the route destination along with their route_no of the cheapest route to that
destination from any origin. Output should have three column:
destination, route_no and cost of the route.
SOLUTION 4: CHEAPEST ROUTE TO DESTINATION:

Select r.destination, r.route_no,r.cost from route_info r, (select destination,min(cost) as mr_cost from route_info group by
destination) mr
WHERE R.COST = MR_COST AND R.DESTINATION=MR.DESTINATION

PROBLEM 5: TOP PAID DRIVERS:

List all the monorails along with the top 2 paid (salary ranked 1st or 2nd) drivers of each
monorail. The output should have four columns: the name of the monorail (rname), the
salary ranking in the drivers of same monorail, the name of the driver (ename), the
driver’s salary (salary).
SOLUTION 5: TOP PAID DRIVERS:

select m.rname,x.salary_rank, x.ename, x.salary from


(
select a.rid, a.eid,e.ename,e.salary, rank() over (partition by a.rid order by e.salary desc) as salary_rank from assignment a
inner join employee e on a.eid=e.eid
) X , monorail m
where x.rid=m.rid and salary_rank<=2

You might also like