Workout Difficulty:
Questions:
- Which customer has made the most orders?
- What’s the total revenue per product?
- Find the day with the highest revenue.
Complete the workout here.
Simply post your code and a screenshot of your results.
Please format your Python code and blur it or place it in a hidden section.
This workout will be released on Monday May 29, 2023, and the author’s solution will be posted on Sunday June 4, 2023.
1 Like
Which customer has made the most orders?
select a.customer_id,COUNT(*) as No_of_orders,concat(b.first_name,' ',b.last_name) as Customer_name from orders a
inner join customers b on
a.customer_id=b.customer_id
group by a.customer_id,b.customer_id,b.first_name,b.last_name
What’s the total revenue per product?
with cte as (select P.*, O.order_id,O.quantity, price*quantity as Total from products P inner join order_items O
on P.product_id=O.product_id)
select product_id,product_name,SUM(Total) as Total_revenue_per_product
from cte
group by product_id,product_name
order by Total_revenue_per_product desc;
Find the day with the highest revenue.
with cte as (
select P.*, OI.order_id,OI.quantity, O.order_date, price*quantity as Total from products P
inner join order_items OI
on P.product_id=OI.product_id
inner join orders O
on O.order_id=OI.order_id)
select order_date, SUM(Total) as Highest_revnue_per_day
from cte
group by order_date
order by Highest_revnue_per_day desc;