-- top customers this year SELECT c.id, c.name, COUNT(o.id) AS orders, SUM(o.total) AS spent, CASE WHEN SUM(o.total) > 1000 THEN 'gold' ELSE 'standard' END AS tier FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status <> 'cancelled' WHERE o.created_at >= '2026-01-01' AND c.country IN ('US', 'CA') GROUP BY c.id, c.name HAVING COUNT(o.id) > 2 ORDER BY spent DESC LIMIT 10;
| Size before (bytes) | 391 |
|---|---|
| Size after (bytes) | 431 |
Strings, quoted names, numbers and comments are copied exactly as written; only keyword case and whitespace change.
SQL Formatter · free calculator by CalcWorthy