Find the top 10 accounts in terms of highest total_amt_usd
:
SELECT
AVG(total_amt_usd)
FROM
orders
WHERE
total_amt_usd = (SELECT A.id, A.name, SUM(O.total_amt_usd) tot_spent
FROM orders O
JOIN accounts A ON A.id = O.account_id
GROUP BY A.id, A.name
ORDER BY 3 DESC
LIMIT 10)