SubQuery and Correlated Subquery in teradata?
Sub queries and Correlated Sub queries are two
important concepts in Teradata and used most of the times.
The basic concept behind a subquery is that it
retrieves a list of values that are used for comparison against one or more
columns in the main query. Here the subquery is executed first and based on the
result set, the main query will be executed.
For example,
Select empname,deptname from employee where empid
IN ( select empid from salarytable where salary>10000).
In the above query, empid will be choosen first
based on the salary in the subquery and main query will be executed based on
the result subset.
Correlated Subquery is an excellent technique to
use when there is a need to determine which rows to SELECT based on one or more
values from another table.It combines subquery processing and Join processing
into a single request.
It first reads a row from the main query and then
goes into the subquery to find the rows that match the specified column
value.Then it goes for the next row from the main query. This process continues
untill all the qualifying rows from MAIN query.
For example,
select empname,deptno, salary
from employeetable as emp
where
salary=(select max(salary) from employeetable as
emt where emt.deptno=emp.deptno)
Above query returns the highest paid employee from
each department. This is also one of the scenario based questions in teradata.
No comments:
Post a Comment