SQL
Sales :
order_id
product_id
order_date
total_amount
sales_person_id
---------------------------------
Products :
product_id
product_name
product_category
---------------------------------
Sales_Person :
person_id
manager_id
assigned_region
---------------------------------
1. find total no of products sold for each category
select
product_category, count(product_id) as count_products
from
products p
inner join
sales s on p.product_id = s.product_id
group by
product_category
2. asked to show also the products not sold - changed inner to left join
3. then asked to convert null values in count_products to zero
(Coalesce function)
4. (case)
--------------------------------
total sales for the past six days
with six_days as(
select
dense_rank() over(order by order_date desc)
from
sales
where order_date <=6
)
select
sum(total_amount)
from
six_days
(date_functions) to get
--------------------------------------------------------
union vs union all
groupby vs having