Cities With Completed Trades
Beginner Mode
Objective
Write a SQL query to identify the top three cities by number of completed trade orders.
Table Schema:
- trades
| Column | Type | Description |
|---|---|---|
| order_id | INTEGER | Unique identifier for each trade order |
| user_id | INTEGER | ID of the user who placed the order |
| status | VARCHAR | Status of the trade order (e.g., 'Completed', 'Cancelled', 'Pending') |
- users
| Column | Type | Description |
|---|---|---|
| user_id | INTEGER | Unique identifier for each user |
| city | VARCHAR | City where the user is located |
Task Requirements:
- Filter only trades with status 'Completed'
- Join trades with user details to get city information
- Count the number of completed orders per city
- Return the top three cities with the highest order counts
- Sort results in descending order by order count
Output columns: city, total_orders
Examples
Example 1:
Output:
Input:
| trades | ||
|---|---|---|
| order_id | status | user_id |
| 1 | Completed | 101 |
| 2 | Completed | 102 |
| 3 | Completed | 103 |
| 4 | Completed | 104 |
| 5 | Cancelled | 105 |
| users | |
|---|---|
| city | user_id |
| New York | 101 |
| New York | 102 |
| San Francisco | 103 |
| Boston | 104 |
| Chicago | 105 |
| city | total_orders |
|---|---|
| New York | 2 |
| Boston | 1 |
| San Francisco | 1 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Robinhood