0% found this document useful (0 votes)
3 views9 pages

Assignment On Cust Salesman Orders

The document contains SQL commands for creating and populating tables for salesmen, customers, and orders. It includes various SQL queries to retrieve specific data such as salespeople with high commissions, customers with low grades, and order details based on certain conditions. Additionally, it defines views to summarize customer orders and salespersons related to specific orders.

Uploaded by

sbpatil1379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Assignment On Cust Salesman Orders

The document contains SQL commands for creating and populating tables for salesmen, customers, and orders. It includes various SQL queries to retrieve specific data such as salespeople with high commissions, customers with low grades, and order details based on certain conditions. Additionally, it defines views to summarize customer orders and salespersons related to specific orders.

Uploaded by

sbpatil1379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment

use practice

select * from INFORMATION_SCHEMA.tables;

create table salesman


(salesman_id int primary key,
name varchar(20),
city varchar(20),
commission decimal(10,2))

insert into salesman


values
(5001, 'james hoog', 'new york', 0.15),
(5002, 'james hoog', 'new york', 0.13),
(5005, 'james hoog', 'new york', 0.11),
(5006, 'james hoog', 'new york', 0.14),
(5007, 'james hoog', 'new york', 0.13),
(5003, 'james hoog', 'new york', 0.12)

create table customer


(customer_id int primary key,
cust_name varchar(20),
city varchar(20),
grade int,
salesman_id int)

insert into customer


values
(3002, 'nick rimando', 'new york', 100, 5001),
(3007, 'brad davis', 'new york', 200, 5001),
(3005, 'graham zusi', 'california', 200, 5002),
(3008, 'julian green', 'london', 300, 5002),
(3004, 'fabian johnson', 'paris', 300, 5006),
(3009, 'geoff cameron', 'berlin', 100, 5003),
(3003, 'jozy altidor', 'moscow', 200, 5007)

create table orders


(ord_no int primary key,
purch_amt decimal(10,2),
ord_date date,
customer_id int,
salesman_id int)

insert into orders


values
(70001, 150.5, '2012-10-05', 3005, 5002),
(70009, 270.65, '2012-09-10', 3001, 5005),
(70002, 65.26, '2012-10-05', 3002, 5001),
(70004, 110.5, '2012-08-17', 3009, 5003),
(70007, 948.5, '2012-09-10', 3005, 5002),
(70005, 2400.6, '2012-07-27', 3007, 5001),
(70008, 5760, '2012-09-10', 3002, 5001)

/* 1. write a SQL query to find salespeople who received commissions


of more than 12 percent from the company.
Return Customer Name, customer city, Salesman, commission. */
select cust_name, c.city, name, commission
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 commission > 0.12
/* 2. write a SQL query to locate those salespeople who do not live in
the same city where their customers live
and have received a commission of more than 12% from the company.
Return Customer Name, customer city,
Salesman, salesman city, and commission. */
select cust_name, c.city, name, s.city, commission
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 c.city !=s.city and commission > 0.12

/* 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. */

/* 5. Write a SQL statement to generate a report with the customer's


name, city, order no. order date,
purchase amount for only those customers on the list who must have a
grade and placed one or more orders or
which order(s) have been placed by the customer who neither is on the
list nor has a grade. */
select name,c.city, ord_no, ord_date, purch_amt
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 c.grade IS NOT NULL
OR c.customer_id NOT IN
(SELECT customer_id FROM Customer WHERE grade IS NOT
NULL)

/* 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

/* 12. create a view to count the number of unique customers, compute


the average and the total purchase
amount of customer orders by each date. */
create view vw_custorders as
select ord_date,
count(distinct customer_id) as Unique_Customers,
sum(purch_amt) as Sum_Purchase_Amount,
avg(purch_amt) as Avg_Purchase_Amount
from orders
group by ord_date;
select * from vw_custorders

/* 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);

select * from vw_salespersonperday

/* 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')

select * from vw_salespersonorders

You might also like