0% found this document useful (0 votes)
22 views

Lab

The document contains SQL queries and tables to analyze sales data. It creates tables for salesmen, customers, and orders. It then writes queries to: 1) Find customers who purchased between two dates excluding a specific date; 2) Find the cheapest customer by name and purchase amount; 3) Find the highest purchase amount for each customer on a given date with their ID and date.

Uploaded by

Nasir Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lab

The document contains SQL queries and tables to analyze sales data. It creates tables for salesmen, customers, and orders. It then writes queries to: 1) Find customers who purchased between two dates excluding a specific date; 2) Find the cheapest customer by name and purchase amount; 3) Find the highest purchase amount for each customer on a given date with their ID and date.

Uploaded by

Nasir Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* Find the customer who has the highest payment */

select * from customers;


select * from employees;
select * from orders;
select * from payments;

select customers.customerNumber,customerName, sum(amount)


from payments join customers
on customers.customerNumber = payments.customerNumber
where amount=(select max(amount) from payments)
group by customerNumber;

/* Find customers whose payments are greater than the average payment */
select avg(amount)
from payments;

select customers.customerNumber,customerName, payments.amount


from customers join payments
where amount> (select round(avg(amount)) from payments);

/* Find the maximum, minimum and average number of items in sale orders */

/* Find products whose buy prices are greater than the average buy price of all
productsin each product line */

/* Find sales orders whose total values are greater than 60k */

********************************************************************************

create table salesman (


salesman_id int(30) primary key,
name varchar(30),
city varchar(30),
commission decimal(3,2)
);

insert into salesman values


(5001,'James Hoog','New York',0.15),
(5002,'Nail Knite','paris',0.13),
(5005,'Pit Alex','London',0.11),
(5006,'Mc Lyon','Paris',0.14),
(5003,'Lauson Hen',null,0.12),
(5007,'Paul Adam','Rome',0.13);

create table customer (


customer_id int(30) primary key,
customer_name varchar(30),
city varchar(30),
grade int(20),
salesman_id int(30),
foreign key (salesman_id) references salesman(salesman_id)
);

insert into customer value


(3002,'Nick Rimando','New York',100,5001),
(3005,'Graham Zusi','California',200,5002),
(3001,'Brad Guzan','London',null ,null),
(3004,'Fabian Johnson','Paris',300,5006),
(3007,'Brad Davis','New York',200,5001),
(3009,'Geoff Cameron','Berlin',100,null),
(3008,'Julian Green','London',300,5002),
(3003,'Jozy Altidor','Moscow',200,5007);

create table orders(


order_no int (30) primary key,
purch_amt decimal(6,2),
order_date date,
customer_id int(30),
salesman_id int(30)
);

insert into orders values


(70001,150.5,"2016-10-05",3005,5002),
(70009,270.65,"2016-09-10",3001,null),
(70002,65.26,"2016-10-05",3002,5001),
(70004,110.5,"2016-08-17",3009,null),
(70007,948.5,"2016-09-10",3005,5002),
(70005,2400.6,"2016-07-27",3007,5001),
(70008,5760,"2016-09-10",3002,5001),
(70010,1983.43,"2016-10-10",3004,5006),
(70003,2480,"2016-10-10",3009,null),
(70012,250.45,"2016-06-27",3008,3002),
(70011,75.29,"2016-08-17",3003,5007);

select * from salesman;


select * from customer;
select * from orders;

/* Q.1 : Show all customer who purchased products from the date 17/08/2016 to
05/10/2016 except the 10/09/2016. */
select* from customer c inner join orders o
on o.customer_id = c.customer_id
where order_date between "2016-08-17" and "2016-10-05" and order_date <> "2016-09-
10" ;

/*Q.2 : Find the name and price of the cheapest buyer.*/


select customer.customer_name , min(purch_amt)
from customer inner join orders
on orders.customer_id = customer.customer_id;

/*Q3. Find the highest purchase amount ordered by the each customer on a particular
date with their ID, order date and highest purchase amount.*/
select customer.customer_id, orders.order_date , sum(orders.purch_amt) as Sum_amt
from customer inner join orders
on orders.customer_id = customer.customer_id
group by customer_id
order by Sum_amt desc;

/*Q4. Find the names of all customers along with the salesmen who works for them*/

select customer.customer_name from customer inner join salesman


on customer.salesman_id = salesman.salesman_id;

/*Q5. Display all the orders which values are greater than the average order value
for 10th October 2012*/

You might also like