Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Subquery and Correlated Subquery

Is the following piece of SQL Query a normal query or a Correlated Subquery ??

SELECT UserID,        FirstName,        LastName,        DOB,        GFName,        GLName,        LoginName,        LoginEffectiveDate,        LoginExpiryDate,        Password,        Email,        ReportingTo,        Mobile,        CommunicationPreference,        IsActive FROM   (SELECT row_number() OVER (ORDER BY FirstName) AS Row,                UserID,                FirstName,                LastName,                DOB,                GFName,                GLName,                LoginName,                LoginEffectiveDate,                LoginExpiryDate,                Password,                Email,                ReportingTo,                Mobile,                CommunicationPreference,                IsActive         FROM   DivakarUserRegistration)  T 

Also, can someone state the difference between the both

like image 216
Divakar Avatar asked Jun 24 '13 05:06

Divakar


People also ask

Which is faster subquery or correlated subquery?

Speed and PerformanceA correlated subquery is much slower than a non-correlated subquery because in the former, the inner query executes for each row of the outer query. This means if your table has n rows then whole processing will take the n * n = n^2 time, as compared to 2n times taken by a non-correlated subquery.

What is the difference between a correlated and non correlated subquery?

Subqueries can be categorized into two types: A noncorrelated (simple) subquery obtains its results independently of its containing (outer) statement. A correlated subquery requires values from its outer query in order to execute.

What is the difference between query and subquery?

A query is an operation that retrieves data from one or more tables or views. In this reference, a top-level SELECT statement is called a query, and a query nested within another SQL statement is called a subquery.

What is a correlated subquery in SQL?

A correlated subquery is a subquery that refers to a column of a table that is not in its FROM clause. The column can be in the Projection clause or in the WHERE clause. In general, correlated subqueries diminish performance.


2 Answers

Above example is not Co-related Sub-Query. It is Derived Table / Inline-View since i.e, a Sub-query within FROM Clause.

A Corelated Sub-query should refer its parent(main Query) Table in it. For example See find the Nth max salary by Co-related Sub-query:

SELECT Salary  FROM Employee E1 WHERE N-1 = (SELECT COUNT(*)              FROM Employee E2              WHERE E1.salary <E2.Salary)  

Co-Related Vs Nested-SubQueries.

Technical difference between Normal Sub-query and Co-related sub-query are:

1. Looping: Co-related sub-query loop under main-query; whereas nested not; therefore co-related sub-query executes on each iteration of main query. Whereas in case of Nested-query; subquery executes first then outer query executes next. Hence, the maximum no. of executes are NXM for correlated subquery and N+M for subquery.

2. Dependency(Inner to Outer vs Outer to Inner): In the case of co-related subquery, inner query depends on outer query for processing whereas in normal sub-query, Outer query depends on inner query.

3.Performance: Using Co-related sub-query performance decreases, since, it performs NXM iterations instead of N+M iterations. ¨ Co-related Sub-query Execution.

For more information with examples :

http://dotnetauthorities.blogspot.in/2013/12/Microsoft-SQL-Server-Training-Online-Learning-Classes-Sql-Sub-Queries-Nested-Co-related.html

like image 95
nayeemDotNetAuthorities Avatar answered Sep 17 '22 18:09

nayeemDotNetAuthorities


Correlated Subquery is a sub-query that uses values from the outer query. In this case the inner query has to be executed for every row of outer query.

See example here http://en.wikipedia.org/wiki/Correlated_subquery

Simple subquery doesn't use values from the outer query and is being calculated only once:

SELECT id, first_name  FROM student_details  WHERE id IN (SELECT student_id FROM student_subjects  WHERE subject= 'Science');  

CoRelated Subquery Example -

Query To Find all employees whose salary is above average for their department

 SELECT employee_number, name        FROM employees emp        WHERE salary > (          SELECT AVG(salary)            FROM employees            WHERE department = emp.department); 
like image 34
Alex Avatar answered Sep 18 '22 18:09

Alex