Difference between Nested Subquery, Correlated Subquery and Join Operation - GeeksforGeeks

28 Dec, 2020
Join Operation :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, FULL JOIN are different types of Joins.
Example –
Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);
Find details of customers who have ordered.
SELECT * from Customers JOIN Orders 
ON Orders.CustomerID=Customers.CustomerID;
SubqueryWhen a query is included inside another query, the Outer query is known as Main Query, and Inner query is known as Subquery.
Nested Query –In Nested Query,  Inner query runs first, and only once. Outer query is executed with result from Inner query.Hence, Inner query is used in execution of Outer query.Example –Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);Find details of customers who have ordered.SELECT * FROM Customers WHERE 
CustomerID IN (SELECT CustomerID FROM Orders);
Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);
Find details of customers who have ordered.
SELECT * FROM Customers WHERE 
CustomerID IN (SELECT CustomerID FROM Orders);
Correlated Query –In Correlated Query,  Outer query executes first and for every Outer query row Inner query is executed. Hence, Inner query uses values from Outer query.Example –Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);Find details of customers who have ordered.SELECT * FROM Customers where 
EXISTS (SELECT CustomerID FROM Orders 
WHERE Orders.CustomerID=Customers.CustomerID);
Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);
Find details of customers who have ordered.
SELECT * FROM Customers where 
EXISTS (SELECT CustomerID FROM Orders 
WHERE Orders.CustomerID=Customers.CustomerID);
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";
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 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 employee table hence we can use either Join Operation or SubqueryUsing 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.
Here, we have to display only eName from employee table hence we can use either Join Operation or SubqueryUsing 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 :
DBMS
Difference Between
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SQL Trigger | Student Database
Introduction of B-Tree
Difference between Clustered and Non-clustered index
Introduction of ER Model
Introduction of DBMS (Database Management System) | Set 1
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Differences between Black Box Testing vs White Box Testing