day4_assignments solutions
day4_assignments solutions
1- write a query to print emp name , their manager name and diffrence in their age
(in days)
for employees whose year of birth is before their managers year of birth
2- write a query to find subcategories who never had any return orders in the month
of november (irrespective of years)
select sub_category
from orders o
left join returns r on o.order_id=r.order_id
where DATEPART(month,order_date)=11
group by sub_category
having count(r.order_id)=0;
3- write a query to print manager names along with the comma separated list(order
by emp salary) of all employees directly reporting to him.
4- write a query to get number of business days between order_date and ship_date
(exclude weekends).
Assume that all order date and ship date are on weekdays only.
select order_id,order_date,ship_date
,datediff(day,order_date,ship_date)-2*datediff(week,order_date,ship_date) as
no_of_business_days
from orders
6- write a query print top 5 cities in west region by average no of days between
order date and ship date.
select top 5 city, avg(datediff(day,order_date,ship_date) ) as avg_days
from orders
where region='West'
group by city
order by avg_days desc
7- write a query to print emp name, manager name and senior manager name (senior
manager is manager's manager)
8- write a query to print first name and last name of a customer using orders
table(everything after first space can be considered as last name)
customer_name, first_name,last_name
write a query to print below output using drivers table. Profit rides are the no of
rides where end location and end_time of a ride is same as start location ans start
time of the next ride for a driver
id, total_rides , profit_rides
dri_1,5,1
dri_2,2,0
10- write a query to print customer name and no of occurence of character 'n' in
the customer name.
customer_name , count_of_occurence_of_n
select
'category' as hierarchy_type,category as hierarchy_name
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by category
union all
select
'sub_category',sub_category
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by sub_category
union all
select
'ship_mode ',ship_mode
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by ship_mode
12- the first 2 characters of order_id represents the country of order placed .
write a query to print total no of orders placed in each country
(an order can have 2 rows in the data when more than 1 item was purchased in the
order but it should be considered as 1 order)