Correlated Subquery
Correlated Subquery
Correlated Subquery
com/2017/04/how-correlated-subquery-works-in-
oracle.html
Let’s check how correlated sub query works using above table.
Here I have replaced e1.sal with 67760 because that is 1st value which
would come and check that query condition. So if you see there, we would
get count only one as we have only 1 record which is >=67760.
It means where condition doesn’t satisfy (i.e. where 2=1).so we don’t get
any result.
Here we ill get count 3 as we have 3 records which are greater than or equal
to 32000 so this one also doesn’t satisfy condition ( i.e. where 2=3). So we
don’t get any result.
Here we ill get count 4 as we have 4 records which are greater than or equal
to 25000 so this one also doesn’t satisfy condition ( i.e. where 2=4). So we
don’t get any result.
Here we ill get count 2 as we have 2 records which are greater than or equal
to 34345 so finally our where condition got satisfied (i.e. where 2=2) for sal
value 34345.So oracle would return this record as output.
In this way we can find out nth highest salary using correlated subquery.
https://www.w3resource.com/sql/subqueries/corelated-subqueries-using-aliases.php
Correlated Subqueries
SQL Correlated Subqueries are used to select data from a table referenced in the outer query.
The subquery is known as a correlated because the subquery is related to the outer query. In this
type of queries, a table alias (also called a correlation name) must be used to specify which table
reference is to be used.
The alias is the pet name of a table which is brought about by putting directly after the table
name in the FROM clause. This is suitable when anybody wants to obtain information from two
separate tables.
The following correlated subqueries retrive ord_num, ord_amount, cust_code and agent_code
from the table orders ( 'a' and 'b' are the aliases of orders and agents table) with following
conditions -
the agent_code of orders table must be the same agent_code of agents table and agent_name of
agents table must be Alex,
SQL Code:
SELECT a.ord_num,a.ord_amount,a.cust_code,a.agent_code
FROM orders a
WHERE a.agent_code=(
SELECT b.agent_code
FROM agents b WHERE b.agent_name='Alex');
Copy
Output:
SQL Code:
SELECT a.ord_num,a.ord_amount,a.cust_code,a.agent_code
FROM orders a
WHERE a.agent_code='A003';
Copy
Pictorical Presentation:
Using EXISTS with a Correlated Subquery
We have already used the EXISTS operator to check the existence of a result of a subquery.
EXISTS operator can be used in correlated subqueries also. Using EXISTS the following query
display the employee_id, manager_id, first_name and last_name of those employees who
manage other employees.
SQL Code:
Output:
Pictorial Presentation:
Using NOT EXISTS with a Correlated Subquery
NOT EXISTS is logically opposite of EXISTS operator. NOT EXISTS is used when we need to
check if rows do not exist in the results returned by a subquery. Using NOT EXISTS the
following query display the employee_id, manager_id, first_name and last_name of those
employees who have no manager status. This query is opposite to the previous one.
SQL Code:
Output:
Pictorial Presentation:
https://www.geeksforgeeks.org/sql-correlated-subqueries/
SQL Correlated Subqueries
Last Updated: 06-02-2018
Correlated subqueries are used for row-by-row processing. Each subquery is executed once for
every row of the outer query.
A correlated subquery is evaluated once for each row processed by the parent statement. The
parent statement can be a SELECT, UPDATE, or DELETE statement.
A correlated subquery is one way of reading every row in a table and comparing values in each
row against related data. It is used whenever a subquery must return a different result or set of
results for each candidate row considered by the main query. In other words, you can use a
correlated subquery to answer a multipart question whose answer depends on the value in each
row processed by the parent statement.
With a normal nested subquery, the inner SELECT query runs first and executes once, returning
values to be used by the main query. A correlated subquery, however, executes once for each
candidate row considered by the outer query. In other words, the inner query is driven by the
outer query.
NOTE : You can also use the ANY and ALL operator in a correlated subquery.
EXAMPLE of Correlated Subqueries : Find all the employees who earn more than the average
salary in their department.
CORRELATED UPDATE :
UPDATE table1 alias1
SET column = (SELECT expression
FROM table2 alias2
WHERE alias1.column =
alias2.column);
Use a correlated subquery to update rows in one table based on rows from another table.
CORRELATED DELETE :
DELETE FROM table1 alias1
WHERE column1 operator
(SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
Use a correlated subquery to delete rows in one table based on the rows from another table.
The EXISTS operator tests for existence of rows in the results set of the subquery. If a subquery
row value is found the condition is flagged TRUE and the search does not continue in the inner
query, and if it is not found then the condition is flagged FALSE and the search continues in the
inner query.
EXAMPLE of using EXIST operator :
Find employees who have at least one person reporting to them.
OUTPUT :