0% found this document useful (0 votes)
2 views20 pages

05.Using Sub Queries

This document provides an overview of sub-queries in Oracle SQL, explaining their definition and types, including single row and multiple row sub-queries. It includes examples and guidelines for using sub-queries effectively, as well as practice exercises for applying the concepts. The document emphasizes the importance of using appropriate operators for different types of sub-queries.

Uploaded by

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

05.Using Sub Queries

This document provides an overview of sub-queries in Oracle SQL, explaining their definition and types, including single row and multiple row sub-queries. It includes examples and guidelines for using sub-queries effectively, as well as practice exercises for applying the concepts. The document emphasizes the importance of using appropriate operators for different types of sub-queries.

Uploaded by

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

Oracle SQL

5. Using Sub-Queries
Eng:- Ahmed Ramadan , Eng:- Marwan Elkordey
Chapter 05 : Using Sub-Queries

Chapter Content :
What is Sub-query
Sub-query Types
Single row Sub-query
Multiple Row Sub-query
IN
All
any
Pairwise and impair-wise Sub-queries
Chapter 05 : Using Sub-Queries

What is Sub-Query ?
Select statement written as a part of another
Select statement , to get value from database

Main Query

Example:
select * Sub-Query
from emp
where deptno = ( Select deptno From emp where ename ='SCOTT' )
Sequence of execution
( Select deptno From emp where ename ='SCOTT' )  Subquery 1. Run sub-query
select * from emp where deptno  Main Query 2. Run Main query
Chapter 05 : Using Sub-Queries

What is Sub-Query ?
Example:
select *
from emp
where deptno = ( Select deptno
From emp
where ename ='SCOTT'
)
Chapter 05 : Using Sub-Queries

What is Sub-Query ?
Example:
select *
from emp
where job= ( Select job
From emp
where ename ='SCOTT'
)
Chapter 05 : Using Sub-Queries

Guide Lines When Using Sub-Queries


Chapter 05 : Using Sub-Queries

Sub-Query Types
Single Row Subquery: Return only one row
Multiple Row Subquery : Return more than one row

Single Row Subquery: Return only one row


Example on single row sub query : Example on Single Row Subquery:
select * select *
from emp from emp
where job = ( Select job
From emp
where deptno = ( Select deptno
From emp
where ename ='SCOTT'
where ename ='SCOTT'
)
)
Chapter 05 : Using Sub-Queries

Sub-Query Types
Single Row Subquery: Return only one row
Multiple Row Subquery : Return more than one row
Chapter 05 : Using Sub-Queries

Sub-Query Types
Single Row Subquery: Return only one row

Display employees with salary < scott’s salary

select *
from emp
where sal < ( Select sal
From emp
where ename='SCOTT'
)

Single Row Sub Queries use comparison operators


= < > <= >= <>
Chapter 05 : Using Sub-Queries

Use Suitable Operator for subquery

=  single value operator


can not be used to test
Multiple Values

you have to use multiple


value operator
Chapter 05 : Using Sub-Queries

Multiple Row Subqueries


Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row Sub-query : In Operator
Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row
Sub-query :
not In Operator

You have to exclude null


Values from sub queries

By adding not null


condition
Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row Subquery : any Operator

This query will display


Employees with salary > 1100

> Min values in the sub query


Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row Subquery : any Operator

This query will display


Employees with salary < 3000

< Max values in the sub query


Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row Subquery : All Operator

This query will display


Employees with salary < 1100

< Min values in the sub query


Chapter 05 : Using Sub-Queries

Sub-Query Types
Multiple Row Subquery : All Operator

This query will display


Employees with salary > 3000

> Max values in the sub query


Chapter 05 : Using Sub-Queries

Pairwise and impair-wise Comparison


Impair-wise

Select *
From emp
Where deptno in (select deptno from emp where deptno = 20)
And sal in (select sal from emp where deptno = 20 )
Chapter 05 : Using Sub-Queries

Pairwise and impair-wise Comparison


Pairwise
Select *
From emp
Where (job ,sal ) in ( ( 'ANALYST ' , 3500 ) , ( 'SALESMAN' ,3000 ), ('CLERK' , 3000) )
Chapter 05 : Using Sub-Queries

Practices

1. Write a query to display employees in the same department with employee scott
2. Write a query to display employees with the same job as employee scott
3. Write a query to display employee data that earn the max salary
4. Write a query to display employee data earn max salary in each department
5. Write a query to display employee data that earn min salary in each department
6. Write a query to display employees in department named ‘Research’

You might also like