Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

As Performance which is better? SQL Joins Or SQL Subqueries? and why?

user-image
Question added by محمد حسام الدين حسن صدقى , Senior Functional Consultant , Egabi Fsi
Date Posted: 2016/08/10
Julien-Yusef Neidballa
by Julien-Yusef Neidballa , Trading Platform Software Engineer , Fidessa Group plc

Generally speaking, it is better to use joins for performance. However, subqueries are generally more readable and are not guaranteed to always be slower. It really depends on the join/query/subquery themselves (use of indexes, etc.), as well as the related tables' sizes. One way to determine which is the better choice in your particular case is to use the EXPLAIN statement – here is a link to Microsoft's documentation for it, if that helps.

ARCHIT KUMAR GUPTA
by ARCHIT KUMAR GUPTA , Senior consultant , Hcl Technologies

Performing actions in joins are more precise and efficient than subqueries

Vishwa Deepak Singh
by Vishwa Deepak Singh , Senior Software Engineer , Soccerlabs Pvt Ltd

The good thing in sub-queries is that they are more readable than JOINS that's why most new SQL people prefer them; it is the easy way; but when it comes to performance, JOINS are better in most cases even though they are not hard to read too.

Mohamed Maher Nada
by Mohamed Maher Nada , IT Senior System Engineer , Saudi Electronic Information Exchange Company (Tabadul)

SQL Join is much faster than Sub Queries for database performance . as they required less time to display an output than Sub Queries.

Guruprasad Patil
by Guruprasad Patil , Software Developers , INTELLECTUAL INFO SOLUTIONS PVT LTD

SQL Join is much faster than sub queries for performance database. and they required less time to display an output. Whereas sql sub queries are relatively very slow.

0 Comments

MOHAMMED IMAM
by MOHAMMED IMAM , Test Engineer , Mphasis Ltd.

 There is no single answer to this question. Sometimes a join is better, sometimes a subquery is better.It depends on several factors, including the specific query you're running, the amount of data in your database, the brand and version of SQL RDBMS you use.The best strategy is that you should test both the join solution and the subquery solution against your database, or a testing database with similar data. Only by analyzing and comparing the performance for a specific case can you be sure.

Mohammad Yousef
by Mohammad Yousef , ERP Application Manager , Mohammed Bin Al-Shaikh Sons’ Group

It depend on the results of the sub-queries, but SQL join is faster in general specially if the fields that is used for the joining have an existing index.

Mohammed yacoob Al-Timiri
by Mohammed yacoob Al-Timiri , Assistant Professor , www.cahcet.in

Performance is based on the amount of data you are executing on...

If it is less data around 20k. JOIN works better.

If the data is more like 100k+ then IN works better.

If you do not need the data from the other table, IN is good, But it is alwys better to go for EXISTS.

All these criterias I tested and the tables have proper inde

Deleted user
by Deleted user

No way to answer that question in general. It depends on what you're doing. You'll need to see the execution plan. When you write a subquery, it doesn’t mean the DBMS will execute it as such.

A subquery and a join can have the same query plan. Also, an outer join is not the same as inner join because outer join is extremely slow.

As a matter of fact, it'll be faster to create a temp table with the required data then use the result in an inner join rather than doing an outer join.

 

A visual explanation of SQL joins

https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

 

A subquery can run in O(k) because it's doing a math operation or it can run in O(n).

Your query can also use join, subquery, or subquery in a join. Using subquery in join will be faster than using a subquery in where clause.

Does your table have clustered index or non-clustered index? Does it need to be sorted or not?

So I don't believe generalization is a correct answer. You'll need to measure performance and see the execution plan to understand how the RDMS will perform because different engines use different methods.

And RDBMS will differ from NoSql (yes, Some NoSql engines offers a join operation such as MongoDB which has $lookup operator that can perform a LEFT-OUTER-JOIN) and they also differ among themselves.

 

based on performence sql joins are better than sql subqueries because in joins rdbms can create an execution plan that is better for your query and can predict what data should be loaded and to be processed and save time, unlike the sub-queries  where it will run all the queries and load all their data to do the processing

Rashid Irshad
by Rashid Irshad , Web & Software Trainer , AGT

Joins are performance hitters. Remembers. Choose minimum joins.

More Questions Like This