Jay Patel: 60004210114 SE
Database Management System
Experiment No-6
Topic: College Management System
Aim: Nested queries and Complex queries.
Theory: A Subquery or Inner query or 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.
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
1) Finding the principals who are in principal table and also in the staff
table
mysql> select name
-> from principal
-> where p_no in (select staff_no from staff);
2) Finding the principals who are in principal table but not in staff table
mysql> select name
-> from principal
-> where p_no not in (select staff_no from staff);
3) Finding all the staff who have greater salary than at least one staff of
VJTI college
mysql> select *
-> from staff
-> where salary > some (select salary
-> from staff
-> where college_name = 'VJTI');
4) Finding all the staff who have salary greater than or equal to that of
each staff of the VJTI college
mysql> select *
-> from staff
-> where salary >= all (select salary
-> from staff
-> where college_name = 'VJTI');
5) Finding the principals who appear in the principal table and also in the
staff table using EXISTS
mysql> select *
-> from principal
-> where exists(select *
-> from staff
-> where principal.p_no = staff.staff_no);
6) Finding the principals who appear in the principal table but not in the
staff table using NOT EXISTS
mysql> select *
-> from principal
-> where not exists(select *
-> from staff
-> where principal.p_no = staff.staff_no);
7) Creating the table of the principal table by copying all the records of
the principal table in to the principal_backup table
mysql> create table principal_backup(
-> age int not null,
-> qualification varchar(100),
-> name varchar(100),
-> p_no int primary key);
mysql> insert into principal_backup
-> select * from principal;
Conclusion: Database is searched for various nested and correlated queries.