DBMS Lab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

EXPERIMENT 1

AIM : To Create Tables and Insert Data into them.


THEORY AND CONCEPTS :
A database most often contains one or more tables.
The SQL CREATE TABLE statement is used to create a new table. -
INSERT INTO Syntax: It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted: insert into
table_name(column1, column2, column3, ...) values(value1, value2, value3, ...);
insert into table_name values(value1, value2, value3, ...);

Query - : Make a table client_master and insert data in that table


mysql> create table client_master(client no varchar(6), name varchar(20), city varchar(15),
state varchar(15), pincode numeric(6), bal_due numeric(8,2)); Query OK, 1 row affected
(0.03 sec)
mysql> insert into client_master values(0001, 'Ivan', 'Bombay', 'Maharastra', 400054,
15000); Query OK, 1 row affected (0.03 sec)
mysql> insert into client_master values(0002, 'Vandana', 'Madras', 'Tamil Nadu', 780001, 0);
Query OK, 1 row affected (0.03 sec)
mysql> insert into client_master values(0003, 'Pramada', 'Bombay', 'Maharastra', 400057,
5000); Query OK, 1 row affected (0.03 sec)
mysql> insert into client_master values(0004, 'Basu', 'Bombay', 'Maharastra', 400056, 0);
Query OK, 1 row affected (0.03 sec)
mysql> insert into client_master values(0005, 'Ravi', 'Delhi', 'Delhi', 100001, 2000); Query
OK, 1 row affected (0.03 sec)
mysql> insert into client_master values(0006, 'Rukmini', 'Bombay', 'Maharastra', 400050, 0);
Query OK, 1 row affected (0.04 sec)
mysql> select * from client_master;
| client_no | name | city | state | pincode | bal_due |
|1 | Ivan | Bombay | Maharastra | 400054 | 15000.00 |
|2 | Vandana | Madras | Tamil Nadu | 780001 | 0.00 |
|3 | Pramada | Bombay | Maharastra | 400057 | 5000.00 |
|4 | Basu | Bombay | Maharastra | 400056 | 0.00 |

SHIVANSH GUPTA IT2/B1 2100910130102


|5 | Ravi | Delhi | Delhi | 100001 | 2000.00 |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 |
7 rows in set (0.00 sec)

Query - : Make a table product_master and insert data in that table


