Leetcode Problem 2372. Calculate the Influence of Each Salesperson

2372. Calculate the Influence of Each Salesperson

Leetcode Solutions

Using LEFT JOIN and GROUP BY to Calculate Sales Totals

  1. Perform a LEFT JOIN from the Salesperson table to the Customer table on salesperson_id to include all salespeople, even those without customers.
  2. Perform another LEFT JOIN from the result to the Sales table on customer_id to include all sales.
  3. Use the SUM function to calculate the total price for each salesperson.
  4. Use the GROUP BY clause to group the results by salesperson_id.
  5. Use the IFNULL function to replace NULL totals with 0.
  6. Select the salesperson_id, name, and the calculated total sales as the final output.

erDiagram
    Salesperson {
        int salesperson_id PK
        varchar name
    }
    Customer {
        int customer_id PK
        int salesperson_id FK
    }
    Sales {
        int sale_id PK
        int customer_id FK
        int price
    }
    Salesperson ||--o{ Customer : has
    Customer ||--o{ Sales : makes

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...