Friday, January 1, 2021

EXISTS and NOT EXISTS in correlated subqueries in MySQL

 

 Exists :Table2 id exists in table1 so it will fetch data from table1


select * from table1 a where exists  (select distinct  b.Emp_id from table2 b
where a.emp_id=b.Emp_id ) ;

 

Not exists : emp_id is not exists from table1 i will return only not exists in table2 

select * from table1 a where not exists (select distinct  b.Emp_id from table2 b
where a.emp_id=b.Emp_id ) ;




No comments:

Post a Comment

How to Find the Third highest salary in mysql

  set @b:=0; set @b1:=3;   # #select distinct salary,@b:=@b+1 as Row_no from emp select  * from emp where salary=( select salary from ( sele...