You are on page 1of 26

DBMS LAB FILE

Submitted to: Dr Parul Tomar

Submitted by:
Vidushi Tickoo
18001003131
CE52
Experiment-1
Creating table in database and entering values.

CLIENT MASTER

CREATE TABLE client_master (clientno VARCHAR(6), name VARCHAR(20), address1 VARCHAR(30),


address2 VARCHAR(30), city VARCHAR(15), pincode NUMERIC(8), state
VARCHAR(15), baldue NUMERIC(10,2));
INSERT INTO client_master (clientno, name, city, pincode, state, baldue) VALUES ('C00001', 'Ivan Bayross',
'Mumbai', 400054, 'Maharashtra', 15000);

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

CREATE TABLE product_master (productno VARCHAR(6), description VARCHAR(15), profitpercent


NUMERIC(4,2), unitmeasure VARCHAR(10), qtyonhand NUMERIC(8), reorderlvl NUMERIC(8), sellprice
NUMERIC(8,2), costprice NUMERIC(8,2));

INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,


sellprice, costprice) VALUES ('P00001', 'T-Shirts', 5,
'Piece', 200, 50, 350, 250);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00002', 'Shirts', 6,
'Piece', 150, 50, 500, 350);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00003', 'Cotton Jeans', 5, 'Piece', 100, 20, 600, 450);

INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,


sellprice, costprice) VALUES ('P00004', 'Jeans', 5, 'Piece', 100, 20, 750, 500);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00005', 'Trousers', 2, 'Piece', 150, 50, 850, 550);

INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,


sellprice, costprice) VALUES ('P00006', 'Pull Overs', 2.5,
'Piece', 80, 30, 700, 450);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00007', 'Denim Shirts', 4, 'Piece', 100, 40, 350, 250);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00008', 'Lyera Tops', 5, 'Piece', 70, 30, 300, 175);
INSERT INTO product_master (productno, description, profitpercent, unitmeasure, qtyonhand, reorderlvl,
sellprice, costprice) VALUES ('P00009', 'Skirts', 2, 'Piece', 75, 30, 450, 300);

SALESMAN MASTER

CREATE TABLE salesman_master (salesmanno VARCHAR(6), salesmanname VARCHAR(20), address1


VARCHAR(30), address2 VARCHAR(30), city VARCHAR(20), pincode NUMERIC(8), state
VARCHAR(20), salamt NUMERIC(8,2), tgttoget NUMERIC(6,2), ytdsales
NUMERIC(6,2), remarks VARCHAR(60));

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

Accessing and editing table.


1: Retrieving records from a table.
a. Find out the names of all the clients.
SELECT name FROM client_master;
b. Retrieve the entire contents of the client_master table.
SELECT * FROM client_master;
c. Retrieve the list of names, city and the state of all the clients.
SELECT name, city, state FROM client_master;
d. List the various products available from the product_master table.
SELECT description FROM product_master;
e. List all the clients who are located in Mumbai.
SELECT * FROM client_master WHERE city='Mumbai';
f. Find the names of salesman who have a salary equal to Rs.3000.
SELECT salesmanname FROM salesman_master WHERE salamt=3000;

2: Updating records in a table.


a. Change the city of clientno 'C00005' to 'Bangalore'.
UPDATE client_master set city='Bangalore' WHERE clientno='C00005';
b. Change the baldue of clientno 'C00001' to Rs.1000.
UPDATE client_master set baldue=1000 WHERE clientno='C00001';
c. Change the cost price of 'Trousers' to Rs.950.00.
UPDATE product_master set costprice=950.00 WHERE description='Trousers';
d. Change the city of the salesman to 'Pune'.
UPDATE salesman_master set city='Pune';

3: Deleting records in a table.


