~/ learn/ postgresql/ cards/ Capstone & Exam Prep
1 of 5

Customer leaderboard: name and total_spent (sum of quantity × unit_price across all their order_items), for customers who have ordered, most spent first. Columns: name, total_spent.

Customer leaderboard: name and total_spent (sum of quantity × unit_price across all their order_items), for customers who have ordered, most spent first. Columns: name, total_spent.

Answer

SELECT c.name, SUM(oi.quantity * oi.unit_price) AS total_spent FROM customers c JOIN orders o ON o.customer_id = c.id JOIN order_items oi ON oi.order_id = o.id GROUP BY c.name ORDER BY total_spent DESC;

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4475ec21-caf0-4332-98d2-9ce971aac276/flashcard utf-8 LF