Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (2024)

Joins are used to combine two or more different tables based on a common field between them with the help of this we can easily retrieve the data from multiple tables. So, In this article, we are going to discuss the JOIN Operation, and Subquery in detail. Let’s start with the JOIN operation.

JOIN Operation

A join operation is a binary operation used to combine data or rows from two or more tables based on a common field between them. INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN are different types of Joins.

Example:

Orders (OrderID , OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);

Find details of customers who have ordered.

Query

SELECT * from Customers JOIN Orders 
ON Orders.OrderID=Customers.CustomerID;

Output

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (1)

output

Subquery

CREATE TABLE Customers(
CustomerID INT PRIMARY KEY NOT NULL,
CustomerName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50);

CREATE TABLE Orders(
OrderID INT ,
OrderDate DATE);

INSERT INTO Orders(OrderID,OrderDate)
VALUES(1,'11/12/2020'),
(2,'14/12/2014'),
(3,'17/1/2019'),
(4,'18/2/2020');

INSERT INTO Customers(CustomerID,CustomerName, ContactName,Country)
VALUES (1,'John','David','USA'),
(2,'Leo','David','UK'),
(3,'David','John','USA'),
(4,'Betty','Leo','UK'),
(5,'Peter','Peter','UAE');

Customers table:

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (2)

Customer Table

Orders table:

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (3)

Order Table

Nested Subquery

In Nested Query, the Inner query runs first, and only once. Outer query is executed with the result from Inner query. Hence, the Inner query is used in execution of the Outer query.

Example:

Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);

Find details of customers who have ordered.

Query

SELECT * FROM Customers WHERE 
CustomerID IN (SELECT CustomerID FROM Orders);

Output

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (4)

output

Correlated Subquery

In Correlated Query, Outer query executes first and for every Outer query row Inner query is executed. Hence, the Inner query uses values from the Outer query.

Example:

Orders (OrderID , OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);

Find details of customers who have ordered.

Query

SELECT * FROM Customers where 
EXISTS (SELECT CustomerID FROM Orders
WHERE Orders.OrderID=Customers.CustomerID);

Output

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (5)

output

Application of Join Operation and Subquery

To understand the difference between Nested Subquery, Correlated Subquery, and Join Operation firstly we have to understand where we use subqueries and where to use joins.

  • When we want to get data from multiple tables we use join operation. Example: Let’s consider two relations as:
    Employee (eId, eName, eSalary, dId);
    Department (dId, dName, dLocation);

    Now, we have to find employee names and Department name working at London Location. Here, we have to display eName from employee table and dName from Department table. Hence we have to use Join Operation.

    SELECT e.eName, d.dName from Employee e, 
    Department d
    where e.dId=d.dId and d.dLocation="London";
  • When we want to get data from one table and the condition is based on another table we can either use Join or Subquery. Now, we have to find employee names working at London Location. Here, we have to display only eName from the employee table hence we can use either Join Operation or Subquery Using Join Operation:
    SELECT e.eName from Employee e, Department d 
    where e.dId=d.dId and d.dLocation="London";

    Using Subquery:

    SELECT eName from Employee 
    where dId=(SELECT dId from Department where dLocation="London");

    After understanding the basic difference between Join and Subqueries, Now we will understand the difference between Nested Subquery, Correlated Subquery, and Join Operation.

Difference between Nested Query, Correlated Query, and Join Operation

Parameters Nested QueryCorrelated QueryJoin Operation
DefinitionIn Nested query, a query is written inside another query and the result of the inner query is used in the execution of the outer query.In Correlated query, a query is nested inside another query and an inner query uses values from the outer query. The join operation is used to combine data or rows from two or more tables based on a common field between them. INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN are different types of Joins.
ApproachBottom-up approach i.e. Inner query runs first, and only once. The outer query is executed with result from the Inner query.Top to Down Approach i.e. Outer query executes first and for every Outer query row Inner query is executed.It is basically cross product satisfying a condition.
DependencyInner query execution is not dependent on Outer query.Inner query is dependent on the Outer query.There is no Inner Query or Outer Query. Hence, no dependency is there.
PerformancePerforms better than Correlated Query but is slower than Join Operation.Performs slower than both Nested Query and Join operations as for every outer query inner query is executed.By using joins we maximize the calculation burden on the database but joins are better optimized by the server so the retrieval time of the query using joins will almost always be faster than that of a subquery.

Conclusion

In this article, we have basically mentioned the JOINS, nested query, and co-related query, and the differences between them. Both the topics, whether the JOINS or the Subquery, play an important role in the Structured query language. A nested query is used to retrieve data based on the result of an inner query and a correlated query is a type of nested query where the inner query references a column from the outer query and a join operation is used to merges the two different tables based on a common field between them. We discussed the JOINS and subquery with suitable examples.


Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 28 Aug, 2023

Like Article

Save Article

Previous

SQL Join vs Subquery

Next

SQL

Share your thoughts in the comments

Please Login to comment...

Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6017

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.