YOUR AD GOES HERE

Why Does My SUM Double After a JOIN? SQL Bug Hunt #SQL #SQLBugs

Published 28, Nov 2026


Description:
Revenue is 550, not 250, and there is no error. Can you spot the bug?

The bug: each order has several items, so joining orders to order_items makes one row per item. Order 1 appears twice and order 3 appears three times, so SUM adds their amounts again and again: 100 + 100 + 50 + 100 + 100 + 100 = 550 instead of 250. COUNT of items still shows 6, which hides the problem.

The fix that still fails: SUM(DISTINCT amount) returns 150, because two orders share the amount 100 and DISTINCT merges them.

The real fix: aggregate before you join. Count the items per order in a subquery with GROUP BY, then join that to orders. Each order stays one row, and the result is 250 revenue and 6 items.

Practice SQL for free on SQLint:
✅ Free, no signup
✅ Practice in the SQL playground
✅ Interview questions with hints
https://sqlint.com
https://sqlint.com/learn-sql

Follow for a new SQL lesson every week.
Did you spot the bug before the reveal? Tell us in the comments.

#SQL #SQLBugs #SQLJoins #LearnSQL #SQLTutorial #Database #DataAnalytics #DataScience #Coding #Debugging #SQLQueries #SQLInterview #SQLint

Releted More Videos

  • Sorry!!! Nothing to show

You May Also Like

YOUR AD GOES HERE

YOUR AD GOES HERE