Handouts - 12 (Sub Queries)
Handouts - 12 (Sub Queries)
Session-12
Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Topics Covered in session:
Subqueries
Nested Subqueries
Inline Subqueries
Correlated Subqueries
Scenario 91: Display complete information about employee who is having maximum salary.
To achieve the requirement we can write query as below.
Select * From Emp Where Sal=5000
Here 5000 is nothing but maximum salary from employee table, but we are passing this
value manually. If we are very less number of records we can pass this value manually. But
if we are having more number of records we cannot find maximum salary manually, in that
situation we need to pass query as below instead of passing value manually
Select * From Emp Where Sal = (Sel Max(Sal) From Emp)
In above query we are using query within the query, these kind of queries we can call it as
Subqueries.
Scenario 92: Display complete information about employee who is having maximum salary.
Select * From Emp Where Sal = (Sel Max(Sal) From Emp)
Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Scenario 93: Display second highest salary from Emp table
Select Max(Sal) As Sal From Emp Where Sal Not In (Sel Max(Sal) From Emp)
Select Max(Sal) As Sal From Emp Where Sal < (Sel Max(Sal) From Emp)
Scenario 94: Display complete information about employee who is having second highest
salary.
Select * From Emp Where Sal = (Select Max(Sal) As Sal From Emp Where Sal < (Sel
Max(Sal) From Emp) )
In this scenario we are using multiple Subqueries
Scenario 95: Display information from Emp table whose departments present in
Department table.
Select E.* From Emp e Join Dept D on E.Did=D.Did
Instead of using join we can write query as below
Select * From Emp Where Did in (Sel Did from Dept)
Types of Subqueries:
Types of Subqueries Description
Nested Subquery Whenever we are using Subqueries by using Comparison operators, In,
NotIn operates those kind of queries we can call it as Nested Subqueries
Inline Subquery Whenever we are using Subquery by using From clause such kind of
queries we can call it as Inline Subquery.
Rule: Whenever we are passing Inline subquery compulsory we should
have to specify alias name
Correlated Subquery When both the inner query and the outer query are interdependent such
kind of Subqueries we can call it as Correlated Subquery. For every row
processed by the inner query, the outer query is processed as well. The
inner query depends on the outer query before it can be processed.
All above scenarios related to Nested Subqueries, because in all those queries we are using
Subqueries with comparison operators or In or Notin operator.
Scenario 96: We are having Student_Src table with students marks information. As per
client requirement need to convert horizontal data in to vertical and need to load that data
in to Student_Tgt table.
Student_Src Student_Tgt
StdID StdName M1 M2 M3 Stdid StdName Marks
1 A 40 50 60 1 A 40
2 B 50 60 70 1 A 50
1 A 60
2 B 50
2 B 60
2 B 70
In real time Developer will load data into target table by using ETL tool after implementing
all the requirements.
As a ETL tester how to perform the validations here?
Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
As a tester we need to perform Data Validation and Count Validations.
Data Validation:
(Select StdId,StdName,M1 From Student_Src UNION Select StdId,StdName,M2 From
Student_Src UNION Select StdId,StdName,M3 From Student_Src)
MINUS
Select * From Student_Tgt
Count Validation:
Sel Count(*) From (Select StdId,StdName,M1 From Student_Src UNION Select
StdId,StdName,M2 From Student_Src UNION Select StdId,StdName,M3 From Student_Src)
Src MINUS
Select Count(*) From Student_Tgt
If you observe in above source Count Query we are writing subquery by using from
statement. These kind of subquiries we can call it as Inline Subqueries.
Correlated Subquery: When both the inner query and the outer query are interdependent
such kind of Subqueries we can call it as Correlated Subquery. For every row processed by
the inner query, the outer query is processed as well. The inner query depends on the outer
query before it can be processed.
While writing subquery if you are maintain relationship between outer query and
subquery such kind of Subqueries we can call it as Correlated Subquery.
In all above subquereis we are maintaining relationship between outer and Subqueries. So
these kind of Subqueries we can call it as Correlated Subqueries.