+----------------+---------+
|Column Name |Type|+----------------+---------+
| salesperson_id | int || name | varchar |+----------------+---------+
salesperson_id containsuniquevalues.
Eachrowin this table shows the ID of a salesperson.
Table: Customer
1
2
3
4
5
6
7
8
9
+----------------+------+
|Column Name |Type|+----------------+------+
| customer_id | int || salesperson_id | int |+----------------+------+
customer_id containsuniquevalues.
salesperson_id is a foreignkey (reference column) from the Salesperson table.
Eachrowin this table shows the ID of a customer and the ID of the salesperson.
Table: Sales
1
2
3
4
5
6
7
8
9
10
+-------------+------+
|Column Name |Type|+-------------+------+
| sale_id | int || customer_id | int || price | int |+-------------+------+
sale_id containsuniquevalues.
customer_id is a foreignkey (reference column) from the Customer table.
Eachrowin this table shows ID of a customer and the price they paid for the sale with sale_id.
Write a solution to report the sum of prices paid by the customers of each salesperson. If a salesperson does not have any customers, the total value should be 0.
Return the result table in any order.
The result format is shown in the following example.
Input: Salesperson table:+----------------+-------+| salesperson_id | name |+----------------+-------+|1| Alice ||2| Bob ||3| Jerry |+----------------+-------+ Customer table:+-------------+----------------+| customer_id | salesperson_id |+-------------+----------------+|1|1||2|1||3|2|+-------------+----------------+ Sales table:+---------+-------------+-------+| sale_id | customer_id | price |+---------+-------------+-------+|1|2|892||2|1|354||3|3|988||4|3|856|+---------+-------------+-------+ Output:+----------------+-------+-------+| salesperson_id | name | total |+----------------+-------+-------+|1| Alice |1246||2| Bob |1844||3| Jerry |0|+----------------+-------+-------+ Explanation: Alice is the salesperson for customers 1 and 2.- Customer 1 made one purchase with354.- Customer 2 made one purchase with892. The total for Alice is354+892=1246. Bob is the salesperson for customers 3.- Customer 1 made one purchase with988 and 856. The total for Bob is988+856=1844. Jerry is not the salesperson of any customer. The total for Jerry is0.
We need to sum the prices of all sales made by customers of each salesperson. If a salesperson has no customers or sales, their total should be 0. We use LEFT JOINs to ensure all salespeople are included, even if they have no customers or sales.