Professional Documents
Culture Documents
Submitted by:
Vidushi Tickoo
18001003131
CE52
Experiment-1
Creating table in database and entering values.
CLIENT MASTER
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES ('C00002', 'Mamta
Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES ('C00003', 'Chhaya
Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES
('C00004', 'Ashwini Joshi', 'Bangalore', 560001, 'Karnataka', 0);
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES ('C00005', 'Hansel
Colaco', 'Mumbai', 400060, 'Maharashtra', 2000);
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);
PRODUCT MASTER
SALESMAN MASTER
INSERT INTO salesman_master (salesmanno, salesmanname, address1, address2, city, pincode, state, salamt,
tgttoget, ytdsales, remarks) VALUES ('S00001', 'Aman',
'A/14', 'Worli', 'Mumbai', 400002, 'Maharashtra', 3000, 100, 50, 'Good');
INSERT INTO salesman_master (salesmanno, salesmanname, address1, address2, city, pincode, state, salamt,
tgttoget, ytdsales, remarks) VALUES ('S00002', 'Omkar',
'65', 'Nariman', 'Mumbai', 400001, 'Maharashtra', 3000, 200, 100, 'Good');
INSERT INTO salesman_master (salesmanno, salesmanname, address1, address2, city, pincode, state, salamt,
tgttoget, ytdsales, remarks) VALUES ('S00003', 'Raj',
'P-7', 'Bandra', 'Mumbai', 400032, 'Maharashtra', 3000, 200, 100, 'Good');
INSERT INTO salesman_master (salesmanno, salesmanname, address1, address2, city, pincode, state, salamt,
tgttoget, ytdsales, remarks) VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Mumbai', 400044, 'Maharashtra', 3500,
200, 150, 'Good');
Experiment-2
ALTER TABLE, PRIMARY KEY, NOT NULL and FOREIGN KEY REFERENCES.
CLIENT MASTER
PRODUCT MASTER
ALTER TABLE product_master MODIFY productno VARCHAR(6) PRIMARY KEY;
ALTER TABLE product_master MODIFY description VARCHAR(15) NOT NULL;
ALTER TABLE product_master MODIFY profitpercent NUMERIC(4,2) NOT NULL;
ALTER TABLE product_master MODIFY unitmeasure VARCHAR(10) NOT NULL;
ALTER TABLE product_master MODIFY qtyonhand NUMERIC(8) NOT NULL;
ALTER TABLE product_master MODIFY reorderlvl NUMERIC(8) NOT NULL;
ALTER TABLE product_master MODIFY sellprice NUMERIC(8,2) NOT NULL;
ALTER TABLE product_master MODIFY costprice NUMERIC(8,2) NOT NULL;
SALESMAN MASTER
ALTER TABLE salesman_master MODIFY salesmanno VARCHAR(6) PRIMARY KEY;
ALTER TABLE salesman_master MODIFY salesmanname VARCHAR(20) NOT NULL;
ALTER TABLE salesman_master MODIFY address1 VARCHAR(30) NOT NULL;
ALTER TABLE salesman_master MODIFY salamt NUMERIC(8,2) NOT NULL;
ALTER TABLE salesman_master MODIFY tgttoget NUMERIC(6,2) NOT NULL;
ALTER TABLE salesman_master MODIFY ytdsales NUMERIC(6,2) NOT NULL;
SALES ORDER
CREATE TABLE sales_order (orderno VARCHAR(6) PRIMARY KEY, clientno VARCHAR(6), orderdate
DATE NOT NULL, delyaddr VARCHAR(25), salesmanno varchar(6), delytype CHAR DEFAULT "F", billyn
CHAR, delydate DATE, orderstatus VARCHAR(10), FOREIGN KEY (clientno) REFERENCES
client_master(clientno), FOREIGN KEY (salesmanno)
REFERENCES salesman_master(salesmanno));
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O19001", "C00001", STR_TO_DATE("12-June-04", "%d-%M-%y"), "S00001", 'F', 'N',
STR_TO_DATE("20-July-02", "%d-%M-%y"), "In Process");
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O19002", "C00002", STR_TO_DATE("25-June-04", "%d-%M-%y"), "S00002", 'P', 'N',
STR_TO_DATE("27-June-02", "%d-%M-%y"), "Cancelled");
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O46865", "C00003",
STR_TO_DATE("18-Feb-04", "%d-%M-%y"), "S00003", 'F', 'Y',
STR_TO_DATE("20-Feb-02", "%d-%M-%y"), "Fulfilled");
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O19003", "C00001", STR_TO_DATE("03-Apr-04", "%d-%M-%y"), "S00001", 'F', 'Y',
STR_TO_DATE("07-Apr-02", "%d-%M-%y"), "Fulfilled");
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O46866", "C00004",
STR_TO_DATE("20-May-04", "%d-%M-%y"), "S00002", 'P', 'N',
STR_TO_DATE("22-May-02", "%d-%M-%y"), "Cancelled");
INSERT INTO sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn, delydate, orderstatus)
VALUES ("O19008", "C00005",
STR_TO_DATE("24-May-04", "%d-%M-%y"), "S00004", 'F', 'N',
STR_TO_DATE("26-July-02", "%d-%M-%y"), "In Process");
3. AND
SELECT * FROM sales_order_details WHERE orderno='O19001' AND productno='P00001';
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O19001 | P00001 | 4 | 4 | 525.00 | +---------+------
-----+------------+---------+-------------+
4. OR
SELECT * FROM sales_order_details WHERE orderno='O19001' OR productno='P00001';
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O19001 | P00001 | 4 | 4 | 525.00 |
| O19001 | P00002 | 2 | 1 | 8400.00 |
| O19001 | P00003 | 2 | 1 | 5250.00 |
| O19002 | P00001 | 10 | 0 | 525.00 |
| O46865 | P00001 | 10 | 10 | 525.00 |
| O19008 | P00001 | 4 | 4 | 525.00 | +---------+------
-----+------------+---------+-------------+
5. LIKE
SELECT * FROM sales_order_details WHERE productno LIKE 'P____1';
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O19001 | P00001 | 4 | 4 | 525.00 |
| O19002 | P00001 | 10 | 0 | 525.00 |
| O46865 | P00001 | 10 | 10 | 525.00 |
| O19008 | P00001 | 4 | 4 | 525.00 |
+---------+-----------+------------+---------+-------------+
6. IN and NOT IN
SELECT * FROM sales_order_details WHERE productno IN('P00008','P00009');
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O19003 | P00008 | 1 | 1 | 12000.00 |
| O46866 | P00009 | 1 | 0 | 8400.00 |
+---------+-----------+------------+---------+-------------+
SELECT * FROM sales_order_details WHERE productno NOT
IN('P00001','P00002','P00003','P00004','P00005');
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O46865 | P00006 | 4 | 4 | 1050.00 |
| O19003 | P00007 | 2 | 2 | 1050.00 |
| O19003 | P00008 | 1 | 1 | 12000.00 |
| O46866 | P00009 | 1 | 0 | 8400.00 |
| O19008 | P00007 | 5 | 3 | 1050.00 |
+---------+-----------+------------+---------+-------------+
5. BETWEEN
SELECT * FROM sales_order_details WHERE qtyordered BETWEEN 9 AND 10;
+---------+-----------+------------+---------+-------------+
| orderno | productno | qtyordered | qtydisp | productrate |
+---------+-----------+------------+---------+-------------+
| O19002 | P00001 | 10 | 0 | 525.00 |
| O46865 | P00001 | 10 | 10 | 525.00 |
+---------+-----------+------------+---------+-------------+
6. AVG()
SELECT AVG(productrate) "Average Rate" FROM sales_order_details;
+--------------+
| Average Rate |
+--------------+
| 3482.142857 |
+--------------+
7. MIN()
SELECT MIN(productrate) "Minimum Rate" FROM sales_order_details;
+--------------+
| Minimum Rate |
+--------------+
| 525.00 |
+--------------+
8. MAX()
SELECT MAX(productrate) "Maximum Rate" FROM sales_order_details;
+--------------+
| Maximum Rate |
+--------------+
| 12000.00 |
+--------------+
9. COUNT()
SELECT COUNT(*) "No. Of Records" FROM sales_order_details;
+----------------+
| No. Of Records |
+----------------+
| 14 |
+----------------+
10. SUM()
SELECT SUM(productrate) "Total Cost" FROM sales_order_details;
+------------+
| Total Cost |
+------------+
| 48750.00 |
+------------+
11. ABS()
SELECT ABS(-20) "Absolute";
+----------+
| Absolute |
+----------+
| 20 |
+----------+
12. POWER(m,n)
SELECT POWER(3,2);
+------------+
| POWER(3,2) |
+------------+
| 9 |
+------------+
13. ROUND(n[,m])
SELECT ROUND(15.19,1);
+----------------+
| ROUND(15.19,1) |
+----------------+
| 15.2 |
+----------------+
14. SQRT(n)
SELECT SQRT(2) "Square Root";
+--------------------+
| Square Root |
+--------------------+
| 1.4142135623730951 |
+--------------------+
15. EXP(n)
SELECT EXP(5) "Exponent";
+-------------------+
| Exponent |
+-------------------+
| 148.4131591025766 |
+-------------------+
16. EXTRACT()
SELECT EXTRACT(YEAR FROM SYSDATE()) "Year";
+------+
| Year |
+------+
| 2020 |
+------+
17. GREATEST()
SELECT GREATEST(4, 5, 17);
+--------------------+
| GREATEST(4, 5, 17) |
+--------------------+
| 17 |
+--------------------+
18. LEAST()
SELECT LEAST(4, 5, 17);
+-----------------+
| LEAST(4, 5, 17) |
+-----------------+
| 4 |
+-----------------+
19. MOD(m, n)
SELECT MOD(15, 7) "Mod1", MOD(15.7, 7) "Mod2";
+------+------+
| Mod1 | Mod2 |
+------+------+
| 1 | 1.7 |
+------+------+
20. TRUNC(number, [decimal_places])
SELECT TRUNC(125.815,1) "Trunc1", TRUNC(125.815, -2) "Trunc2";
ERROR 1305 (42000): FUNCTION mysql.TRUNC does not exist
21. FLOOR(n)
SELECT FLOOR(24.8);
+-------------+
| FLOOR(24.8) |
+-------------+
| 24 |
+-------------+
22. CEIL(n)
SELECT CEIL(24.8);
+------------+
| CEIL(24.8) |
+------------+
| 25 |
+------------+
Experiment-5
1. LOWER
SELECT LOWER(name) "Lower" FROM client_master;
+----------------+
| Lower |
+----------------+
| ivan bayross |
| mamta muzumdar |
| chhaya bankar |
| ashwini joshi |
| hansel colaco |
| deepak sharma |
+----------------+
2. INITCAP
SELECT INITCAP(name) "Title Case" FROM client_master;
ERROR 1305 (42000): FUNCTION mysql.INITCAP does not exist
3. UPPER
SELECT UPPER(name) "Capitalised" FROM client_master;
+----------------+
| Capitalised |
+----------------+
| IVAN BAYROSS |
| MAMTA MUZUMDAR |
| CHHAYA BANKAR |
| ASHWINI JOSHI |
| HANSEL COLACO |
| DEEPAK SHARMA |
+----------------+
4. SUBSTR
6. COMPOSE
SELECT COMPOSE('a'||UNISTR('\0301')) "Composerd";
ERROR 1305 (42000): FUNCTION mysql.COMPOSE does not exist
7. DECOMPOSE
SELECT DECOMPOSE(COMPOSE('a'||UNISTR('\0301'))) "Decomposerd";
ERROR 1305 (42000): FUNCTION mysql.DECOMPOSE does not exist
8. INSTR
SELECT INSTR('SCT on the net','t') "inst1";
+-------+
| inst1 |
+-------+
| 3 |
+-------+
SELECT INSTR('SCT on the net', 't', 1, 2) "Instr2";
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'INSTR'
9. TRANSLATE
SELECT TRANSLATE('1sct523', '123', '7a9') "Change";
ERROR 1305 (42000): FUNCTION mysql.TRANSLATE does not exist
10. LENGTH
SELECT LENGTH(name) "Length" FROM client_master;
+--------+
| Length |
+--------+
| 12 |
| 14 |
| 13 |
| 13 |
| 13 |
| 13 |
+--------+
11. LTRIM
SELECT LTRIM(' ABC ') "LTRIM";
+----------+
| LTRIM |
+----------+
| ABC |
+----------+
12. TRIM
SELECT TRIM(' ABC ') "Trim";
+------+
| Trim |
+------+
| ABC |
+------+
SELECT TRIM(LEADING 'x' FROM 'xxxHanselxxx') "Remove Prefixes";
+-----------------+
| Remove Prefixes |
+-----------------+
| Hanselxxx |
+-----------------+
SELECT TRIM(BOTH 'x' FROM 'xxxHanselxxx') "Remove Prefixes & Suffixes";
+----------------------------+
| Remove Prefixes & Suffixes |
+----------------------------+
| Hansel |
+----------------------------+
13. LPAD
SELECT LPAD('Page 1',10,'*') "LPAD";
+------------+
| LPAD |
+------------+
| ****Page 1 |
+------------+
14. RPAD
15. VSIZE
SELECT VSIZE(name) "Size" FROM client_master;
ERROR 1305 (42000): FUNCTION mysql.VSIZE does not exist
16. TO_NUMBER
SELECT TO_NUMBER(SUBSTR('$100',2,4));
ERROR 1305 (42000): FUNCTION mysql.TO_NUMBER does not exist
SELECT TO_CHAR(delydate, 'Month DD, YYYY') "New Date Format" FROM sales_order;
ERROR 1305 (42000): FUNCTION mysql.TO_CHAR does not exist
+------------+
| TO DATE |
+------------+
| 2002-05-22 |
+------------+
21. LAST_DAY
SELECT SYSDATE(), LAST_DAY(SYSDATE()) "Last Day";
+---------------------+------------+
| SYSDATE() | Last Day |
+---------------------+------------+
| 2020-09-02 10:33:11 | 2020-09-30 |
+---------------------+------------+
b. List the clients who stay in a city whose First letter is ‘M’.
SELECT * FROM client_master WHERE city LIKE 'M%';
+----------+----------------+----------+----------+-----------+---------+-------------+----------+
| clientno | name | address1 | address2 | city | pincode | state | baldue |
+----------+----------------+----------+----------+-----------+---------+-------------+----------+
| C00001 | Ivan Bayross | NULL | NULL | Mumbai | 400054 | Maharashtra | 15000.00 |
| C00002 | Mamta Muzumdar | NULL | NULL | Madras | 780001 | Tamil Nadu | 0.00 |
| C00003 | Chhaya Bankar | NULL | NULL | Mumbai | 400057 | Maharashtra | 5000.00 |
| C00005 | Hansel Colaco | NULL | NULL | Mumbai | 400060 | Maharashtra | 2000.00 |
| C00006 | Deepak Sharma | NULL | NULL | Mangalore | 560050 | Karnataka | 0.00 |
+----------+----------------+----------+----------+-----------+---------+-------------+----------+
e. List all information from the sales_order table for order placed in the month of June.
SELECT * FROM sales_order WHERE EXTRACT(MONTH FROM
sales_order.orderdate)=6;
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+
| orderno | clientno | orderdate | delyaddr | salesmanno | delytype | billyn | delydate | orderstatus |
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+
| O19001 | C00001 | 2004-06-12 | NULL | S00001 | F | N | 2002-07-20 | In Process |
| O19002 | C00002 | 2004-06-25 | NULL | S00002 | P | N | 2002-06-27 | Cancelled |
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+
g. List products whose selling price is greater than 500 and less than or equal to 750.
SELECT * FROM product_master WHERE sellprice>500 AND sellprice<=750;
+-----------+--------------+---------------+-------------+-----------+------------+-----------+-----------+
| productno | description | profitpercent | unitmeasure | qtyonhand | reorderlvl | sellprice | costprice |
+-----------+--------------+---------------+-------------+-----------+------------+-----------+-----------+
| P00003 | Cotton Jeans | 5.00 | Piece | 100 | 20 | 600.00 | 450.00 |
| P00004 | Jeans | 5.00 | Piece | 100 | 20 | 750.00 | 500.00 |
| P00006 | Pull Overs | 2.50 | Piece | 80 | 30 | 700.00 | 450.00 |
+-----------+--------------+---------------+-------------+-----------+------------+-----------+-----------+
h. List the names, city and state of clients who are not in the state of ‘Maharashtra’.
SELECT name, city, state FROM client_master WHERE NOT state='Maharashtra';
+----------------+-----------+------------+
| name | city | state |
+----------------+-----------+------------+
| Mamta Muzumdar | Madras | Tamil Nadu |
| Ashwini Joshi | Bangalore | Karnataka |
| Deepak Sharma | Mangalore | Karnataka |
+----------------+-----------+------------+
k. Determine the maximum and minimum product prices. Rename the output as max_price
and min_price respectively.
SELECT MAX(sellprice) "max_price", MIN(sellprice) "min_price" FROM product_master;
+-----------+-----------+
| max_price | min_price |
+-----------+-----------+
| 850.00 | 300.00 |
+-----------+-----------+
l. Count the number of products having price less than or equal to 500.
SELECT COUNT(*) FROM product_master WHERE sellprice<=500;
+----------+
| COUNT(*) |
+----------+
| 5 |
+----------+
m. List all the products whose qtyonhand is less than recorder level.
SELECT * FROM product_master WHERE qtyonhand<reorderlvl;
Empty set (0.00 sec)
b.List the month (in alphabets) and date when the orders must be delivered.
SELECT MONTHNAME(delydate) "Month", delydate "Date" FROM sales_order;
+----------+------------+
| Month | Date |
+----------+------------+
| July | 2002-07-20 |
| June | 2002-06-27 |
| April | 2002-04-07 |
| July | 2002-07-26 |
| February | 2002-02-20 |
| May | 2002-05-22 |
+----------+------------+
Joins
1. Find out the products, which have been sold to ‘Ivan Bayross’
select name,product_master.product_no,description,sales_order.orderno from
client_master,product_master,sales_order,salesorder_details where client_master.name = 'Ivan
Bayross' and client_master.clientno = sales_order.clientno and sales_order.orderno =
salesorder_details.orderno and salesorder_details.product_no = product_master.product_no;
2. Find out the products and their quantities that will have to be delivered in the current
month.
select description,qtyordered,delydate from product_master,salesorder_details,sales_order where
month(sales_order.delydate)=month(sysdate()) and
product_master.product_no=salesorder_details.product_no and
sales_order.orderno=salesorder_details.orderno;
3. List the ProductNo and description of constantly sold (i.e. rapidly moving) products.
SELECT product_master.product_no,description from product_master, salesorder_details where
product_master.product_no=salesorder_details.product_no and product_rate=(select
max(product_rate) from salesorder_details);
5. List the products and orders from customers who have ordered less than 5 units of ‘Pull
Overs’.
SELECT description,sales_order.orderno from product_master,sales_order,salesorder_details
where qtyordered<5 and description='Pull Overs' and
sales_order.orderno=salesorder_details.orderno and
product_master.product_no=salesorder_details.product_no;
6. Find the products and their quantities for the orders placed by ‘Ivan Bayross’ and ‘Mamta
Muzumdar’.
select name,description,qtyordered from
client_master,product_master,sales_order,salesorder_details where name in('Ivan
Bayross','Mamta Muzumdar') and client_master.clientno = sales_order.clientno and
sales_order.orderno = salesorder_details.orderno and salesorder_details.product_no =
product_master.product_no;
7. Find the products and their quantities for the orders placed by ClientNO ‘C00001’ and
‘C00002’
select description,qtyordered from client_master,product_master,sales_order,salesorder_details
where client_master.clientno in ('C00001','C00002') and client_master.clientno =
sales_order.clientno and sales_order.orderno = salesorder_details.orderno and
salesorder_details.product_no = product_master.product_no;
Experiment-7
a. Print the description and total qty sold for each product.
select description,sum(qtyordered) from product_master,salesorder_details where
salesorder_details.product_no=product_master.product_no group by description;
c. Calculate the average qty sold for each client that has a maximum order value of 15000.00.
select name,avg(qty_disp)"average qty sold" from client_master,salesorder_details,sales_order
where client_master.clientno=sales_order.clientno and
sales_order.orderno=salesorder_details.orderno group by client_master.clientno having
MAX(qtyordered*product_rate)>1500;
d. Find out the total of all the billed orders for the month of June.
select sales_order.orderno,SUM(qtyordered*product_rate)"billed sum" from
sales_order,salesorder_details where sales_order.orderno=salesorder_details.orderno and
sales_order.billyn='Y' and month(orderdate)=6 group by sales_order.orderno;
Experiment-8
VIEWS
1. create view table_1 as select
sales_order.orderno,sales_order.orderdate,sales_order.order_status,product_no,qtyordered from
sales_order,salesorder_details where salesorder_details.orderno=sales_order.orderno;
select * from table_1;