Wednesday, 5 April 2023

Sub-Query and correlated Sub-Query in SQL

In SQL, a subquery is a query that is embedded within another query. The subquery is used to retrieve data that will be used in the main query's WHERE clause, SELECT statement, or FROM clause. A subquery can be either a standalone query or a correlated subquery.

A standalone subquery is a subquery that can be executed independently of the main query. The results of the subquery are then used in the main query. Here's an example:

SELECT *
FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderDate > '2022-01-01');

In this example, the subquery (SELECT CustomerID FROM Orders WHERE OrderDate > '2022-01-01') returns a list of customer IDs who have placed an order after a certain date. The main query then uses this list to retrieve all the customers who match this criteria.

On the other hand, a correlated subquery is a subquery that references a column from the outer query. The subquery is executed once for each row returned by the outer query, and the results of the subquery are used in the main query's WHERE clause or SELECT statement. Here's an example:

SELECT *
FROM Customers c
WHERE c.CustomerID IN (SELECT o.CustomerID FROM Orders o WHERE o.OrderDate > '2022-01-01' AND o.CustomerID = c.CustomerID);

In this example, the subquery (SELECT o.CustomerID FROM Orders o WHERE o.OrderDate > '2022-01-01' AND o.CustomerID = c.CustomerID) is correlated with the outer query by the condition o.CustomerID = c.CustomerID. The subquery is executed for each row returned by the outer query, and the results are used to filter the results in the main query.

Correlated subqueries are generally slower than standalone subqueries because they have to be executed multiple times. However, they can be very useful when you need to retrieve data from related tables based on a condition in the outer query.

 Sub-Query VS correlated Sub-Query in SQL 

A subquery and a correlated subquery are both types of queries in SQL that are used to retrieve data from one or more tables. However, there are some key differences between the two:

  1. Definition: A subquery is a query that is embedded within another query and can be executed independently of the outer query. A correlated subquery, on the other hand, is a subquery that references one or more columns in the outer query and is executed for each row of the outer query.

  2. Execution: In the case of a subquery, it is executed first, and its result set is used by the outer query. However, in the case of a correlated subquery, the inner query is executed for each row of the outer query, which can make it slower than a regular subquery.

  3. Result: A subquery always returns a result set that is used in the outer query, whereas a correlated subquery returns a result set that is specific to each row of the outer query.

  4. Purpose: Subqueries are often used to filter data in the WHERE clause, to check if a value exists in a table, or to perform aggregate calculations. Correlated subqueries are often used to retrieve related data from another table, to filter results based on a condition in another table, or to perform complex calculations involving related tables.

  5. Syntax: A subquery is enclosed in parentheses and is typically used with a comparison operator or an IN or EXISTS clause. A correlated subquery is similar to a regular subquery, but it also includes a reference to one or more columns from the outer query in its WHERE clause.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top