I tried solving 106 problem using the following query:
SELECT utm_campaign, SUM(amount) AS total_revenue, COUNT(DISTINCT a.id) AS total_users, SUM(amount)/COUNT(DISTINCT b.id) AS ARPU FROM purchases a JOIN users b ON a.user_id = b.id AND a.refunded = FALSE -- WHERE utm_campaign IS NOT NULL GROUP BY 1 ORDER BY 4 DESC
which should be quite similar with the proposed solution, the difference is on the order of FROM clause. I used purchase table before user table and it gives a very different results.
I’d like to understand why is this the case as this is a quite surprising SQL behavior to me.