Here are two example of query for the same purpose
(in this example, I want to fine salesman from same city where Patriks lives.)
select *
from salesman
where salesman.city =
( select city
from salesman
where salesman_name='Patriks'
);
and
select s1.*
from salesman s1,salesman s2
where s1.city=s2.city
and s2.salesman_name='Patriks';
Which is better or efficient and why?
(I know this is small example, but I want to figure out, which will be good in complex situation, and for big database.)
As a general rule of thumb:
If you use a subquery, you force Oracle to use a certain execution path (ie it must execute the sub-query before it can execute the outer query)
If you use a join, Oracle is free to pick whichever it considers to be the most efficient path.
I would always go for the join over the subquery therefore. YMMV.