-- link sales folks to their offices
select a.firstName, a.lastName, a.employeeNumber, a.officeCode, 
b.officeCode, b.country
from employees a, offices b
where a.officeCode = b.officeCode 
and
a.jobTitle = 'Sales Rep'
order by a.officeCode;

-- link sales folks to their orders
select a.customerNumber, a.customerName, a.salesRepEmployeeNumber, b.employeeNumber, c. customerNumber, c.orderNumber
from customers a, employees b, orders c
where a.salesRepEmployeeNumber = b.employeeNumber
and
a.customerNumber = c.customerNumber
order by b.employeeNumber;

-- link orders and order details and products together order by the product code
select a.customerNumber, a.orderNumber, b.orderNumber, b.productCode, b.quantityOrdered, c.productCode, c.productName, c.productLine
from orders a, orderdetails b, products c
where a.orderNumber = b.orderNumber
and
b.productCode = c.productCode
order by c.productCode;

-- link orders and order details and products together order by the customer number
select a.customerNumber, a.orderNumber, b.orderNumber, b.productCode, b.quantityOrdered, c.productCode, c.productName, c.productLine
from orders a, orderdetails b, products c
where a.orderNumber = b.orderNumber
and
b.productCode = c.productCode
order by a.customerNumber;

-- sum total items ordered and link orders and order details and products together order by the customer number
select a.customerNumber, a.orderNumber, b.orderNumber, b.productCode, sum(b.quantityOrdered), c.productCode, c.productName, c.productLine
from orders a, orderdetails b, products c
where a.orderNumber = b.orderNumber
and
b.productCode = c.productCode
group by a.customerNumber;



-- link sales people, customers, orders and order details and products together order by the customer number
select 
a.employeeNumber, b.salesRepEmployeeNumber, c.customerNumber, c.orderNumber, 
d.orderNumber, d.productCode, d.quantityOrdered, e.productCode, e.productName, e.productLine
from 
employees a, customers b, orders c, orderdetails d, products e
where 
a.employeeNumber = b.salesRepEmployeeNumber
and
b.customerNumber = c.customerNumber
and
c.orderNumber = d.orderNumber
and
d.productCode = e.productCode
order by a.employeeNumber;

-- Final with total orders link sales people, customers, orders and order details and products together order by the customer number
select 
a.employeeNumber, b.salesRepEmployeeNumber, c.customerNumber, c.orderNumber, 
d.orderNumber, d.productCode, sum(d.quantityOrdered), e.productCode, e.productName, e.productLine
from 
employees a, customers b, orders c, orderdetails d, products e
where 
a.employeeNumber = b.salesRepEmployeeNumber
and
b.customerNumber = c.customerNumber
and
c.orderNumber = d.orderNumber
and
d.productCode = e.productCode
group by a.employeeNumber;