Professional Documents
Culture Documents
SUB QUERIES
Aim :
To create a MySQL program that implements the following
queries.
Syntax :
IN : select * from <table 1> where <col. name> in (select
<col. name> from <table 2>);
NOT IN : select * from <table 1> where <col. name> not in
(select <col. name> from <table 2>);
ALL : select * from <table 1> where <col. name> = all
select <col. name> from <table 2>;
ANY : select * from <table 1> where <col. name> = any
select <col. name> from <table 2>;
EXISTS : select * from <table 1> where exists (select * from
<table 2>);
NOT EXISTS : select * from <table 1> where not exists
(select * from <table 2>);
UNION : (select * from <table 1>) union (select * from
<table 2>);
QURIES:
mysql> select * from marklist;
+-------+-------+-------+-------+-------+
| name | mark1 | mark2 | mark3 | total |
+-------+-------+-------+-------+-------+
| anand |
| sagar |
| ravi |
80 |
90 |
90 |
80 |
90 |
90 |
90 | NULL |
90 | 240 |
90 | 240 |
+-------+-------+-------+-------+-------+
80 |
80 | NULL |
| ravl | 78 |
80 |
66 | NULL |
| raj | 70 |
70 |
70 | NULL |
| depak | 80 |
60 |
80 | NULL |
| ravi | 80 |
80 |
80 | NULL |
| rat | 78 |
80 |
66 | NULL |
| eaj | 70 |
70 |
70 | NULL |
| anand | 80 |
60 |
80 | NULL |
+-------+------+-------+-------+-------+
8 rows in set (0.01 sec)
mysql> select * from marklist1 where name in(select name from marklist);
+-------+------+-------+-------+-------+
| name | mak1 | mark2 | mark3 | total |
+-------+------+-------+-------+-------+
| ravi | 80 |
| anand | 80 |
80 |
80 | NULL |
60 |
80 | NULL |
+-------+------+-------+-------+-------+
2 rows in set (0.00 sec)
mysql> select * from marklist1 where name not in(select name from
marklist);
+-------+------+-------+-------+-------+
| name | mak1 | mark2 | mark3 | total |
+-------+------+-------+-------+-------+
| ram | 80 |
80 |
80 | NULL |
| ravl | 78 |
80 |
66 | NULL |
| raj | 70 |
70 |
70 | NULL |
| depak | 80 |
60 |
80 | NULL |
| rat | 78 |
80 |
66 | NULL |
| eaj | 70 |
70 |
70 | NULL |
+-------+------+-------+-------+-------+
6 rows in set (0.00 sec)
80 |
80 | NULL |
60 |
80 | NULL |
+-------+------+-------+-------+-------+
2 rows in set (0.00 sec)
mysql> select * from marklist1 where name= all(select name from marklist);
Empty set (0.00 sec)
80 |
| anand | 80 |
80 | NULL |
60 |
80 | NULL |
+-------+------+-------+-------+-------+
2 rows in set (0.00 sec)
mysql> select * from marklist1 where not exists(select * from marklist where
marklist.name=marklist1.name);
+-------+------+-------+-------+-------+
| name | mak1 | mark2 | mark3 | total |
+-------+------+-------+-------+-------+
| ram | 80 |
80 |
80 | NULL |
| ravl | 78 |
80 |
66 | NULL |
| raj | 70 |
70 |
70 | NULL |
| depak | 80 |
60 |
80 | NULL |
| rat | 78 |
80 |
66 | NULL |
| eaj | 70 |
70 |
70 | NULL |
+-------+------+-------+-------+-------+
6 rows in set (0.00 sec)
80 |
90 |
90 |
80 |
90 |
90 |
90 | NULL |
90 | 240 |
90 | 240 |
| ram |
80 |
80 |
80 | NULL |
| ravl |
78 |
80 |
66 | NULL |
| raj |
70 |
70 |
70 | NULL |
| depak |
80 |
60 |
80 | NULL |
| ravi |
80 |
80 |
80 | NULL |
| rat |
78 |
80 |
66 | NULL |
| eaj |
70 |
70 |
70 | NULL |
| anand |
80 |
60 |
80 | NULL |
+-------+-------+-------+-------+-------+
11 rows in set (0.01 sec)