Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Finance suspects some orders were refunded twice. A duplicated return means more than one row in shop_returns pointing at the same order.
Return one row per affected order, ordered by order_id.
Result columns · in this order
order_id | The order that was refunded more than once. |
return_rows | How many return rows point at it. |
refunded | Total refunded across those rows. |
How to approach it
Group by the key you think should be unique, then keep only the groups that are not.
Sample input
| return_id | order_id | reason | refund_amount | returned_on |
|---|---|---|---|---|
| 1 | 1001 | damaged | 60 | 2026-01-09 |
| 2 | 1004 | wrong size | 50 | 2026-02-05 |
| 3 | 1004 | wrong size | 50 | 2026-02-20 |
| 4 | 1007 | changed mind | 90 | 2026-03-01 |
| 5 | null | unlinked | 25 | 2026-02-11 |
| 6 | 1008 | damaged | 150 | 2026-02-16 |
| 7 | 1011 | late delivery | 130 | 2026-03-20 |
| 8 | 1002 | damaged | 80 | 2026-01-15 |
| 9 | 1002 | damaged | 80 | 2026-01-18 |
| 10 | 1008 | damaged | 150 | 2026-02-20 |
10 rows — all rows shown.
Expected output
| order_id | return_rows | refunded |
|---|---|---|
| 1002 | 2 | 160 |
| 1004 | 2 | 100 |
| 1008 | 2 | 300 |
3 rows — all rows shown.
Constraints
refunded is the total refunded across those rows — the amount actually at risk.NULL order_id are not duplicates of anything and must be excluded.Worked example
Order 1004 has two return rows of 50.00 each, so it reports return_rows 2 and refunded 100.00.
Without the NULL filter, the unmatched return forms its own group. It has only one row so it fails the HAVING test here — but on data with two unmatched returns it would appear as a phantom duplicated order with no id, which is why the filter is written rather than left to luck.
What this tests
The duplicate-detection idiom — GROUP BY the key, then HAVING COUNT(*) > 1 — and remembering that NULLs group together into a bucket of their own.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.