HardSQL challenge #263GROUP BYHAVINGDATA QUALITY

Find Orders with More Than One Payment Record

Return the orders that have two or more rows in the payments table.

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

Why this question comes up

Finance found that some orders have more than one payment record: split payments, retries after a failure, or duplicates. The first step is listing those orders.

The task

Find orders that have more than one payment record.

A correct answer returns 19 rows with one column: order_id.

Tables used in this challenge

payments 207 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
12810.52Apple Paysuccess2024-03-15TXNPTNV0A8A1
23136181.19Debit Cardsuccess2023-02-15TXNZAMXH61A0
34115114.95Debit Cardsuccess2022-01-10TXN1PVXYJS9D

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
order_id
19
31
36
48
77

First 5 of 19 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.

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
  • Filtering groups after aggregation with HAVING

Hint and approach

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

Show the hint

GROUP BY order_id HAVING COUNT

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. Group the rows by order_id, so the result has one row for each value.
  3. For each group, compute COUNT(*).
  4. Filter the groups in HAVING: keep those where COUNT(*) is greater than 1.
Show the model solution
SELECT order_id
FROM payments
GROUP BY order_id
HAVING COUNT(*) > 1;

This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.

What to watch for

Group by the key and keep the groups with a count above 1. The same query finds duplicate emails, invoice numbers or any other value that should be unique.

  • HAVING. Conditions on single rows belong in WHERE and conditions on aggregates in HAVING. Putting a row filter in HAVING works, but it groups rows that are about to be thrown away.
  • 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.
  • 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