r/learnSQL • • Oct 29 '26

SQL puzzle: what's total March revenue from these two tables?

I'm practising SQL and came across this question. Curious how others would solve it.

Question: What was our total revenue for March 2026?

orders has one row per order. order_total is the full order amount.

order_items has one row per item in an order, linked by order_id.

orders:

order_id customer_id order_date order_total

1 101 2026-03-02 50

2 102 2026-03-05 120

3 103 2026-03-09 25

4 101 2026-03-12 90

5 104 2026-03-18 200

6 105 2026-03-21 35

7 102 2026-03-27 60

8 106 2026-02-26 80

order_items:

item_id order_id product item_price

1 1 Notebook 30

2 1 Pen set 20

3 2 Backpack 60

4 2 Lamp 40

5 2 Mug 20

6 3 Poster 25

7 4 Headphones. 70

8 4 Cable 20

9 5 Desk mat 80

10 5 Keyboard 50

11 5 Mouse 40

12 5 Stand 30

13 6 Cap 35

14 7 Bottle 35

15 7 Towel 25

16 8 Socks 50

17 8 Scarf 30

What's your query, and what number do you get? I'll post the answer in a few days.

Also curious: have you made a mistake like this at work or in an interview?

2 Upvotes

11 comments sorted by

2

u/jayjetley Oct 29 '26

The figure is 580 and the SQL is

select sum(O.order_total) as revenue
from orders O
where O.order_date between '2026-03-01' and '2026-03-31'

The order_lines is irrelevant in this case unless you wanted revenue by product in which case you would use

select product, sum(item_price) as revenue
from orders O
join order_items OI on OI.order_id = O.order_id
where O.order_date between '2026-03-01' and '2026-03-31'
group by product

1

u/shah-26 Oct 30 '26

Thanks, that's right, 580. You're also right that order_items isn't needed for this version.

One question if you don't mind: when you were first learning SQL, was this kind of double-counting (joining and then summing) something you ever got caught by, or did it click straight away? And would you have found it useful if a practice site told you exactly why a wrong total was wrong?

1

u/jayjetley Nov 02 '26

I suspect if anyone who writes SQL on a regular basis says they haven't been caught by this trap they are not being entirely truthful. It's why it also sensible to sense check any queries and if possible run them over a smaller dataset so you can easily validate the answer before running over a larger dataset.

However, sometimes it can be useful to have all combinations in a results set so it's not always a bug. For instance if you had a table of products and a table of stores you can easily get a list of every product/store combination using

select S.storeId, P.productId
from Store S, Product P

1

u/shah-26 Nov 02 '26

Agreed, I think everyone gets caught by it once. Checking on a small dataset first is a great habit, and the cross-join example is a good reminder that a multiplying join isn't always a bug. Thanks for the perspective!

1

u/shah-26 Oct 31 '26

Thanks everyone! The answer is 580, and you're right that the items table isn't needed here, so my puzzle didn't force the join. My mistake.

The pitfall I was after: if you do join orders to order_items and then SUM(order_total), each order is counted once per item, which gives 1,620 here.

For people still learning: did you ever get caught by that? I'm trying to understand which SQL mistakes are most common when you're starting out, and whether feedback that explains why a total is wrong would have helped. I'll post a harder version where the join is needed.

1

u/Wonderful-Mode-4027 Oct 29 '26

The query is a simple SELECT SUM(order_total) FROM orders WHERE order_date BETWEEN 2026-03-01 AND 2026-03-31. Joining with order_items causes double counting because one order has multiple items.

1

u/shah-26 Oct 30 '26

Thanks, that's right, 580. You're also right that order_items isn't needed for this version.

One question if you don't mind: when you were first learning SQL, was this kind of double-counting (joining and then summing) something you ever got caught by, or did it click straight away? And would you have found it useful if a practice site told you exactly why a wrong total was wrong?

1

u/woahboooom Oct 29 '26

Select #2026-03-01# month_date, sum(order_items.item_price) total_price From Orders left join order_items On orders.order_id = order_items.order_id Where orders.order_date >= #2026-03-01# and orders.order_date<#2026-04-01#

1

u/shah-26 Oct 30 '26

Thanks, that's right, 580. You're also right that order_items isn't needed for this version.

One question if you don't mind: when you were first learning SQL, was this kind of double-counting (joining and then summing) something you ever got caught by, or did it click straight away? And would you have found it useful if a practice site told you exactly why a wrong total was wrong?

2

u/woahboooom Oct 30 '26

It does catch you out. Usually mid joins on multiple tables.

It helps to build the tables, and make it in sheets with minimal data to visualise how it happens.

Occassionally still happens.

As a check, run a total on the sub table or a count of rows on the left side table; as many checks as possible. Avoid inner, its faster, but might have less if ids break for some reason...