with
t1 as(
    select distinct
        product_id,
        name,
        sum(quantity)over(partition by name order by name) as pcount,
        category
    from
        orders left join products using(product_id)
)
,t2 as(
    select
        product_id,
        name as product_name,
        pcount as total_sales,
        rank()over(partition by category order by pcount desc) as category_rank
    from
        t1
)

select product_name,total_sales,category_rank from t2 where product_name is not null