Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

EXPERIMENT - 6

Create a database containing three table


SALES (sid,name,city,commission)
ORDERS (ordno,purch_amt,ord_date,customer_id,salesman_id)
CUSTOMER (customer_id,cust_name,city,grade,salesman_id)

SalesTable:
Create database joinss;
Use joinss;
create table sales ( sid int primary key, name
varchar(20), city varchar(20),
commission float );

Insert Commands for the Sales Table:


Insert into joinss.sales values
('5001','Harsh Chauhan','kanpur','0.15'),
('5002','Mehul Kumar','Varanasi','0.13'),
('5003','Dev','Prayagraj','0.11'),
('5004',' Anshveer','Delhi','0.12'),
('5005','Obama','Jammu','0.17');

OrderTable:
create table orders (
ordno int primary
key, purch_amt float,
ord_date date,
customer_id int,
salesman_id int );

Insert Commands for the order table:


insert into joinss.orders values
('77018','150.9','2023-09-10','3005','5001'),
('77019','270.65','2023-09-11','3001','5001'),
('77020','65.36','2023-09-12','3005','5003'),
('77021','700.76','2023-09-13','3002','5004'),
('77022','948.78','2023-09-14','3004','5005'),
('77023','2400.89','2023-09-15','3004','5002');
Customer Table:
create table customer (
customer_id int primary key,
cust_name varchar(20),
city varchar(20),
grade int,
salesman_id int );

Insert Command for the Customer table:


insert into joinss.customer values
('3001','Alan','Varanasi','100','5001'),
('3002','Meul','Prayagraj','200','5002'),
('3003','Divya','Bengalore','300','5001'),
('3004','Kanss','Chennai','150','5003'),
('3005','Barack','Kolkata','400','5005');

Queries:
1. Findthesalespersonandcustomerwhoresideinthesamecity.
Return Salesman,
select sales.name,customer.cust_name
from joins.sales,joins.customer
where sales.city = customer.city;

2. Find those orders where the order amount exists between 500
and 2000. Return ord_no, purch_amt, cust_name, city.
select orders.ordno,orders.purch_amt,customer.cust_name,customer.city
from orders join customer on orders.customer_id=customer.customer_id
where orders.purch_amt > '500' and orders.purch_amt < '2000';
3. Find the salesperson(s) and the customer(s) he represents.
Return Customer Name, city, Salesman, commission.
select customer.cust_name, customer.city, sales.sid, sales.commission
from customer inner join sales on customer.salesman_id = sales.sid;

4. Find salespeople who received commissions of more than 12


percent from the company. Return Customer Name, customer
city, Salesman, commission.
select customer.cust_name
from customer INNER JOIN sales ON
customer.salesman_id = sales.sid
where sales.commission > 0.12;

5. 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,
commission.
select customer.cust_name as "Customer Name",
customer.city as "Customer City",
sales.sid, sales.city as "Salesman City",
sales.commission from customer inner join sales on
customer.salesman_id= sales.sid
where sales.city <> customer.city and sales.commission > 0.12;
6. Find the details of an order. Return ord_no, ord_date,
purch_amt, Customer Name, grade, Salesman, commission
select orders.ordno, orders.ord_date, orders.purch_amt,
customer.cust_name as "Customer Name", customer.grade,
sales.sid, sales.commission from orders
join customer on orders.customer_id = customer.customer_id join sales
on customer.salesman_id = sales.sid where orders.ordno = '77019';

7. Write a SQL statement to join the tables salesman, customer and


orders so that the same column of each
select sales.sid as "Salesman",
customer.cust_name as "Customer Name",
orders.ordno as "Order Number", orders.ord_date as "Order Date" from
sales inner join customer on sales.city = customer.city inner join orders on
customer.customer_id = orders.customer_id;

8.
Display the customer name, customer city, grade, salesman,
salesman city. The results should be sorted by ascending
customer_id. table appears once and only the relational rows are
returned.
select customer.cust_name as "Customer Name",
customer.city as "Customer City",
customer.grade as "Grade", sales.sid,
sales.city as "Salesman City" from customer inner join sales on
customer.salesman_id = sales.sid order by customer.customer_id
asc;
9. 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. Write a SQL
statement to create a Cartesian product between salesperson
and customer, i.e. each salesperson will appear for all
customers and vice versa for that salesperson who belongs to
that city.
select
customer.cust_name,customer.city,customer.grade,sales.sid,
sales.city from customer join sales on customer.city=sales.city
where customer.grade < '300' order by customer.customer_id;

You might also like