SQL
SQL Query for Aggregated Sales Summary
Summarize sales data by grouping and aggregating customer orders, filtering groups based on total value. Essential for sales reports.
SELECT c.customer_name, COUNT(o.order_id) AS total_orders, SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
HAVING SUM(o.total_amount) > 1000
ORDER BY total_spent DESC;
How it works: This query calculates the total number of orders and total amount spent for each customer. It then filters these aggregated results to show only customers who have spent more than 1000, ordering them by the total amount spent. This is valuable for identifying high-value customers.