Uploaded by hasancool728

SEUIS20MIT136 Assignment 2

advertisement
MIT12063
Lab sheet – 01
A.R. J Hassan
SEU/IS/20/MIT/136
01)
-- Create the Order table
CREATE TABLE Orders ( ord_no INT NOT NULL, purch_amt
DECIMAL(10,2) NOT NULL, ord_date DATE NOT NULL,
customer_id INT NOT NULL, salesman_id INT NOT NULL,
PRIMARY KEY (ord_no)
);
-- Create the Customer table CREATE TABLE Customers ( e_id
INT NOT NULL, cust_name VARCHAR(255) NOT NULL, city
VARCHAR(255) NOT NULL, grade INT NOT NULL,
salesman_id INT NOT NULL,
PRIMARY KEY (e_id)
);
-- Insert the order data into the Order table
INSERT INTO Orders (ord_no, purch_amt, ord_date, customer_id, salesman_id)
VALUES
(70001, 150.5, '2015-10-05', 3005, 5005),
(70009, 270.65, '2015-09-10', 3001, 5005),
(70002, 65.26, '2015-10-05', 3002, 5001),
(70004, 110.5, '2015-08-17', 3009, 5003),
(70007, 949.5, '2015-09-10', 3005, 5002),
(70005, 2400.6, '2015-07-27', 3007, 5001),
(70008, 5760, '2015-09-10', 3002, 5001),
(70010, 1983.43, '2015-10-10', 3004, 5006),
(70003, 2480.4, '2015-10-10', 3009, 5003),
(70022, 250.45, '2015-06-27', 3008, 5002),
(70011, 75.29, '2015-08-17', 3003, 5007),
(70013, 3045.6, '2015-04-25', 3002, 5001);
-- Insert the customer data into the Customer table
INSERT INTO Customers (e_id, cust_name, city, grade, salesman_id)
VALUES
(3002, 'Wijitha silva', 'Kandy', 200, 5001),
(3005, 'Walter Dip', 'Fandy', 500, 5005),
(3001, 'John Palitha', 'Gampola', 300, 5001),
(3004, 'We Senerath Damani oysa', 'Colombo', 100, 5006),
(3006, 'Radha John', 'Colombo', 9005, 5003),
(3003, 'Odaya Bandara', 'Gampiha', 500, 5007);
02)
SELECT AVG (purchase amount) as ‘Average purchase amount' From order
03)
SELECT SalesmanID , count (Distinct Customer Id) As ‘num of customer'
FROM order
GROUP by SalesmanID
04)
SELECT COUNT (*) As num.Customer grade
FROM customer
WHERE grade Is not null;
05)
SELECT Max(purch.amt)
FROM order;
06)
SELECT Max (purch..amt),customer_id,ord_data.
FROM order
GGROUP by customer_id,ord_date;
07)
WITH DailyMaxPurchase AS (
customer_id,
SELECT
order_date,
MAX(purchase_amount) AS max_purchase_amount
FROM
orders
GROUP BY
customer_id, order_date
HAVING
MAX(purchase_amount) > 2000
)
SELECT
o.customer_id,
dmp.order_date,
dmp.max_purchase_amount
FROM
DailyMaxPurchase dmp JOIN
orders o
ON
dmp.customer_id = o.customer_id
AND dmp.order_date = o.order_date
AND dmp.max_purchase_amount = o.purchase_amount;
08)
SELECT
customer_id,
MAX(purchase_amount) AS highest_purchase_amount FROM
orders
WHERE
customer_id BETWEEN 3002 AND 3007 GROUP BY
customer_id;
09)
SELECT COUNT(*) AS total_orders
FROM orders
WHERE DATE(order_date) = '2015-08-17';
10)
SELECT
o.order_date,
s.salesman_id,
COUNT(o.order_id) AS orders_registered
FROM
salesmen s
LEFT JOIN
orders o ON s.salesman_id = o.salesman_id
GROUP BY
o.order_date, s.salesman_id
ORDER BY
o.order_date, s.salesman_id;
11)
SELECT
c.customer_name,
s.salesman_name,
s.city
FROM
customers c JOIN
salesmen s ON c.city = s.city;
12)
SELECT
o.order_id,
o.customer_id,
o.order_date,
o.order_amount
FROM
orders o
JOIN
customers c ON o.customer_id = c.customer_id JOIN
ON c.salesman_id = s.salesman_id
WHERE
c.city <> s.city;
13)
SELECT o.order_number, c.customer_name
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id;
14)
SELECT
c.customer_name,
c.city,
s.salesman_name,
salesmen s
s.commission
FROM
customers AS c JOIN
salesmen AS s ON c.salesman_id = s.salesman_id
WHERE
s.commission BETWEEN 0.12 AND 0.14;
15)
SELECT
o.order_number,
c.customer_name,
cg.commission_rate,
(cg.commission_rate * o.order_amount) AS earned_commission
FROM
orders AS o
JOIN
customers AS c ON o.customer_id = c.customer_id JOIN
AS cg ON c.customer_id = cg.customer_id
WHERE
cg.grade > 200;
customer_grades
Download