SELECT
    p.name AS product_name,
    SUM(o.quantity) AS total_sales,
    RANK() OVER (PARTITION BY p.category ORDER BY SUM(o.quantity) DESC, p.product_id ASC) AS category_rank
FROM
    products p
    LEFT JOIN orders o ON p.product_id = o.product_id 
GROUP BY
    p.product_id, p.name, p.category
HAVING
    SUM(o.quantity) IS NOT NULL
ORDER BY
    p.category ASC,
    total_sales DESC,
    p.product_id ASC;