Mysql Exists

The Tutorial covers on 'Mysql Exists'. The section helps you to understand an example from 'Mysql Exists'.

Mysql Exists

Mysql Exists

     

The Tutorial covers on 'Mysql Exists'. The section helps you to understand an example from 'Mysql Exists'.We create a table 'stu' and 'Lib' with required filednames and datatypes  respectively.

 

 

 

 

 

Create Table Stu

Create Table Stu(Id int,Name Varchar(15));

Create Table Lib

Create Table Lib(Id int,libno int);

Insert Values into Stu

The insert into is used to add the records or rows to the table 'Stu'.

Insert Into Stu values(1,'Ajay');
Insert Into Stu values(2,'Bhanu');
Insert Into Stu values(4,'Rakesh');
Insert Into Stu values(5,'Santosh');
Insert Into Stu values(3,'Komal');

Insert Values into Lib

The insert into statement adds the records to the table 'Lib'.

Insert Into Lib values(1,101);
Insert Into Lib values(2,102);
Insert Into Lib values(4,103);
Insert Into Lib values(5,104);
Insert Into Lib values(3,105);

Query for Exists

The Query Exists returns only those id  records or rows that exists in table 'stu' and not in table 'lib'.

select id from stu where  EXISTS (select id from lib);

Result

+------+---------+
| Id   | Name    |
+------+---------+
| 1    | Ajay    |
| 2    | Bhanu   |
| 4    | Rakesh  |
| 5    | Santosh |
| 3    | Komal   |
+------+---------+