day3_assignments solutions
day3_assignments solutions
you need
to solve the questions only with the concepts that have been discussed so far.
1- write a query to get total sales for each profit group. Profit groups are
defined as
profit < 0 -> Loss
profit < 50 -> Low profit
profit < 100 -> High profit
profit >=100 -> very High profit
select case
when profit< 0 then 'loss'
when profit<50 then 'low profit'
when profit<100 then 'high profit'
else 'very high profit'
end as profit_bucket
,SUM(sales) as total_sales
from orders
group by case
when profit< 0 then 'loss'
when profit<50 then 'low profit'
when profit<100 then 'high profit'
else 'very high profit'
end
2- 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
3- write a query to get total profit, first order date and latest order date for
each category
4- write a query to find sub-categories where average profit is more than the half
of the max profit in that sub-category (validate the output using excel)
select sub_category
from orders
group by sub_category
having avg(profit) > max(profit)/2
write a query to find students who have got same marks in Physics and Chemistry.
7- write a query to find top 5 sub categories in west region by total quantity sold
8- write a query to find total sales for each region and ship mode combination for
orders in year 2020
10- write a query to get category wise sales of orders that were not returned
11- write a query to print dep name and average salary of employees in that dep .
12- write a query to print dep names where none of the emplyees have same salary.
select d.dep_name
from employee e
inner join dept d on e.dept_id=d.dep_id
group by d.dep_name
having count(e.emp_id)=count(distinct e.salary)
13- write a query to print sub categories where we have all 3 kinds of returns
(others,bad quality,wrong items)
select o.sub_category
from orders o
inner join returns r on o.order_id=r.order_id
group by o.sub_category
having count(distinct r.return_reason)=3
14- write a query to find cities where not even a single order was returned.
select city
from orders o
left join returns r on o.order_id=r.order_id
group by city
having count(r.order_id)=0
15- write a query to find top 3 subcategories by sales of returned orders in east
region
16- write a query to print dep name for which there is no employee
select d.dep_id,d.dep_name
from dept d
left join employee e on e.dept_id=d.dep_id
where e.dept_id is null
17- write a query to print employees name for which dep id is not present in dept
table
select e.*
from employee e
left join dept d on e.dept_id=d.dep_id
where d.dep_id is null;
18- write a query to print 3 columns : category, total_sales and (total sales of
returned orders)