Calculate Payment Success Rate per Method with CASE WHEN
For each payment method, show transactions, successful payments, success rate and revenue.
The challenge runs on the SQLabHub practice database (SQLite) and uses the payments table. You can solve it in the browser, with nothing to install.
The task
Analyze payment methods: show transaction count, success rate (%), total revenue per method.
A correct answer returns 8 rows with 5 columns: method, total_transactions, successful, success_rate_pct, revenue.
Tables used in this challenge
payments 200 rows
id INTEGER (PK) · order_id INTEGER · user_id INTEGER · amount REAL · method TEXT · status TEXT · paid_at TEXT · transaction_id TEXT
| id | order_id | user_id | amount | method | status | paid_at | transaction_id |
|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| method | total_transactions | successful | success_rate_pct | revenue |
|---|---|---|---|---|
The complete result: 8 rows. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping rows with GROUP BY to get one result row per group
- Turning conditions into values with CASE WHEN
- Sorting the result with ORDER BY
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Use CASE WHEN inside SUM/COUNT for conditional aggregation
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
paymentstable. - Group the rows by
method, so the result has one row for each value. - For each group, compute
COUNT(*), a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'success' and a conditional sum ofamountover the rows wherestatusequals 'success'. ROUND()trims the calculated figures to a fixed number of decimals.- Sort the result by
revenuedescending.
Model solution
Checking your session…
What to watch for
Multiplying by 100.0 rather than 100 forces decimal division; with integers, SQLite would truncate every rate to a whole number. Putting the status test inside SUM(CASE ...) keeps failed payments in the denominator, which a WHERE filter would remove.
- GROUP BY. Every selected column must either be in GROUP BY or sit inside an aggregate. SQLite accepts other columns and returns an arbitrary value for them; most databases raise an error.
- CASE WHEN. CASE stops at the first WHEN that is true, so overlapping conditions must be ordered from most to least specific. Without an ELSE, unmatched rows get NULL.
- ORDER BY. Without ORDER BY the row order is not guaranteed, even if it looks stable. Rows that tie on the sort columns can come back in any order.
Related SQL challenges
- Salary Statistics per Department: Count, Avg, Min and MaxSimilar levelMedium
- Find the Best-Selling Products by QuantitySimilar levelMedium
- Count Orders by Status with GROUP BYEasierEasy
- Pivot Order Status Counts into Columns with CASE WHENHarderHard
- Count Employees per Salary Band in Each DepartmentHarderHard
Keep learning
- Lesson: GROUP BY
- Lesson: CASE WHEN
- Lesson: ORDER BY
- Lesson: Aggregate Functions
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database