Problem Statement
After joining orders to order_items, your totals are doubled. Why did this happen, and how do you fix it?
Explanation
The join expanded each order into one row per item. If you later sum an order level column, it repeats per item and inflates the total. This is a cardinality trap common with one-to-many joins.
Fix by aggregating at the right level before joining, or by summing item level amounts instead of order level amounts. Another option is to SELECT DISTINCT at the order level if your goal is only to deduplicate, but pre-aggregation is more precise for sums.
Code Solution
SolutionRead Only
SELECT SUM(oi.line_total) FROM orders o JOIN order_items oi ON oi.order_id = o.id;
