Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Query or Joins

I have heard joins should be preferred over nested queries. Is it true in general? Or there might be scenarios where one would be faster than other:

for e.g. which is more efficient way to write a query?:

Select emp.salary 
from employee emp    
where emp.id = (select s.id from sap s where s.id = 111)

OR

Select emp.salary     
from employee emp   
INNER JOIN sap s ON emp.id = s.id   
WHERE s.id = 111
like image 933
ajay Avatar asked Oct 09 '10 16:10

ajay


People also ask

Is join better than nested query?

The advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.

What is faster a correlated subquery or an inner join?

"Correlated subqueries" are faster than Normal joins.


1 Answers

I have heard joins should be preferred over nested queries. Is it true in general?

It depends on the requirements, and the data.

Using a JOIN risks duplicating the information in the resultset for the parent table if there are more than one child records related to it, because a JOIN returns the rows that match. Which means if you want unique values from the parent table while using JOINs, you need to look at using either DISTINCT or a GROUP BY clause. But none of this is a concern if a subquery is used.

Also, subqueries are not all the same. There's the straight evaluation, like your example:

where emp.id = (select s.id from sap s where s.id = 111)

...and the IN clause:

where emp.id IN (select s.id from sap s where s.id = 111)

...which will match any of the value(s) returned by the subquery when the straight evaluation will throw an error if s.id returns more than one value. But there's also the EXISTS clause...

WHERE EXISTS(SELECT NULL 
               FROM SAP s
              WHERE emp.id = s.id
                AND s.id = 111)

The EXISTS is different in that:

  • the SELECT clause doesn't get evaluated - you can change it to SELECT 1/0, which should trigger a divide-by-zero error but won't
  • it returns true/false; true based on the first instance the criteria is satisfied so it's faster when dealing with duplicates.
  • unlike the IN clause, EXISTS supports comparing two or more column comparisons at the same time, but some databases do support tuple comparison with the IN.
  • it's more readable
like image 122
OMG Ponies Avatar answered Sep 30 '22 17:09

OMG Ponies