RAMALAKSHMI K
953622104080
EXERCISE 3
1.Write a SQL query to calculate total purchase amount of all orders. Return total purchase
amount
2.Write a SQL query to find the maximum purchase amount
3.Write a SQL query to find the highest purchase amount ordered by each customer on a
particular date. Return, order date and highest purchase amount
4.Write a SQL query to find the maximum order (purchase) amount in the range 2000 - 6000
(Begin and end values are included.) by combination of each customer and order date. Return
customer id, order date and maximum purchase amount.
5.Write a SQL query to find the maximum order (purchase) amount for each customer. The
customer ID should be in the range 3002 and 3007(Begin and end values are included.).
Filter the rows for maximum order (purchase) amount is higher than 1000. Return customer
id and maximum purchase amount.
6.Write a SQL query to count the number of salespeople in a city. Return number of
salespeople.
use ramu
CREATE TABLE orders (
ord_no INT PRIMARY KEY,
purch_amt DECIMAL(10,2),
ord_date DATE,
cust_id INT,
salesman_id INT
);
exec sp_help orders
INSERT INTO orders (ord_no, purch_amt, ord_date, cust_id, salesman_id)
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),
(70010, 1983.43, '2012-10-10', 3004, 5006),
(70003, 2480.4, '2012-10-10', 3009, 5003),
(70012, 250.45, '2012-06-27', 3008, 5002),
(70011, 75.29, '2012-08-17', 3003, 5007),
(70013, 3045.6, '2012-04-25', 3002, 5001);
select *from orders
RAMALAKSHMI K
953622104080
SELECT SUM(purch_amt) AS total_purchase_amount FROM orders;
SELECT MAX(purch_amt) AS max_purchase_amount FROM orders;
SELECT ord_date, cust_id, MAX(purch_amt) AS highest_purchase_amount FROM orders
GROUP BY ord_date, cust_id;
SELECT cust_id, ord_date, MAX(purch_amt) AS max_purchase_amount FROM orders
GROUP BY cust_id, ord_date HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;
RAMALAKSHMI K
953622104080
SELECT cust_id, MAX(purch_amt) AS max_purchase_amount FROM orders WHERE
cust_id BETWEEN 3002 AND 3007 GROUP BY cust_id HAVING MAX(purch_amt) >
1000;
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
cust_name VARCHAR(255),
city VARCHAR(255),
grade INT,
salesman_id INT
);
exec sp_help customers
INSERT INTO customers (customer_id, cust_name, city, grade, salesman_id)
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),
(3001, 'Brad Guzan', 'London', NULL, 5005);
select * from customers
SELECT city, COUNT(DISTINCT salesman_id) AS number_of_salespeople FROM
customers GROUP BY city;
RAMALAKSHMI K
953622104080