SELECT P.PRODUCT_CODE, SUM(P.PRICE * O.SALES_AMOUNT) AS SALES FROM PRODUCT P JOIN OFFLINE_SALE O ON P.PRODUCT_ID = O.PRODUCT_ID GROUP BY P.PRODUCT_CODE ORDER BY SUM(P.PRICE * O.SALES_AMOUNT) DESC, PRODUCT_CODE;