a. Delete all salesman from the salesman_master whose salaries are equal to Rs.3500.
DELETE FROM salesman_master WHERE salamt=3500;
b. Delete all products from product_master where the quantity on hand is equal to 100.
DELETE FROM product_master WHERE qtyonhand=100;
c. Delete from client_master where the column state holds the value 'Tamil Nadu'.

DELETE FROM client_master WHERE state='Tamil Nadu';

4: Altering the table structure.


a. Add a column called 'telephone' of data type 'NUMERIC' and size=10 to the client_master.
ALTER TABLE client_master ADD telephone NUMERIC(10);
b. Change the size of sellprice column in product_master to 10,2.
ALTER TABLE product_master MODIFY sellprice NUMERIC(10,2);

5: Deleting the table structure along with the data.


a. Destroy the table client_master along with its data.
DROP TABLE client_master;

6: Renaming the table.


a. Change the name of the salesman_master table to sman_mast.
ALTER TABLE salesman_master RENAME to sman_mast;
Experiment-3

ALTER TABLE, PRIMARY KEY, NOT NULL and FOREIGN KEY REFERENCES.

CLIENT MASTER

ALTER TABLE client_master MODIFY clientno VARCHAR(6) PRIMARY KEY;

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");

SALES ORDER DETAILS


CREATE TABLE sales_order_details (orderno VARCHAR(6), productno VARCHAR(6), qtyordered
NUMERIC(8), qtydisp NUMERIC(8), productrate NUMERIC(10,2), FOREIGN KEY
(orderno) REFERENCES sales_order(orderno), FOREIGN KEY (productno) REFERENCES
product_master(productno));

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O19001", "P00001", 4, 4, 525);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O19001", "P00002", 2, 1, 8400);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O19001", "P00003", 2, 1, 5250);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O19002", "P00001", 10, 0, 525);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O46865", "P00004", 3, 3, 3150);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O46865", "P00005", 3, 1, 5250);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O46865", "P00001", 10, 10, 525);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O46865", "P00006", 4, 4, 1050);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O19003", "P00007", 2, 2, 1050);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O19003", "P00008", 1, 1, 12000);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O46866", "P00009", 1, 0, 8400);

INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES


("O46866", "P00005", 1, 0, 1050);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O19008", "P00001", 4, 4, 525);
INSERT INTO sales_order_details (orderno, productno, qtyordered, qtydisp, productrate) VALUES
("O19008", "P00007", 5, 3, 1050);
Experiment-4

1. ADD PRIMARY KEY


ALTER TABLE client_master ADD PRIMARY KEY (name);

2. DROPPING INTEGRITY CONSTRAINTS VIA THE ALTER TABLE


ALTER TABLE client_master DROP PRIMARY KEY;

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

SELECT SUBSTR(name,3,13) "Substring" FROM client_master;


+--------------+
| Substring |
+--------------+
| an Bayross |
| mta Muzumdar |
| haya Bankar |
| hwini Joshi |
| nsel Colaco |
| epak Sharma |
+--------------+
5. ASCII
SELECT ASCII('a') "a", ASCII('A') "A";
+----+----+
| a | A |
+----+----+
| 97 | 65 |
+----+----+

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

SELECT RPAD(name,20,'x') "RPAD" FROM client_master;


+----------------------+
| RPAD |
+----------------------+
| Ivan Bayrossxxxxxxxx |
| Mamta Muzumdarxxxxxx |
| Chhaya Bankarxxxxxxx |
| Ashwini Joshixxxxxxx |
| Hansel Colacoxxxxxxx |
| Deepak Sharmaxxxxxxx |
+----------------------+

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

17. TO_CHAR (number conversion)

SELECT TO_CHAR(17145, '$099,999') "Char";


ERROR 1305 (42000): FUNCTION mysql.TO_CHAR does not exist

18. TO_CHAR (date conversion)

SELECT TO_CHAR(delydate, 'Month DD, YYYY') "New Date Format" FROM sales_order;
ERROR 1305 (42000): FUNCTION mysql.TO_CHAR does not exist

