Amazon SQL Test
Amazon SQL Test
Desired output
PRODUCT_ID COUNT
P1 3
P2 2
P3 2
Answer:
select PRODUCT_ID,count(1) from orders
group by PRODUCT_ID
having count(1)>1
Question 2 / 10
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
FROM tblProduct AS T2
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 (
GROUP BY T1.Product_Id,T1.Order_Day
),
FROM CTE1
GROUP BY Order_Day)
JOIN CTE2 AS T2 ON T1.Order_Day = T2.Order_Day AND T1.SumByDay = T2.SoldAmount ORDER BY T2.SoldAmount DESC
Question 4 / 10
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
(
WITH CTE1 AS (
FROM tblProduct AS T1
GROUP BY T1.Product_Id,T1.Order_Day
)
SELECT [Product_id]
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
PIVOT
SUM(Sale)
) AS PivotTable
Question 5 / 10
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:
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:
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
FROM orders as s
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
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
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
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
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
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
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.
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 |
employee(eid,ename,salary)
monorail(rid,rname,running_range)
route_info(route_no,origin,destination,distance,cost)
assignment(eid,rid)
TABLE STRUCTURES:
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');
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:
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
……….
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
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
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: