Home > front end >  Having difficulty with sql query
Having difficulty with sql query

Time:01-01

I am working on a problem where I have to create an SQL query retrieving all employees and the respective sum of invoice totals for the customers they support. Please order the result set in descending order by the total invoice sum.

Here is the schema Schema

Here output of the problem

Here is my SQL query

select e.EmployeeId,  avg(i.Total) as total 
from employees as e, invoices as i ;

CodePudding user response:

Join 3 tables and apply order by; I guess that's OK because model doesn't show entire invoices table ("4 more columns", eh?)

select e.employee_id,
       sum(i.total) as sum_total
from employees e join customers c on c.supportrepid = e.employee_id
                 join invoices i on i.customerid = c.customerid
group by e.employee_id
order by 2 desc
  • Related