SELECT c.name,COUNT(t.id) purchases,SUM(t.final_total) total_spent FROM contacts c JOIN transactions t ON c.id=t.contact_id WHERE t.type="sell" AND c.email IS NOT NULL GROUP BY c.id,c.name ORDER BY purchases DESC;