19. TO_DATE, STR_TO_DATE


SELECT TO_DATE('25-JUN-1952 10:55 A.M.', 'DD-MON-YY HH:MI A.M.');
ERROR 1305 (42000): FUNCTION mysql.TO_DATE does not exist
SELECT STR_TO_DATE("22-May-02", "%d-%M-%y") "TO DATE";

+------------+
| TO DATE |
+------------+
| 2002-05-22 |
+------------+

20. ADD_MONTHS, ADDDATE(<date>, INTERVAL <value> <addunit>)SELECT


ADD_MONTHS(SYSDATE(), 4) "Add Months";
ERROR 1305 (42000): FUNCTION mysql.ADD_MONTHS does not exist
SELECT SYSDATE() "Current Date", ADDDATE(SYSDATE(), INTERVAL 4 MONTH) "4
MONTHS ADDED";
+---------------------+---------------------+
| Current Date | 4 MONTHS ADDED |
+---------------------+---------------------+
| 2020-09-02 10:50:03 | 2021-01-02 10:50:03 |
+---------------------+---------------------+
SELECT SYSDATE() "Current Date", ADDDATE(SYSDATE(), INTERVAL 3 HOUR) "3
HOURS ADDED";
+---------------------+---------------------+
| Current Date | 3 HOURS ADDED |
+---------------------+---------------------+
| 2020-09-02 10:51:15 | 2020-09-02 13:51:15 |
+---------------------+---------------------+

21. LAST_DAY
SELECT SYSDATE(), LAST_DAY(SYSDATE()) "Last Day";
+---------------------+------------+
| SYSDATE() | Last Day |
+---------------------+------------+
| 2020-09-02 10:33:11 | 2020-09-30 |
+---------------------+------------+

22. MONTHS_BETWEEN(d1, d2), DATEDIFF(d1, d2)


SELECT MONTHS_BETWEEN('02-FEB-2010', '02-JAN-2020') "Months";
ERROR 1305 (42000): FUNCTION mysql.MONTHS_BETWEEN does not exist
SELECT DATEDIFF('2020/02/27', '2020/02/20') "Days";
+------+
| Days |
+------+
| 7 |
+------+

23. NEXT_DAY(date, char)

SELECT NEXT_DAY('2020/09/02', 'Saturday') "Next Day";


ERROR 1305 (42000): FUNCTION mysql.NEXT_DAY does not exist

1: Perform the following computations on table data:


a. List the names of all clients having ‘a’ as the second letter in their names.
SELECT * FROM client_master WHERE name LIKE '_a%';
+----------+----------------+----------+----------+--------+---------+-------------+---------+
| clientno | name | address1 | address2 | city | pincode | state | baldue |
+----------+----------------+----------+----------+--------+---------+-------------+---------+
| C00002 | Mamta Muzumdar | NULL | NULL | Madras | 780001 | Tamil Nadu | 0.00 |
| C00005 | Hansel Colaco | NULL | NULL | Mumbai | 400060 | Maharashtra | 2000.00 |
+----------+----------------+----------+----------+--------+---------+-------------+---------+

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 |
+----------+----------------+----------+----------+-----------+---------+-------------+----------+

c. List all clients who stay in ‘Bangalore’ or ‘Mangalore’.


SELECT * FROM client_master WHERE city IN ('Bangalore','Mangalore');
+----------+---------------+----------+----------+-----------+---------+-----------+--------+
| clientno | name | address1 | address2 | city | pincode | state | baldue
| +----------+---------------+----------+----------+-----------+---------+-----------+------
--+
| C00004 | Ashwini Joshi | NULL | NULL | Bangalore | 560001 | Karnataka | 0.00 |
| C00006 | Deepak Sharma | NULL | NULL | Mangalore | 560050 | Karnataka | 0.00 |
+----------+---------------+----------+----------+-----------+---------+-----------+--------+

d. List all clients whose baldue is greater than value 10000.


