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;
Subquery
When 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);
- 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);
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 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 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 Query | Correlated Query | Join Operation |
---|---|---|---|
Definition | In Nested query, a query is written inside another query and the result of inner query is used in execution of outer query. | In Correlated query, a query is nested inside another query and inner query uses values from outer query. | 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, FULL JOIN are different types of Joins. |
Approach | Bottom up approach i.e. Inner query runs first, and only once. Outer query is executed with result from 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. |
Dependency | Inner query execution is not dependent on Outer query. | Inner query is dependent on Outer query. | There is no Inner Query or Outer Query. Hence, no dependency is there. |
Performance | Performs 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. |
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.