β¨ Programming
Easy
SQL Aggregates
Order Count & Revenue by Status
Question
For each order status, find the number of orders and the total revenue. Return status, count, and total revenue ordered by total revenue descending.
Use the orders table.
Solution
SELECT
status,
COUNT(order_id) AS order_count,
SUM(total_amount) AS total_revenue
FROM orders
GROUP BY status
ORDER BY total_revenue DESC;
Explanation: GROUP BY status, then COUNT rows and SUM total_amount per group. ORDER BY the aggregate result descending.