Find Customers by Department
MediumPremium
You are given the following tables:
orders table:
departments table:
customers table:
Find how many customers ordered from the electronics and the fashion department respectively in 2022. Your output should have the following columns: customers, department_name
SELECT
COUNT(DISTINCT o.customer_id) AS customers,
d.department_name
FROM departments d
JOIN orders o ON d.department_id = o.department_id
WHERE strftime('%Y', o.order_date) = '2022'
AND d.department_name IN ('Electronics', 'Fashion')
GROUP BY d.department_name;
SELECT COUNT(DISTINCT o.customer_id) AS customers, d.department_name FROM orders o INNER JOIN departments d ON d.department_id = o.department_id WHERE d.department_name IN ('Electronics','Fashion') AND o.order_date BETWEEN '2022-01-01' AND '2022-12-31' GROUP BY d.department_name;