Leetcode Problem 1158. Market Analysis I

1158. Market Analysis I

Leetcode Solutions

Left Join and Aggregation

  1. Start with the Users table as the base for the query to ensure all users are included in the results.
  2. Perform a LEFT JOIN with the Orders table on the condition that the user_id matches the buyer_id and the order_date is within the year 2019.
  3. Use the COUNT function to count the number of orders for each user, resulting in the number of orders made by each user as a buyer in 2019.
  4. Group the results by user_id to consolidate orders per user.
  5. Select the user_id (aliased as buyer_id), join_date, and the count of orders as orders_in_2019.
  6. Sort the final result by user_id to organize the output.
erDiagram
    Users {
        int user_id
        date join_date
        varchar favorite_brand
    }
    Orders {
        int order_id
        date order_date
        int item_id
        int buyer_id
        int seller_id
    }
    Items {
        int item_id
        varchar item_brand
    }
    Users ||--o{ Orders : ""
    Orders }|--|| Items : ""

Subquery and Group By

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...