you ask
“Which five customers spent the most in the last 30 days?”
PlainQuery joined orders and customers, filtered by created_at, summed totals, and ranked descending. The query was validated read-only before it ran.
SELECT c.name, SUM(o.total) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= NOW() - INTERVAL '30 days'
GROUP BY c.name
ORDER BY revenue DESC
LIMIT 5;