SQL
Lab
Amazon Easy Behavioral + SQL Mix Classic

Most
Active
User

Find the user with the highest number of transactions.

Table Schema

Inspect
Table

interactive
ColumnTypeDescription
transaction_idintegerUnique transaction identifier.
user_idintegerUser who made the transaction.

Sample Data

Input
Output

Sample Input: transactions
transaction_iduser_id
1101
2102
3101
4103
5101
6102
Expected Output
user_idtransaction_count
1013

SQL Editor

Run
Query

postgresql
Waiting for query

user_idtransaction_count
1013

Hints

Unlock
Clues

Hint 01: Count transactions per user.
Hint 02: Sort by the transaction count in descending order.
Hint 03: Return the top user with the highest count.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT
  user_id,
  COUNT(*) AS transaction_count
FROM transactions
GROUP BY user_id
ORDER BY transaction_count DESC, user_id ASC
LIMIT 1;

Explanation

Step By
Step

01

Count how many transactions each user has made.

02

Sort users by that count from highest to lowest.

03

Return the top user and their transaction count.

Related Questions

Keep
Solving