Assignment On Cust Salesman Orders
Assignment On Cust Salesman Orders
use practice
/* 3. write a SQL query to find those customers with a grade less than
300. Return cust_name, customer city,
grade, Salesman, salesmancity. The result should be ordered by
ascending customer_id. */
select cust_name, c.city, grade, name, s.city
from customer c
inner join
orders o
on c.customer_id = o.customer_id
inner join salesman s
on o.salesman_id = s.salesman_id
Where grade < 300
/* 4. Write a SQL statement to make a list for the salesmen who either
work for one or more customers or
yet to join any of the customer. The customer may have placed, either
one or more orders on or above order
amount 2000 and must have a grade, or he may not have placed any
order to the associated supplier. */
/* 6. Write a SQL query to combine each row of the salesman table with
each row of the customer table. */
select * from customer
cross join salesman
/* 7. write a SQL query to find the details of the customers whose names
end with the letter 'n'.
Return customer_id, cust_name, city, grade, salesman_id. */
select customer_id, cust_name, city, grade, salesman_id
from customer
where cust_name like '%n'
/* 8. write a SQL query to find all the orders issued by the salesman
'Paul Adam'. Return ord_no, purch_amt,
ord_date, customer_id and salesman_id. */
select ord_no, purch_amt, ord_date, customer_id, salesman_id
from orders
where salesman_id = (select salesman_id from salesman where name =
'paul adam')
/* 9. write a SQL query to find the order values greater than the average
order value of 10th October 2012.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id. */
select ord_no, purch_amt, ord_date, customer_id, salesman_id
from orders
where purch_amt =(select avg(purch_amt) from orders
where ord_date = '10-10-2012')
/* 10. write a SQL query to find those salespeople who earned the
maximum commission.
Return ord_no, purch_amt, ord_date, and salesman_id. */
select ord_no, purch_amt, ord_date, salesman_id
from orders
where salesman_id in
(select salesman_id from salesman where commission = (select
max(commission) from salesman))
/* 11. Write a query to find the sums of the amounts from the orders
table, grouped by date, and eliminate
all dates where the sum was not at least 1000.00 above the maximum
order amount for that date. */
select sum(purch_amt), ord_date
from orders
group by ord_date
having sum(purch_amt) > max(purch_amt) + 1000.00
/* 13. create a view to find the salesperson who handles a customer who
makes the highest order of the day.
Return order date, salesperson ID, name. */
create view vw_salespersonperday as
select
o.ord_date,
s.salesman_id,
s.name as salesman_name
from orders o
join salesman s on o.salesman_id = s.salesman_id
where o.purch_amt = (
select max(o2.purch_amt)
from orders o2
where o2.ord_date = o.ord_date);
/* 14. create a view to find the salespersons who issued orders on either
August 17th, 2012 or October 10th, 2012.
Return salesperson ID, order number and customer ID. */
create view vw_salespersonorders as
select c.salesman_id,
o.ord_no,
o.customer_id
from orders o
join customer c on o.customer_id = c.customer_id
where o.ord_date in ('2012-08-17', '2012-10-10')