SELECT * FROM client_master WHERE baldue>10000;
+----------+--------------+----------+----------+--------+---------+-------------+----------+
| clientno | name | address1 | address2 | city | pincode | state | baldue
| +----------+--------------+----------+----------+--------+---------+-------------+--------
--+
| C00001 | Ivan Bayross | NULL | NULL | Mumbai | 400054 | Maharashtra | 15000.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 |
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+

f. List the order information for clientno ‘C00001’ and ‘C00002’.


SELECT * FROM sales_order WHERE clientno='C00001' OR clientno='C00002';
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+
| orderno | clientno | orderdate | delyaddr | salesmanno | delytype | billyn | delydate | orderstatus |
+---------+----------+------------+----------+------------+----------+--------+------------+-------------+
| O19001 | C00001 | 2004-06-12 | NULL | S00001 | F | N | 2002-07-20 | In Process |
| O19003 | C00001 | 2004-04-03 | NULL | S00001 | F | Y | 2002-04-07 | Fulfilled |
| 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 |
+----------------+-----------+------------+

i. Count the total number of orders.


SELECT COUNT(*) "Total Orders" FROM sales_order;
+--------------+
| Total Orders |
+--------------+
| 6 |
+--------------+

j. Calculate the average price of all the products.


SELECT AVG(sellprice) "Average Sell Price", AVG(costprice) "Average Cost
Price" FROM product_master;
+--------------------+--------------------+
| Average Sell Price | Average Cost Price |
+--------------------+--------------------+
| 538.888889 | 363.888889 |
+--------------------+--------------------+

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)

2: Exercise on Date Manipulation:


a.List the order number and day on which clients placed their order.
SELECT orderno, DAY(orderdate) FROM sales_order;
SELECT orderno, DAY(orderdate) "Day" FROM sales_order;
+---------+------+
| orderno | Day |
+---------+------+
| O19001 | 12 |
| O19002 | 25 |
| O19003 | 3 |
| O19008 | 24 |
| O46865 | 18 |
| O46866 | 20 |
+---------+------+

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 |
+----------+------------+

c.List the orderdate in the format ‘DD-Month-YY’. e.g. 12-February-02.


SELECT DATE_FORMAT(orderdate, '%d-%M-%y') "Order Date" FROM sales_order;
+----------------+
| Order Date |
+----------------+
| 12-June-04 |
| 25-June-04 |
| 03-April-04 |
| 24-May-04 |
| 18-February-04 |
| 20-May-04 |
+----------------+

d.List the date, 15 days after today’s date.


SELECT SYSDATE() "Current Date", ADDDATE(SYSDATE(), INTERVAL 15 DAY) "15
Days Added";
+---------------------+---------------------+
| Current Date | 15 Days Added |
+---------------------+---------------------+
| 2020-09-02 16:10:19 | 2020-09-17 16:10:19 |
+---------------------+---------------------+
Experiment-6

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);

4. Find the names of clients who have purchased ‘Trousers’


Select name from client_master,product_master,sales_order,salesorder_details where
client_master.clientno=sales_order.clientno and sales_order.orderno=salesorder_details.orderno
and salesorder_details.product_no=product_master.product_no and description='Trousers';

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

Group by, Having

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;

b. Find the value of each product sold


select product_master.product_no,description,sum(product_rate*qty_disp)"value" from
product_master,salesorder_details where
salesorder_details.product_no=product_master.product_no group by
product_master.product_no,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;

2. create view products as select product_no,description,qty_on_hand from product_master;


select* from products;

3. create sales_view as select so.orderno, orderdate, orderstatus, productno, productrate, qtyordered


FROM sales_order so, sales_order_details sod where sod.orderno=so.orderno;

4. insert into sales_view(orderno, orderdate, orderstatus) values ("O99999", STR_TO_DATE("6-


May2020", "%d-%M-%Y"), "Successful");
select * from sales_view;

You might also like