I am trying to formulate a query which, given two tables: (1) Salespersons, (2) Sales; displays the id, name, and sum of sales brought in by the salesperson. The issue is that I can get the id and sum of brought in money but I don't know how to add their names. Furthermore, my attempts omit the salespersons which did not sell anything, which is unfortunate.
In detail:
There are two naive tables:
create table Salespersons (
id integer,
name varchar(100)
);
create table Sales (
sale_id integer,
spsn_id integer,
cstm_id integer,
paid_amt double
);
I want to make a query that for each Salesperson displays their total sum of sales brought in.
This query comes to mind:
select spsn_id, sum(paid_amt) from Sales group by spsn_id
This query only returns list of ids and total amount brought in, but not the names of the salespersons and it omits salespersons that sold nothing.
How can I make a query that for each salesperson in Salespersons table, prints their id, name, sum of their sales, 0 if they have sold nothing at all.
I appreciate any help!
Thanks ahead of time!