β¨ Programming
Easy
SQL Joins
Orders Per City
Question
Count the number of orders placed by customers in each city. Return city and order count, sorted by most orders first.
Use customers and orders tables.
Solution
SELECT
c.city,
COUNT(o.order_id) AS order_count
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.city
ORDER BY order_count DESC;
Explanation: JOIN customers to orders, GROUP BY city, COUNT orders per city.