Identifying Top Cities with the Highest Number of Completed Trade Orders: An SQL Tutorial

In this tutorial, we will solve a problem that involves identifying the top cities with the highest number of completed trade orders. We’ll use two tables from a Robinhood trading system: trades and users. Our goal is to find the top three cities with the highest number of completed trades and list them in descending order based on the number of completed orders.

Problem Overview

You have the following two tables:

  1. trades Table
  • order_id: Unique identifier for each trade order.
  • user_id: Identifier for the user who placed the trade.
  • quantity: Number of units traded.
  • status: Status of the trade (either ‘Completed’ or ‘Cancelled’).
  • date: Date and time when the trade was placed.
  • price: Price per unit of the traded item.
  1. users Table
  • user_id: Unique identifier for each user.
  • city: City where the user is located.
  • email: User’s email address.
  • signup_date: Date and time when the user signed up.

Objective: Write a query to retrieve the top three cities with the highest number of completed trade orders. The results should be sorted in descending order based on the number of completed orders. Output the city name and the corresponding number of completed orders.

Example Data:

trades Table:

order_iduser_idquantitystatusdateprice
10010111110Cancelled08/17/2022 12:00:009.80
10010211110Completed08/17/2022 12:00:0010.00
10025914835Completed08/25/2022 12:00:005.10
10026414840Completed08/26/2022 12:00:004.80
10030530015Completed09/05/2022 12:00:0010.00
10040017832Completed09/17/2022 12:00:0012.00
1005652652Completed09/27/2022 12:00:008.70

users Table:

user_idcityemailsignup_date
111San Franciscorrok10@gmail.com08/03/2021 12:00:00
148Bostonsailor9820@gmail.com08/20/2021 12:00:00
178San Franciscoharrypotterfan182@gmail.com01/05/2022 12:00:00
265Denvershadower_@hotmail.com02/26/2022 12:00:00
300San Franciscohoustoncowboy1122@hotmail.com06/30/2022 12:00:00

Desired Output:

citytotal_orders
San Francisco3
Boston2
Denver1

Explanation:

  1. San Francisco has the highest number of completed trade orders with a total of 3.
  2. Boston has the second highest number with 2 completed orders.
  3. Denver ranks third with 1 completed order.

SQL Query Breakdown

To solve this problem, follow these steps:

  1. Join the trades table with the users table on user_id to get the city information for each trade.
  2. Filter the trades to include only those with a ‘Completed’ status.
  3. Count the number of completed trades per city.
  4. Sort the results in descending order based on the count of completed trades.
  5. Limit the results to the top three cities.

Here is the SQL query to achieve this:

SELECT 
    u.city,
    COUNT(t.order_id) AS total_orders
FROM 
    trades t
JOIN 
    users u ON t.user_id = u.user_id
WHERE 
    t.status = 'Completed'
GROUP BY 
    u.city
ORDER BY 
    total_orders DESC
LIMIT 3;

Query Explanation:

  1. Joining Tables:
  • JOIN users u ON t.user_id = u.user_id: Joins the trades table with the users table to include city information.
  1. Filtering Completed Trades:
  • WHERE t.status = 'Completed': Ensures only completed trades are counted.
  1. Counting Completed Orders:
  • COUNT(t.order_id): Counts the number of completed trade orders per city.
  1. Grouping and Sorting:
  • GROUP BY u.city: Groups the results by city.
  • ORDER BY total_orders DESC: Sorts the results in descending order of completed orders.
  1. Limiting Results:
  • LIMIT 3: Restricts the output to the top three cities with the highest number of completed orders.

Conclusion

This SQL query efficiently identifies the top cities with the highest number of completed trade orders by leveraging joins, aggregations, and sorting techniques. Running this query with your dataset will help you gain valuable insights into the trading activity across different cities.