Customer Placing the Largest Number of Orders
Problem
Orders(order_number, customer_number). Return the customer_number that placed the most orders.
o=[(1,1),(2,2),(3,3),(4,3)]3SELECT customer_number
FROM Orders
GROUP BY customer_number
ORDER BY COUNT(*) DESC
LIMIT 1;
Explanation
We want the customer who placed the most orders. The idea is to count orders per customer, sort those counts from highest to lowest, and grab the very top one.
GROUP BY customer_number bundles all rows belonging to each customer together. Then ORDER BY COUNT(*) DESC sorts the customers so the one with the most orders comes first.
LIMIT 1 returns just that single top customer. We don't even need to put COUNT(*) in the SELECT — it is only used for ordering, while the result is the customer_number itself.
Example: orders belong to customers 1, 2, 3, 3. The counts are 1→1, 2→1, 3→2. Sorting by count descending puts customer 3 (two orders) on top, so LIMIT 1 returns 3.