Subquery vs Co-related subquery


It might sound that both the queries are same but there is difference between the two. The difference is order of how these queries are executed and what is the relation between these two queries. Let’s create two sample tables and some sample data. So our initial data setup is like :

Sub-query vs Co-related sub-Query

Now let’s discuss these queries one by one.

Normal Sub-query : In this type of query, the outer or parent query and the inner query are independent of each other when executing. In this, inner query is executed first and then the result set returned by this query, is used by the outer query to generate the final result set. Let’s write a sub-query with the data setup we created above. This will provide us with all the employees, which have a record in the EmployeeDepartment table. So our query is like below, with the results.

Sub-query vs Co-related sub-Query

How normal sub-query works

First it selects all the records in the EmployeeDepartment table, which returns all the EmployeeId‘s in the table (4 records). Then, using the IN operator, the outer query gets all the records from Employees table, which have an EmployeeId in the result set returned by the inner query, to return only the matching records. So comes the output which we can see above.

Co-related sub-query : In this type of query, the two queries i.e. the outer and the inner query are dependent or linked to each other, to generate the final result set. Now, let’s use the co-related sub-query to get the same results and then we will discuss how they are linked and how it works. Our query changes to the following and we can see the results accordingly.

Sub-query vs Co-related sub-Query

How co-related sub query works 

First of all, the outer query executes and selects the first record from the Employees table. In this case, it is Mark with EmpId as 1. This record is then sent as an input to the inner query, where it checks whether the EmployeeDepartment table has any record with EmployeeId matching the EmpId of the record Mark, received from the outer query. If it finds any match in EmployeeDepartment table, then this record is kept for the result set (as the where condition in outer query is satisfied by it) else the outer query fetches the next record. Again, the second record is sent as an input to the inner query and this process continues until all the records of the Employees table are traversed by the outer query.
So we can see here that the inner query is dependent on the input or EmpId from the outer query to generate the results. This is how the outer and inner query are linked to each other. So this is how the sub-query and co-related sub-query work.

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in SQL Server and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s