Day6 Assignment Solution
Day6 Assignment Solution
Day6 Assignment Solution
you need
to solve the questions only with the concepts that have been discussed so far.
Run the following command to add and update dob column in employee table
alter table employee add dob date;
update employee set dob = dateadd(year,-1*emp_age,getdate())
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- orders table can have multiple rows for a particular order_id when customers
buys more than 1 product in an order.
write a query to find order ids where there is only 1 product bought by the
customer.
select order_id
from orders
group by order_id
having count(1)=1
4- write a query to print manager names along with the comma separated list(order
by emp salary) of all employees directly reporting to him.
select e2.emp_name as manager_name , string_agg(e1.emp_name,',') as emp_list
from employee e1
inner join employee e2 on e1.manager_id=e2.emp_id
group by e2.emp_name
5- 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
8- 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
9- write a query to print emp name, manager name and senior manager name (senior
manager is manager's manager)