Table Schema
Inspect
Table
| Column | Type | Description |
|---|---|---|
| transaction_id | integer | Unique transaction identifier. |
| user_id | integer | User who made the transaction. |
Find the user with the highest number of transactions.
Table Schema
| Column | Type | Description |
|---|---|---|
| transaction_id | integer | Unique transaction identifier. |
| user_id | integer | User who made the transaction. |
Sample Data
| transaction_id | user_id |
|---|---|
| 1 | 101 |
| 2 | 102 |
| 3 | 101 |
| 4 | 103 |
| 5 | 101 |
| 6 | 102 |
| user_id | transaction_count |
|---|---|
| 101 | 3 |
SQL Editor
| user_id | transaction_count |
|---|---|
| 101 | 3 |
Hints
Solution
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
Count how many transactions each user has made.
Sort users by that count from highest to lowest.
Return the top user and their transaction count.
Related Questions