Monthly revenue
Mediumsqlgroup bydateswhere
Return month (as YYYY-MM) and revenue = the sum of amount over orders whose status is paid. Months without paid orders are omitted. Sort by month.
Tables
- orders (id INTEGER, ordered_on TEXT, amount INTEGER, status TEXT)
Example 1
orders
| id | ordered_on | amount | status |
|---|---|---|---|
| 1 | 2026-01-05 | 300 | paid |
| 2 | 2026-01-20 | 200 | refunded |
| 3 | 2026-02-01 | 150 | paid |
| 4 | 2026-01-31 | 100 | paid |
Expected result
| month | revenue |
|---|---|
| 2026-01 | 400 |
| 2026-02 | 150 |
Constraints
- 0 ≤ rows ≤ 1000
- ordered_on is YYYY-MM-DD
- The order of rows matters
Submit checks your solution against 7 tests, including hidden ones.
SQL
-- Write one SELECT query. Tables: orders SELECT