day5_assignments_solutions
day5_assignments_solutions
with no_of_orders_each_customer as (
select customer_id,count(distinct order_id) as no_of_orders
from orders
group by customer_id)
select * from
no_of_orders_each_customer where no_of_orders > (select avg(no_of_orders) from
no_of_orders_each_customer)
2- write a query to find employees whose salary is more than average salary of
employees in their department
3- write a query to find employees whose age is more than average age of all the
employees.
4- write a query to print emp name, salary and dep id of highest salaried employee
in each department
5- write a query to print emp name, salary and dep id of highest salaried employee
overall
6- write a query to print product id and total sales of highest selling products
(by no of units sold) in each category
with product_quantity as (
select category,product_id,sum(quantity) as total_quantity
from orders
group by category,product_id)
,cat_max_quantity as (
select category,max(total_quantity) as max_quantity from product_quantity
group by category
)
select *
from product_quantity pq
inner join cat_max_quantity cmq on pq.category=cmq.category
where pq.total_quantity = cmq.max_quantity
with all_teams as
(select team_1 as team, case when team_1=winner then 1 else 0 end as win_flag from
icc_world_cup
union all
select team_2 as team, case when team_2=winner then 1 else 0 end as win_flag from
icc_world_cup)
select team,count(1) as total_matches_played , sum(win_flag) as
matches_won,count(1)-sum(win_flag) as matches_lost
from all_teams
group by team
Please solve these problems. You have option to solve them in MySQL, sql server and
postgres. All are free questions.
1- https://www.namastesql.com/coding-problem/38-product-reviews
2- https://www.namastesql.com/coding-problem/61-category-sales-part-1
3- https://www.namastesql.com/coding-problem/62-category-sales-part-2
4- https://www.namastesql.com/coding-problem/71-department-average-salary
5- https://www.namastesql.com/coding-problem/72-product-sales
6- https://www.namastesql.com/coding-problem/73-category-product-count
7- https://www.namastesql.com/coding-problem/103-employee-mentor
8- https://www.namastesql.com/coding-problem/8-library-borrowing-habits
9- https://www.namastesql.com/coding-problem/52-loan-repayment
10- https://www.namastesql.com/coding-problem/55-lowest-price