0% found this document useful (0 votes)
25 views3 pages

Handouts - 12 (Sub Queries)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Handouts - 12 (Sub Queries)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Quality Thought ETL Testing Daily Handouts

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

Subqueries: Subquery or Inner query is a query in a query. SQL subquery is usually


added in the WHERE Clause of the SQL statement.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the comparison operators like =, <, >, >=, <=,<> and In,
NotIn operators and by using From Statement.

Emp Table: Dept Table


EID Ename Sal Did DID Dname
1 A 1000 10 10 XX
2 B 2000 20 20 YY
3 C 3000 30
4 D 4000 40

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.

How Subqueries will execute:


Step1: Whenever we are executing query with subquery first subquery will execute. But
this query will not display output into output screen. The output of subquery is passing as
input to outer query.
Step2: With step1 whatever output we are getting it is passing as input to outer query.
Outer query will execute and output will displayed to output screen.

Scenario 92: Display complete information about employee who is having maximum salary.
Select * From Emp Where Sal = (Sel Max(Sal) From Emp)

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

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?

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

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.

Scenario 97: Display n th highest salary?


Sel Sal From Emp O Where
n-1= (Sel Count(Distinct Sal) From Emp S Where O.Sal<S.Sal)

Scenario 98: Display 4th highest salary?


Sel Sal From Emp O Where
3= (Sel Count(Distinct Sal) From Emp S Where O.Sal<S.Sal)

Scenario 99: Display 4th least salary?


Sel Sal From Emp O Where
3= (Sel Count(Distinct Sal) From Emp S Where O.Sal>S.Sal)

In all above subquereis we are maintaining relationship between outer and Subqueries. So
these kind of Subqueries we can call it as Correlated Subqueries.

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]

You might also like