LEFT JOIN from the Salesperson table to the Customer table on salesperson_id to include all salespeople, even those without customers.LEFT JOIN from the result to the Sales table on customer_id to include all sales.SUM function to calculate the total price for each salesperson.GROUP BY clause to group the results by salesperson_id.IFNULL function to replace NULL totals with 0.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