Top 3 Customers by Total Transaction Amount

Quick Overview

Write a SQL query that joins users to their transactions and returns the three users with the largest total transaction amount, breaking ties by user id. It tests JOIN with GROUP BY and SUM, a deterministic ORDER BY with a tiebreaker, and LIMIT.

Top 3 Customers by Total Transaction Amount

Company: NVIDIA

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You have a `Users` table and a `Transactions` table. Return the top 3 customers by total transaction amount: `user_id`, `name`, and `total_amount` (the sum of their `amount`). Order by `total_amount` descending, with ties broken by `user_id` ascending. ```hint Rank the right thing The ranking is over each customer's total, so work out what one row per customer looks like before you sort anything. ``` ### Input Table(s) Table `Users`, primary key `user_id`: | Column | Type | Meaning | |---|---|---| | user_id | INT | User identifier (primary key) | | name | VARCHAR(100) | User name, NOT NULL | Table `Transactions`, primary key `transaction_id`. `user_id` is a foreign key to `Users.user_id`: | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier (primary key) | | user_id | INT | User who made the transaction, NOT NULL | | amount | DECIMAL(10,2) | Transaction amount, positive, NOT NULL | ### Output Contract - Columns, in this order: `user_id`, `name`, `total_amount`. - `total_amount` is the sum of `amount` over all of the user's transactions. Because `amount` has 2 decimal places, the sum does too, and no extra rounding is needed. - Sort by `total_amount` descending, then by `user_id` ascending. - Return at most 3 rows. When fewer than 3 users have any transactions, return only those users. ### Example `Users` | user_id | name | |---|---| | 1 | Ana | | 2 | Ben | | 3 | Cara | | 4 | Dev | | 5 | Emma | `Transactions` | transaction_id | user_id | amount | |---|---|---| | 1 | 1 | 50.00 | | 2 | 2 | 120.00 | | 3 | 1 | 70.00 | | 4 | 3 | 45.50 | | 5 | 4 | 80.00 | | 6 | 3 | 34.50 | Expected result: | user_id | name | total_amount | |---|---|---| | 1 | Ana | 120.00 | | 2 | Ben | 120.00 | | 3 | Cara | 80.00 | The totals are Ana 120.00 (50.00 + 70.00), Ben 120.00, Cara 80.00 (45.50 + 34.50) and Dev 80.00. Emma has no transactions. Ana and Ben tie for first and come out in `user_id` order. Cara and Dev tie for third, and Cara takes the slot because her `user_id` is smaller. ### Constraints and Clarifications - `Users` has at most 10,000 rows and `Transactions` at most 100,000 rows. - Every `Transactions.user_id` references a valid `Users.user_id`. - `amount` is always positive and never NULL. - Users with no transactions are left out of the result. - Ties on `total_amount` are broken by smaller `user_id`. Because `user_id` is the primary key, the order of the result is unique.

Overview: Write a SQL query that joins users to their transactions and returns the three users with the largest total transaction amount, breaking ties by user id. It tests JOIN with GROUP BY and SUM, a deterministic ORDER BY with a tiebreaker, and LIMIT.

NVIDIA logo
NVIDIA
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

You have a Users table and a Transactions table. Return the top 3 customers by total transaction amount: user_id, name, and total_amount (the sum of their amount). Order by total_amount descending, with ties broken by user_id ascending.

Input Table(s)

Table Users, primary key user_id:

ColumnTypeMeaning
user_idINTUser identifier (primary key)
nameVARCHAR(100)User name, NOT NULL

Table Transactions, primary key transaction_id. user_id is a foreign key to Users.user_id:

ColumnTypeMeaning
transaction_idINTTransaction identifier (primary key)
user_idINTUser who made the transaction, NOT NULL
amountDECIMAL(10,2)Transaction amount, positive, NOT NULL

Output Contract

  • Columns, in this order: user_id , name , total_amount .
  • total_amount is the sum of amount over all of the user's transactions. Because amount has 2 decimal places, the sum does too, and no extra rounding is needed.
  • Sort by total_amount descending, then by user_id ascending.
  • Return at most 3 rows. When fewer than 3 users have any transactions, return only those users.

Example

Users

user_idname
1Ana
2Ben
3Cara
4Dev
5Emma

Transactions

transaction_iduser_idamount
1150.00
22120.00
3170.00
4345.50
5480.00
6334.50

Expected result:

user_idnametotal_amount
1Ana120.00
2Ben120.00
3Cara80.00

The totals are Ana 120.00 (50.00 + 70.00), Ben 120.00, Cara 80.00 (45.50 + 34.50) and Dev 80.00. Emma has no transactions. Ana and Ben tie for first and come out in user_id order. Cara and Dev tie for third, and Cara takes the slot because her user_id is smaller.

Constraints and Clarifications

  • Users has at most 10,000 rows and Transactions at most 100,000 rows.
  • Every Transactions.user_id references a valid Users.user_id .
  • amount is always positive and never NULL.
  • Users with no transactions are left out of the result.
  • Ties on total_amount are broken by smaller user_id . Because user_id is the primary key, the order of the result is unique.