mysql> create table product_master(product_no varchar(7), Description varchar(15),
profile_percent numeric(2), unit_measure varchar(7), Qty_on_hand numeric(4), Reorder_lvl
numeric(3), sell_price numeric(6), cost_price numeric(6)); Query OK, 0 rows affected (0.06
sec)
mysql> insert into product_master values('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500); Query OK, 1 row affected (0.03 sec)
mysql> insert into product_master values('P03453', 'Monitors', 6, 'piece', 10, 3, 12000,
11200); Query OK, 1 row affected (0.03 sec)
mysql> insert into product_master values('P06734', 'Mouse', 5, 'piece', 20, 5, 1050, 500);
Query OK, 1 row affected (0.03 sec)
mysql> insert into product_master values('P07865', '1.22floppies', 5, 'piece', 100, 20, 525,
500); Query OK, 1 row affected (0.04 sec)
mysql> select * from product_master;
| product_no | Description | profile_percent | unit_measure | Qty_on_hand | Reorder_lvl |
sell_price | cost_price |
| P00001 | 1.44floppies | 5 | piece | 100 | 20 | 525 | 500 |
| P03453 | Monitors | 6 | piece | 10 | 3| 12000 | 11200 |
| P06734 | Mouse | 5 | piece | 20 | 5| 1050 | 500 |
| P07865 | 1.22floppies | 5 | piece | 100 | 20 | 525 | 500 |
| P07868 | Keyboards | 2 | piece | 10 | 3| 3150 | 3050 |
| P07885 | CD Drive | 2.5 | piece | 10 | 3| 5250 | 5100|
| P07965 | 540 HDD | 4 | piece | 10 | 3| 525 | 8400 | 8000 |
| P07975 | 1.44 Drive | 5 | piece | 10 | 3| 1050 | 1000 |
| P08865 | 1.22 Drive | 5 | piece | 2| 3| 1050 | 1000 |

4 rows in set (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 2
AIM : To use select command and access data from tables.
THEORY AND CONCEPTS :
The SQL SELECT Statement is used to select data from a database.
The SQL WHERE Clause is used to filter records.The WHERE clause is used to extract
only those records that fulfill a specified condition.
The AND operator displays a record if all the conditions separated by AND is TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The SQL UPDATE Statement: The UPDATE statement is used to modify the existing
records in a table.
The SQL DELETE Statement: The DELETE statement is used to delete existing records in
a table.

Query : Find out the names of all the clients


mysql> select name from client_master;
| name |
| Ivan |
| Vandana |
| Pramada |
| Basu |
| Ravi |
| Rukmini | 6 rows in set (0.00 sec)

Query : Retrieve the list of names and cities of all the cities
mysql> select name, city from client_master;
| name | city |
| Ivan | Bombay |
| Vandana | Madras |
| Pramada | Bombay |
| Basu | Bombay |
| Ravi | Delhi |
| Rukmini | Bombay | 6 rows in set (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


Query : List various product available from product_master table.
mysql> select Description from product_master;
| Description |
| 1.44floppies |
| Monitors |
| Mouse |
| 1.22floppies |
| Keyboards |
| CD Drive |
| 540 HDD |
| 1.44 Drive |
| 1.22 Drive | 9 rows in set (0.00 sec)

Query : List all the cities located in Bombay


mysql> select name from client_master where city="Bombay";
| name |
| Ivan |
| Pramada |
| Basu |
| Rukmini | 4 rows in set (0.00 sec)

Query : Find the product with description as “1.44Drive” and “1.22Drive”


mysql> select * from product_master where description in ("1.44Floppies", "1.22Floppies");
| product_no | Description | profile_percent | unit_measure | Qty_on_hand | Reorder_lvl |
sell_price | cost_price |
| P00001 | 1.44floppies | 5 | piece | 100 | 20 | 525 | 500 |
| P07865 | 1.22floppies | 5 | piece | 100 | 20 | 525 | 500 |
2 rows in set (0.00 sec)

Query : Find list of all clients who stay in city “Bombay” or “Delhi” or
“Madras”
mysql> select name from client_master where city in ("Delhi","Bombay","Madras");
| name |

SHIVANSH GUPTA IT2/B1 2100910130102


| Ivan |
| Vandana |
| Pramada |
| Basu |
| Ravi |
| Rukmini | 6 rows in set (0.00 sec)

Query : Find the product whose Selling Price >2000 or <=5000


mysql> select description from product_master where sell_price >2000 || sell_price <=5000;
+--------------+
| description |
| 1.44floppies |
| Mouse |
| 1.22floppies |
| Keyboards |
| 1.44 Drive |
| 1.22 Drive | 6 rows in set (0.00 sec)

Query : List name, city, state of clients not in state of “Maharastra”


mysql> select * from client_master where state!="Maharastra";
| client_no | name | city | state | pincode | bal_due |
|2 | Vandana | Madras | Tamil Nadu | 780001 | 0.00 |
|5 | Ravi | Delhi | Delhi | 100001 | 2000.00 | 2 rows in set (0.00 sec)

Query : Find out clients who stay in city whose second letter is “a”
mysql> select city from client_master where city like "_a%";
| city |
| Madras | 1 row in set (0.00 sec)
mysql> select name from client_master where name like "_a%";
| name |
| Vandana |
| Basu |
| Ravi | 3 rows in set (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 3
AIM : To understand and use
THEORY AND CONCEPTS :
Transaction:
Atomicity: The atomicity aspect of the ACID model mainly involves InnoDB transactions.
Related MySQL features include :
Autocommit setting: The autocommit() function turns on or off auto-committing database
modifications.
COMMIT statement: The commit() function commits the current transaction for the specified
database connection. ( commit checkpoints )
ROLLBACK statement: The rollback() function rolls back the current transaction for the
specified database connection.

Query: Change the selling price of “1.44floppies” to Rs.1150.00/-


mysql> update product_master set sell_price="1150" where Description="1.44floppies";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Query: Delete the record with client 0001 from client_master table.
mysql> delete from client_master where client_no=0001;
Query OK, 1 row affected (0.00 sec)

Query: Change the city of client_no=0.0005 to Bombay.


mysql> update client_master set city="Bombay" where client_no=0005;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings:

Query: Change the bal_due of client_no=0.002 to 1000.


mysql> update client_master set bal_due=1000 where client_no=0002;

Query OK, 1 row affected (0.00 sec)


Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from client_master;
| client_no | name | city | state | pincode | bal_due |
|2 | Vandana | Madras | Tamil Nadu | 780001 | 1000.00 |

SHIVANSH GUPTA IT2/B1 2100910130102


|3 | Pramada | Bombay | Maharastra | 400057 | 5000.00 |
|4 | Basu | Bombay | Maharastra | 400056 | 0.00 |
|5 | Ravi | Bombay | Delhi | 100001 | 2000.00 |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 | 5 rows in set (0.00 sec)
mysql> savepoint S1;
Query OK, 0 rows affected (0.00 sec)

Query: Delete all records from the table client_master table


mysql> delete from client_master;
Query OK, 5 rows affected (0.00 sec)
mysql> select * from client_master;
Empty set (0.00 sec)
mysql> rollback to S1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from client_master;
| client_no | name | city | state | pincode | bal_due |
|2 | Vandana | Madras | Tamil Nadu | 780001 | 1000.00 |
|3 | Pramada | Bombay | Maharastra | 400057 | 5000.00 |
|4 | Basu | Bombay | Maharastra | 400056 | 0.00 |
|5 | Ravi | Bombay | Delhi | 100001 | 2000.00 |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 |
5 rows in set (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 4
AIM : To understand and implement Integrity Constraints using full DDL
commands.
THEORY AND CONCEPTS :
Primary Key: The PRIMARY KEY constraint uniquely identifies each record in a database
table.
A table can have only one primary key, which may consist of single or multiple fields.
Foreign Key: field in one table that refers to primary key of another table (REFERENTIAL
INTEGRITY CONSTRAINT)
A FOREIGN KEY is a key used to link two tables together.
The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.
Default: The DEFAULT constraint is used to provide a default value for a column.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new
record, or update a record without adding a value to this field.
mysql> create table Sales_Master(salesman_no varchar(6) primary key
check(salesman_no like 's%'),sal_name varchar(20) not null, address varchar(20) not
null,city varchar(20), pincode numeric(6),sal_amt numeric(8,2) not null check(sal_amt
>0),tgt_to_get numeric(6,2) not null check(tgt_to_get >0),ytd_sales numeric(6,2) not null
check(ytd_sales >0),remarks varchar(30)); Query OK, 0 rows affected (0.13 sec)
mysql> create table Sales_Order(S_order_no varchar(6) primary key check(S_order_no
like "O%"),
S_order_date Date,Client_no varchar(6), Salesman_no varchar(6)references
Sales_Master(Salesman_no),Dely_type char(1) check(Dely_type in ('f','p')), Billed_yn
char(1), Dely_date date check(Dely_date>=S_order_date),Order_status varchar(10)
check(Order_status in('inprocess','fulfilled','backorder','canceled'))); Query OK, 0 rows
affected (0.06 sec)

Query: Make the primary key to client_no in client_master


mysql> alter table client_master add constraint pk primary key(client_no);
Query OK, 5 rows affected (0.24 sec)
Records: 5 Duplicates: 0 Warnings: 0

SHIVANSH GUPTA IT2/B1 2100910130102


Query: Make the primary key to product_no in product_master
mysql> alter table product_master add constraint pk primary key(product_no);
Query OK, 4 rows affected (0.18 sec)
Records: 4 Duplicates: 0 Warnings: 0

Query: Add foreign key constraint in Sales_Order:- Client_no foreign


key references client_no of client_master
mysql> alter table Sales_Order add constraint fk foreign key(Client_no) references
client_master(client_no);
Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0
Query: Add foreign key constraint in Sales_Order_Details:- Product_no foreign key
refernces product_no of product_master
mysql> alter table Sales_Order_Details add constraint fk1 foreign key(Product_no)
references product_master(product_no); Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
#Alter the size of field phone_no in client_master
mysql> alter table client_master add phone_no numeric(10);
Query OK, 5 rows affected (0.20 sec) Records: 5 Duplicates: 0 Warnings: 0

Query: Alter the size of field client_no in client_master table


mysql> alter table client_master modify client_no varchar(10);
Query OK, 5 rows affected (0.21 sec)
Records: 5 Duplicates: 0 Warnings: 0

Query: Add the not null constraint in product_master table with columns
description, profit_percent, sell_price and cost_price
mysql> alter table product_master modify Description varchar(15) not null, modify
profile_percent
decimal(2,0),modify sell_price decimal(6,0),modify cost_price decimal(6,0) not null;
Query OK, 4 rows affected (0.21 sec) Records: 4 Duplicates: 0 Warnings: 0
mysql> alter table client_master add constraint pk primary key(client_no);
Query OK, 5 rows affected (0.24 sec) Records: 5 Duplicates: 0 Warnings: 0
mysql> alter table product_master add constraint pk primary key(product_no);
Query OK, 4 rows affected (0.18 sec) Records: 4 Duplicates: 0 Warnings: 0

SHIVANSH GUPTA IT2/B1 2100910130102


Query: Add a new column phone_no in the client_master table
mysql> alter table client_master add phone_no numeric(10);
Query OK, 5 rows affected (0.20 sec) Records: 5 Duplicates: 0 Warnings: 0

Query: Alter the size of client_no in client_master table


mysql> alter table client_master modify client_no varchar(10);
Query OK, 5 rows affected (0.21 sec) Records: 5 Duplicates: 0 Warnings: 0

Query: Add the not null constraint in the product_master table with the
columns description, profit_percent, sell_price and cost_price
mysql> alter table product_master modify Description varchar(15) not null, modify
profile_percent
decimal(2,0),modify sell_price decimal(6,0),modify cost_price decimal(6,0) not null;
Query OK, 4 rows affected (0.21 sec) Records: 4 Duplicates: 0 Warnings: 0

Query: Add new field State in Sales_Master


mysql> alter table Sales_Master add State varchar(20);
Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into Sales_Master values(500001,'Kiren','A/14
Worli','Bombay',400002,3000,100,50,'Good','Mah'); Query OK, 1 row affected (0.04 sec)
mysql> insert into Sales_Master values(500002,'Manish','64,
Nariman','Bombay',400002,3000,100,100,'Good','Mah'); Query OK, 1 row affected (0.03
sec)
mysql> insert into Sales_Master values(500003,'Ravi','P-7
Bandra','Bombay',400032,3000,100,100,'Good','Mah'); Query OK, 1 row affected (0.03 sec)
mysql> insert into Sales_Master values(500004,'Ashish','A/5
Juhu','Bombay',400044,3500,200,150,'Good','Mah'); Query OK, 1 row affected (0.03 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 5
AIM : To retrieve the data using the concept of Join
THEORY AND CONCEPTS :
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
Types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from
the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records
from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
SQL supports few Set operations which can be performed on the table data. These are
used to get meaningful results from data stored in the table, under different special
conditions.

Query : the order number,client name and day of week on which clients
placed their order.
mysql> select s_order_no,name,extract(day from s_order_date) from sales_order
s,client_master c where s.client_no= c.client_no;
| s_order_no | name | extract(day from s_order_date) |
| 010008 | Ravi | 24 |
| 016865 | Pramada | 18 |
| 019001 | Ivan | 12 |
| 019002 | Vandana | 25 |
| 019003 | Ivan | 3|
| 046866 | Basu | 20 |

Query: Display the month and date when the order must be delivered
and the name of the salesman to whom the order was placed.
mysql> select sal_name,extract(month from dely_date),dely_date from sales_master
p,sales_order where p.salesman_no=o.salesman_no;
| sal_name | extract(month from dely_date) | dely_date |
| kiran | 1 | 1996-01-20 |

SHIVANSH GUPTA IT2/B1 2100910130102


| kiran | 4 | 1996-04-07 |
| manish | 1 | 1996-01-27 |
| ravi | 1 | 1996-01-20 |
| ashish | 5 | 1996-05-26 |
| ashish | 5 | 1996-05-22 |

Query: Find out the names of products that have been sold to “Ivan”.
mysql> select description from sales_order s,sales_order_details d,product_master
p,client_master c where s.s_order_no=d.s_order_no and d.product_no=p.product_no and
c.client_no=s.client_no and c.name='Ivan';
| description |
| 1.44floppies |
| CD Drive |
| 540 HDD |
| 1.44floppies |
| Monitors |

Query: For each sales order display the name of the client and the
salesman.
mysql> select name,sal_name from client_master c,sales_master s,sales_order t where
s.salesman_no=t.salesman_no and c.client_no=t.client_no;
| name | sal_name |
| Ivan | kiran |
| Ivan | kiran |
| Vandana | manish |
| Pramada | ravi |
| Ravi | ashish |
| Basu | ashish |

Query :Find out the names of clients who have purchased “CD DRIVE”.
mysql>select name from client_master c,product_master p,sales_order
s,sales_order_details d where c.client_no=s.client_no and s.s_order_no=d.s_order_no and
d.product_no=p.product_no and p.description='CD Drive';

SHIVANSH GUPTA IT2/B1 2100910130102


| name |
| Ivan |

Query: List the product_no and s_order_no of customers who have


ordered less than 5 quantity of product “1.44 floppies”.
mysql> SELECT PRODUCT_MASTER.PRODUCT_NO,
SALES_ORDER_DETAILS.S_ORDER_NO FROM PRODUCT_MASTER,
SALES_ORDER_DETAILS WHERE PRODUCT_MASTER.PRODUCT_NO =
SALES_ORDER_DETAILS.PRODUCT_NO AND DESCRIPTION = "1.44 Floppies" AND
SALES_ORDER_DETAILS.QTY_ORDER <5;
| PRODUCT_NO | S_ORDER_NO |
| P00001 | 019001 |
| P00001 | 019003 |

Query:Display all clients and the salesman in the city of Bombay.


mysql>select name ,client_no,salesman_no,sal_name from client_master c.sales_master
where c.city=T.city and c.city=’Bombay’;
| NAME | SAL_NAME |
| Ivan | KIRAN |
| Ivan | KIRAN |
| Vandana | MANISH |
| Basu | MANISH |
| Pramada | RAVI |
| Ravi | ASHISH |

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 6
Aim: To use aggregate functions in SQL.
Theory and Concepts:
By definition, an aggregate function performs a calculation on a set of values and returns a
single value. Often, aggregate functions are accompanied by the GROUP BY clause of the
SELECT statement.
MySQL provides many aggregate functions that include AVG, COUNT, SUM, MIN, MAX,
etc. An aggregate function ignores NULL values when it performs calculation except for the
COUNT function.
AVG Function:
The AVG function calculates the average value of a set of values. It ignores NULL values in
the calculation.

Query:. Count the total no of orders.


mysql> select count(s_order_no) from sales_order;
| count(s_order_no) |
| 6|

Query: Calculate the average cost price of all the products.


mysql> select avg(cost_price) from product_master;
| avg(cost_price) |
| 3427.7778 |

Query:. Calculate the minimum sale price of the products. mysql> select
min(sell_price) from product_master;
| min(sell_price) |
| 525 |

Query: Determine the max and min cost price. Rename the title as ‘max
price’ and ‘min price’ .
mysql> select min(cost_price) as 'min price',max(cost_price) as 'max price' from
product_master;
| min price | max price |
| 500 | 11200 |

SHIVANSH GUPTA IT2/B1 2100910130102


Q5. Count the no of products having price greater than or equal to 1500.
mysql> select count(product_no) from product_master where sell_price>1500;
| count(product_no) |
| 4|

Query: Find out the product name and their quantities to be delivered.
mysql> select description,qty_order from product_master pm , sales_order_details sod
where pm.product_no = sod.product_no group by(description);
| description | qty_order |
| 1.44 Drive | 1|
| 1.44floppies | 4|
| CD Drive | 2|
| Keyboards | 3|
| Monitors | 2|
| Mouse | 1|

Query:. Find the product no and their quantities for orders placed by
client_no ‘0001’ and ‘0002’.
mysql> select product_no,qty_order from sales_order so, sales_order_details sod where
sod.s_order_no = so.s_order_no and (client_no='0001' or client_no = '0002');
| product_no | qty_order |
| p07885 | 2|
| p07965 | 2|
| p03453 | 2|

Query:. Find the product no and quantities for orders placed by ‘


Vandana ’ and ‘ Ivan ’.
mysql> select sod.product_no , sod.qty_order from client_master cm , sales_order so ,
sales_order_details sod where so.s_order_no = sod.s_order_no and cm.client_no =
so.client_no and cm.Name in ('Ivan','Vandana');
| product_no | qty_order |
| p07885 | 2|
| p07965 | 2|

SHIVANSH GUPTA IT2/B1 2100910130102


| p03453 | 2|

Query:. Find order no , client no. , and salesman no. where more than
one salesman has received a client.
mysql> select s_order_no , client_no , salesman_no from sales_order group
by(salesman_no) having(count(client_no)>1);
| s_order_no | client_no | salesman_no |
| o19002 | 0002 | 500001 |
| o19001 | 0001 | 500002 |

Query: Print the description and total quantity sold for each product.
mysql> select description , qty_disp from product_master pm , sales_order_details sod
where pm.product_no = sod.product_no;
| description | qty_disp |
| 1.44floppies | 4|
| Monitors | 2 | | Mouse | 1|
| Keyboards | 3|
| CD Drive | 1|
| 1.44 Drive | 0|

Query: Find the value of each product sold.


mysql> select description, sell_price from product_master group by (description);
| description | sell_price |
| 1.22 Drive | 1050 | | 1.22floppies | 525 | | 1.44 Drive | 1050 | | 1.44floppies |
525 |
| 540 HDD | 8400 |
| CD Drive | 5250 |
| keyboards | 3150 |
| Monitors | 12000 |
| Mouse | 1050 |

SHIVANSH GUPTA IT2/B1 2100910130102


Query: Select product_no, qty_order for each product. mysql> select
product_no, qty_order from sales_order_details;
| product_no | qty_order |
| p00001 | 4|
| p03453 | 2|
| p06734 | 1|
| p07868 | 3|
| p07885 | 2|
| p07965 | 2|
| p07975 | 1|

Query: Select product_no, description and qty_order for each product.


mysql> select description, pm.product_no, qty_order from product_master pm,
sales_order_details sod where pm.product_no = sod.product_no;
| description | product_no | qty_order |
| 1.44floppies | P00001 | 4|
| Monitors | P03453 | 2|
| Mouse | P06734 | 1|
| CD Drive | P07885 | 2|
| keyboards | P07868 | 3|
| 540 HDD | P07965 | 2|
| 1.44 Drive | P07975 | 1|

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 7
Aim: To write nested subqueries and correlated subquerries.
Theory and Concepts:
A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to
further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are mainly two types of nested queries:
Independent Nested Queries: In independent nested queries, query execution starts from
innermost query to outermost queries. The execution of inner query is independent of outer
query, but the result of inner query is used in execution of outer query. Various operators
like IN, NOT IN, ANY, ALL etc are used in writing independent nested queries. Co-related
Nested Queries: In co-related nested queries, the output of inner query depends on the row
which is being currently executed in outer query.
Subqueries with the INSERT Statement
Subqueries also can be used with INSERT statements. The INSERT statement uses the
data returned from the subquery to insert into another table. The selected data in the
subquery can be modified with any of the character, date or number functions.
Subqueries with the UPDATE Statement
-
The subquery can be used in conjunction with the UPDATE statement. Either single or
multiple columns in a table can be updated when using a subquery with the UPDATE
statement.
Subqueries with the DELETE Statement
The subquery can be used in conjunction with the DELETE statement.
mysql> select * from client_master;
+-----------+---------+--------+------------+---------+---------+----------+
| client_no | name | city | state | pincode | bal_due | phone_no |
+-----------+---------+--------+------------+---------+---------+----------+
|2 | Vandana | Madras | Tamil Nadu | 780001 | 1000.00 | NULL |
|3 | Pramada | Bombay | Maharastra | 400057 | 5000.00 | NULL |

SHIVANSH GUPTA IT2/B1 2100910130102


|4 | Basu | Bombay | Maharastra | 400056 | 0.00 | NULL |
|5 | Ravi | Bombay | Delhi | 100001 | 2000.00 | NULL |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 | NULL |
+-----------+---------+--------+------------+---------+---------+----------+
5 rows in set (0.00 sec)

Query: Display the customer name, address, city and pincode for the
clients who live in the same city as ‘Basu’. Basu’s details should not be
displayed
mysql> select * from client_master where city=(select city from client_master where
name='Basu');
| client_no | name | city | state | pincode | bal_due | phone_no |
|3 | Pramada | Bombay | Maharastra | 400057 | 5000.00 | NULL |
|4 | Basu | Bombay | Maharastra | 400056 | 0.00 | NULL |
|5 | Ravi | Bombay | Delhi | 100001 | 2000.00 | NULL |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 | NULL |
4 rows in set (0.00 sec)

Query: Display the details of all the customers who live in the same city
and has the same balance due as ‘Basu’. Basu’s details should not be
displayed
mysql> select name from client_master where city=(select city from client_master where
name='Basu') and bal_due=(select bal_due from client_master where name='Basu');
| client_no | name | city | state | pincode | bal_due | phone_no |
|6 | Rukmini | Bombay | Maharastra | 400050 | 0.00 | NULL |
1 row in set (0.00 sec)
mysql> select * from product_master;
| product_no | Description | profile_percent | unit_measure | Qty_on_hand | Reorder_lvl |
sell_price | cost_price |
| P00001 | 1.44floppies | 5 | piece | 100 | 20 | 1150 | 500|
| P03453 | Monitors | 6 | piece | 10 | 3| 12000 | 11200 |
| P06734 | Mouse | 5 | piece | 20 | 5| 1050 | 500 |
| P07865 | 1.22floppies | 5 | piece | 100 | 20 | 525 | 500 |

SHIVANSH GUPTA IT2/B1 2100910130102


4 rows in set (0.00 sec)

Query: Display the names of client who have placed orders worth
Rs.10000 or more
mysql> select name from client_master,Sales_Order_Details, Sales_Order where
product_rate >10000 and client_master.client_no= Sales_Order.client_no and
Sales_Order.S_order_no=Sales_Order_Details.s_order_no;
Empty set (0.00 sec)

Query: Display the client names who have placed orders before any
orders placed by client_no ‘0003’
mysql> select c.name from client_master c, Sales_Order s where c.client_no=s.client_no
and s_order_date<(select s_order_date from Sales_Order where client_no=0003);
Empty set (0.00 sec)
mysql> select * from Sales_Master;
| salesman_no | sal_name | address | city | pincode | sal_amt | tgt_to_get | ytd_sales |
remarks | State |
| 500001 | Kiren | A/14 Worli | Bombay | 400002 | 3000.00 | 100.00 | 50.00 |
Good | Mah |
| 500002 | Manish | 64, Nariman | Bombay | 400002 | 3000.00 | 100.00 | 100.00 |
Good | Mah |
| 500003 | Ravi | P-7 Bandra | Bombay | 400032 | 3000.00 | 100.00 | 100.00 |
Good | Mah |
| 500004 | Ashish | A/5 Juhu | Bombay | 400044 | 3500.00 | 200.00 | 150.00 |
Good | Mah |
4 rows in set (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102


EXPERIMENT 8
Aim: To create i. Indexes ii. Views
Theory and Concepts:
SQL CREATE INDEX Statement
The CREATE INDEX statement is used to create indexes in tables.
Indexes are used to retrieve data from the database very fast. The users cannot see the
indexes, they are just used to speed up searches/queries.
CREATE INDEX Syntax
Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
SQL CREATE VIEW Statement
CREATE VIEW Syntax
CREATE VIEW view_name AS SELECT column1, column2, ...
FROM table_name WHERE condition;

Query: Create an index on the table client_master, field client_no


mysql> create index field on client_master(client_no);
Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0

Query: Create an index on the sales_order, field s_order_no mysql>


create index field on Sales_Order(s_order_no);
Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0
| remarks | varchar(30) | YES | | NULL | |
| State | varchar(20) | YES | | NULL | | 10 rows in set (0.00 sec)

Query: Create an unique index on the table salesman_master, field


salesman_no
mysql> create unique index fields on Sales_Master(salesman_no);
Query OK, 0 rows affected (0.14 sec) Records: 0 Duplicates: 0 Warnings: 0
| product_no | varchar(6) | NO | PRI | | |
| qty_order | decimal(8,0) | YES | | NULL | |

SHIVANSH GUPTA IT2/B1 2100910130102


| qty_disp | decimal(8,0) | YES | | NULL | |
| product_rate | decimal(10,2) | YES | | NULL | |
5 rows in set (0.00 sec)

Query: Create an composite index on the sales_order_details table for


the column s_order_no and product_no.
mysql> create index field on Sales_Order_Details(s_order_no, product_no);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

Query: Create view on salesman_master whose sal_amt <3500


mysql> create view V as select * from Sales_Master where sal_amt<3500; Query OK, 0
rows affected (0.03 sec)

Query: Create a view client_view on client_master and rename the


columns as name, new_city, pincode_new, state_new
mysql> create view client_view(name_new,city_new,pincode_new,state_new) as select
name,city,pincode,state from client_master; Query OK, 0 rows affected (0.03 sec)

Query: Select the client names from client_view who lives in city
‘Bombay’ mysql> select name_new from client_view where
city_new="Bombay";
| name_new |
| Pramada |
| Basu |
| Ravi |
| Rukmini |
4 rows in set (0.00 sec)

Query: Drop the view client_view


mysql> drop view client_view;
Query OK, 0 rows affected (0.00 sec)

SHIVANSH GUPTA IT2/B1 2100910130102

You might also like