My issue
/* Write a query to perform the following
- Join the tables - ‘product_catalog’ and division_product_sales’
- Output product_id, selling_price, units_sold and month from the joined table
- Remember the condition - we need to identify the product_id which is sold the most in each month
- Order by the units_sold in descending order */
My code
/* Write a query to perform the following
- Join the tables - 'product_catalog' and division_product_sales'
- Output product_id, selling_price, units_sold and month from the joined table
- Remember the condition - we need to identify the product_id which is sold the most in each month
- Order by the units_sold in descending order */
SELECT
dps.product_id,
pc.selling_price,
dps.units_sold AS 'max_sold',
dps.month
FROM
division_product_sales dps
JOIN
product_catalog pc ON dps.product_id = pc.product_id
WHERE
(dps.units_sold, pc.selling_price) IN (
SELECT
units_sold,
selling_price
FROM
division_product_sales
GROUP BY
month
)
ORDER BY
dps.units_sold DESC, pc.selling_price DESC;
Learning course: Learn Data Analytics using SQL and Python
Problem Link: CodeChef: Practical coding for everyone