EasySQL challenge #97SUM

Total Payment Amount

Calculate total amount of all payments.

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.

Solve This SQL Challenge

The task

Calculate total amount of all payments.

A correct answer returns 1 row with one column: SUM(amount).

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

First 3 rows of the payments table
idorder_iduser_idamountmethodstatuspaid_attransaction_id
146105921.62Gift Cardsuccess2022-05-18TXNHTN4GIK5L
2157371723.82Bank Transfersuccess2024-11-11TXNKJPIXJOFP
34536978.76Bank Transferrefunded2023-10-24TXNU2RW4MK1R

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
SUM(amount)
200873.37

The complete result: 1 row. 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

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

SUM(amount)

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the payments table.
  2. Aggregate over all the rows that are left: compute SUM(amount). With no GROUP BY, the result is a single row.

Model solution

Checking your session…

What to watch for

  • Aggregate functions. Aggregates skip NULLs. AVG divides by the number of non-NULL values and COUNT(column) can be smaller than COUNT(*).
Solve This SQL Challenge

Keep learning