Professional Documents
Culture Documents
Day10 Assignment Solution
Day10 Assignment Solution
you need
to solve the questions only with the concepts that have been discussed so far.
1- write a query to print 3rd highest salaried employee details for each department
(give preferece to younger employee in case of a tie).
In case a department has less than 3 employees then print the details of highest
salaried employee in that department.
with rnk as (
select *, dense_rank() over(partition by dept_id order by salary desc) as rn
from employee)
,cnt as (select dept_id,count(1) as no_of_emp from employee group by dept_id)
select
rnk.*
from
rnk
inner join cnt on rnk.dept_id=cnt.dept_id
where rn=3 or (no_of_emp<3 and rn=1)
with rnk as (
select *, dense_rank() over(partition by dept_id order by salary desc) as rn
,count(1) over(partition by dept_id ) as no_of_emp
from employee)
select
*
from
rnk
where rn=3 or (no_of_emp<3 and rn=1)
2- write a query to find top 3 and bottom 3 products by sales in each region.
with region_sales as (
select region,product_id,sum(sales) as sales
from orders
group by region,product_id
)
,rnk as (select *, rank() over(partition by region order by sales desc) as drn
, rank() over(partition by region order by sales asc) as arn
from region_sales
)
select region,product_id,sales,case when drn <=3 then 'Top 3' else 'Bottom 3' end
as top_bottom
from rnk
where drn <=3 or arn<=3
3- Among all the sub categories..which sub category had highest month over month
growth by sales in Jan 2020.
with sbc_sales as (
select sub_category,format(order_date,'yyyyMM') as year_month, sum(sales) as sales
from orders
group by sub_category,format(order_date,'yyyyMM')
)
, prev_month_sales as (select *,lag(sales) over(partition by sub_category order by
year_month) as prev_sales
from sbc_sales)
select top 1 * , (sales-prev_sales)/prev_sales as mom_growth
from prev_month_sales
where year_month='202001'
order by mom_growth desc
4- write a query to print top 3 products in each category by year over year sales
growth in year 2020.
with cat_sales as (
select category,product_id,datepart(year,order_date) as order_year, sum(sales) as
sales
from orders
group by category,product_id,datepart(year,order_date)
)
, prev_year_sales as (select *,lag(sales) over(partition by category order by
order_year) as prev_year_sales
from cat_sales)
,rnk as (
select * ,rank() over(partition by category order by
(sales-prev_year_sales)/prev_year_sales desc) as rn
from prev_year_sales
where order_year='2020'
)
select * from rnk where rn<=3
write a query to get start time and end time of each call from above 2 tables.Also
create a column of call duration in minutes. Please do take into account that
there will be multiple calls from one phone number and each entry in start table
has a corresponding entry in end table.
select s.phone_number,s.rn,s.start_time,e.end_time,
datediff(minute,start_time,end_time) as duration
from
(select *,row_number() over(partition by phone_number order by start_time) as rn
from call_start_logs) s
inner join (select *,row_number() over(partition by phone_number order by end_time)
as rn from call_end_logs) e
on s.phone_number = e.phone_number and s.rn=e.rn;
solve below questions. You can write SQLs and verify on the platform itself.
Note : The platform supports only postgreSQL so there may be few diffrences in
functions. Listing down some important diffrences:
6-https://datalemur.com/questions/top-fans-rank
7-https://datalemur.com/questions/sql-highest-grossing
8-https://datalemur.com/questions/top-drugs-sold
9-https://datalemur.com/questions/yoy-growth-rate
10-https://datalemur.com/questions/long-calls-growth