Hi Guys you've used lots of time In and Exists in Sql and they look almost same
but there is difference between both of these.
Suppose you have two tables as follows :
a)tbl_employee(id,emp_name,comp_id)
b)tbl_company(id,comp_name)
Now if you fire following query
select * from tbl_employee where comp_id in (select id from tbl_company)
by the way this query is executes in the following way :
select * from tbl_employee,(select distinct(id) from tbl_company) tbl_company
where tbl_employee.comp_id=tbl_company.id
What's the scenario Here:
a)the indexed result from tbl_employee,which is fast in terms of time.
b)a full scan through tbl_company for and selecting distinct of id from this,
for matching records in where condition.
Where's the IN is suitable:
well, in a scenario where your subquery having less record then IN suitable in that
case,because you main query have indexed and only need to have matched with subquery
while subquery need to have a full scan for each matching id with where condition.
Now if you fire following query
select * from tbl_employee where exists(select null from tbl_company where id=tbl_employee.comp_id)
query is executes in the following way :
for emp in ( select * from tbl_employee )
loop
if ( exists ( select null from tbl_company where id = emp.comp_id )
then
output the record
end if
end loop
What's the scenario Here:
a)indexed on tbl_employee which gives emp rowset.
b)for each emp rowset we need to check weather if exists is true or false if so output the record.
Where's the Exists is suitable:
well, in a scenario where your subquery having Huge records then Exists suitable ,because you only need to check with subquery that if the above record id exist or not and a full scan is made with main query for each rowset.
but there is difference between both of these.
Suppose you have two tables as follows :
a)tbl_employee(id,emp_name,comp_id)
b)tbl_company(id,comp_name)
Now if you fire following query
select * from tbl_employee where comp_id in (select id from tbl_company)
by the way this query is executes in the following way :
select * from tbl_employee,(select distinct(id) from tbl_company) tbl_company
where tbl_employee.comp_id=tbl_company.id
What's the scenario Here:
a)the indexed result from tbl_employee,which is fast in terms of time.
b)a full scan through tbl_company for and selecting distinct of id from this,
for matching records in where condition.
Where's the IN is suitable:
well, in a scenario where your subquery having less record then IN suitable in that
case,because you main query have indexed and only need to have matched with subquery
while subquery need to have a full scan for each matching id with where condition.
Now if you fire following query
select * from tbl_employee where exists(select null from tbl_company where id=tbl_employee.comp_id)
query is executes in the following way :
for emp in ( select * from tbl_employee )
loop
if ( exists ( select null from tbl_company where id = emp.comp_id )
then
output the record
end if
end loop
What's the scenario Here:
a)indexed on tbl_employee which gives emp rowset.
b)for each emp rowset we need to check weather if exists is true or false if so output the record.
Where's the Exists is suitable:
well, in a scenario where your subquery having Huge records then Exists suitable ,because you only need to check with subquery that if the above record id exist or not and a full scan is made with main query for each rowset.
