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.

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 Tables Table `users`, primary key `user_id`: | Column | Type | Meaning | |---|---|---| | user_id | INTEGER | 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 | INTEGER | Transaction identifier (primary key) | | user_id | INTEGER | 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. ### 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.

Tables

users(user_id INTEGER, name VARCHAR(100))

transactions(transaction_id INTEGER, user_id INTEGER, amount DECIMAL(10,2))

Hints

  1. The ranking is over each customer's total, so work out what one row per customer looks like before you sort anything.
  2. Two users can share the same total. Settle the order among them before you cut the list down to 3 rows.
NVIDIAData ScientisteasyData Manipulation (SQL/Python)

Top 3 Customers by Total Transaction Amount

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 Tables

Table users, primary key user_id:

ColumnTypeMeaning
user_idINTEGERUser 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_idINTEGERTransaction identifier (primary key)
user_idINTEGERUser 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.

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.

Expected Output

Tables

userstransactions
users
ColumnType
user_idINTEGER
nameVARCHAR(100)
transactions
ColumnType
transaction_idINTEGER
user_idINTEGER
amountDECIMAL(10,2)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions