SlideShare a Scribd company logo
SQL Fundamentals Oracle 11g
M U H A M M A D WA H E E D
O R AC L E DATA BA S E D E V E LO P E R
E M A I L : m .wa h e e d 3 6 6 8 @ g m a i l . co m
Lecture#8
SQL Subqueries
Subqueries
2
Subqueries(cont’d)
Syntax for subqueries:
3
Subqueries(cont’d)
•The subquery(inner query) is executed once before the
main query.
•The result of subquery is used by the main query(outer
query).
4
Subqueries(cont’d)
There are following types of subqueries:
•Single row
•Multiple row
5
Subqueries(cont’d)
Guidelines for using subqueries:
•Enclose subquery in parenthesis
•Place subquery on the right side of comparison condition
•Use single row operators with single row queries and
multiple with multiple queries
6
Subqueries(cont’d)
There are two types of subqueries:
•Single row subqueries
•Multiple row subqueries
7
Subqueries(cont’d)
Single row subqueries:
•Returns only one row
•Use single row comparison operators i.e. =, < , > , <= ,>=, <>
8
Subqueries(cont’d)
Single row subqueries:
•Example:
SELECT *
FROM teacher
WHERE dept_id =
(SELECT dept_id
FROM department
WHERE dept_name = ‘UIIT’);
9
Subqueries(cont’d)
More than one single row subqueries:
•Example:
SELECT *
FROM teacher
WHERE dept_id =
(SELECT dept_id
FROM department
WHERE dept_name = ‘UIIT’)
AND salary >
(SELECT AVG(salary)
FROM teacher) ;
10
Subqueries(cont’d)
HAVING clause single row subqueries:
•Example:
SELECT dept_id, MIN(salary)
FROM employee
GROUP BY dept_id
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employee
WHERE dept_name = ‘UIIT’);
11
Subqueries(cont’d)
12
Illegal queries are as following:
• You cannot use multiple rows return queries for single row
operators
Subqueries(cont’d)
13
Empty result subqueries are as following:
• ‘no rows selected’ because subquery returns nothing
Multiple-Row Subqueries(cont’d)
•Return more than one row
•Uses multiple row comparison operators i.e.
14
Multiple-Row Subqueries(cont’d)
Use of IN operator in multiple-row subqueries
15
Multiple-Row Subqueries(cont’d)
Use of ANY operator in multiple-row subqueries:
• ‘<ANY’ means less than maximum of returned values
• ‘>ANY’ means more than the minimum of returned values
•‘=ANY’ is equivalent to ‘IN’ operator
16
Multiple-Row Subqueries(cont’d)
Use of ALL operator in multiple-row subqueries:
• ‘<ALL’ means less than maximum of returned values
• ‘>ALL’ means more than the minimum of returned values
17
Multiple-Row Subqueries(cont’d)
NULL values in multiple-row subqueries:
• If one of the returned values by inner query is NULL value then the entire query returns no
values
•‘NOT IN’ operator is equivalent to ‘<>ALL’
•This issue can be resolved by using an additional WHERE clause in the inner query i.e WHERE
mgr.manager_id IS NOT NULL
18
Feedback/Suggestions?
Give your feedback at: m.waheed3668@gmail.com
The feedback I get is that my books are honest.
- Laurie Halse Anderson

More Related Content

What's hot (20)

PPTX
Array within a class
AAKASH KUMAR
 
PPT
Session 6
Shailendra Mathur
 
PDF
Data Manipulation(DML) and Transaction Control (TCL)
MuhammadWaheed44
 
PDF
Scala laboratory: Globus. iteration #2
Vasil Remeniuk
 
PDF
Composite types
Manjitsing Valvi
 
PDF
Listing for TryLambdaExpressions
Derek Dhammaloka
 
PDF
SQL Fundamentals - Lecture 2
MuhammadWaheed44
 
PPTX
Arrays
RababaKollathodi
 
PPTX
Queues
nidhisatija1
 
PPTX
Operators and expression in c++
ThamizhselviKrishnam
 
PPT
Queue
Shaista Qadir
 
PPTX
Stack Data Structure
Afaq Mansoor Khan
 
PPTX
Java Tutorial Lab 7
Berk Soysal
 
PDF
Functional Programming
Yuan Wang
 
PPTX
Introduction to Dependently Types: Idris
Abdulsattar Mohammed
 
PPTX
Stack of Data structure
Sheikh Monirul Hasan
 
PDF
Pointers & functions
Manjitsing Valvi
 
PPT
358 33 powerpoint-slides_6-strings_chapter-6
sumitbardhan
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
Array within a class
AAKASH KUMAR
 
Data Manipulation(DML) and Transaction Control (TCL)
MuhammadWaheed44
 
Scala laboratory: Globus. iteration #2
Vasil Remeniuk
 
Composite types
Manjitsing Valvi
 
Listing for TryLambdaExpressions
Derek Dhammaloka
 
SQL Fundamentals - Lecture 2
MuhammadWaheed44
 
Queues
nidhisatija1
 
Operators and expression in c++
ThamizhselviKrishnam
 
Stack Data Structure
Afaq Mansoor Khan
 
Java Tutorial Lab 7
Berk Soysal
 
Functional Programming
Yuan Wang
 
Introduction to Dependently Types: Idris
Abdulsattar Mohammed
 
Stack of Data structure
Sheikh Monirul Hasan
 
Pointers & functions
Manjitsing Valvi
 
358 33 powerpoint-slides_6-strings_chapter-6
sumitbardhan
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 

Similar to SQL Subqueries - Oracle SQL Fundamentals (20)

PPT
SQL subquery
Vikas Gupta
 
PPT
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
PPT
Les06
Sudharsan S
 
PPT
Les06- Subqueries.ppt
gznfrch1
 
PPTX
Ch 6 Sub Query.pptx
RamishaRauf
 
PPT
Les06
Vijay Kumar
 
PPT
Les06 (using subqueries to solve queries)
Achmad Solichin
 
PPTX
Sql subquery
Raveena Thakur
 
PPT
Les18
Vijay Kumar
 
PPT
Subconsultas
Maria
 
PDF
7. Using Sub Queries
Evelyn Oluchukwu
 
PPT
SUBQUERY materi kegiatan pembelajaran 1.1
mochammadagri
 
PDF
Sub query_SQL
CoT
 
PPTX
SUBQUERIES.pptx
RenugadeviR5
 
PPTX
Unit 3-p2relationaldatabasescodeing.pptx
artipunia12
 
PPT
Les06
Sudharsan S
 
PPT
Using subqueries to solve queries
Syed Zaid Irshad
 
PPT
Subqueries -Oracle DataBase
Salman Memon
 
PPTX
Week6_Theory.pptx
MDTOUKERAHMED
 
SQL subquery
Vikas Gupta
 
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
Les06- Subqueries.ppt
gznfrch1
 
Ch 6 Sub Query.pptx
RamishaRauf
 
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Sql subquery
Raveena Thakur
 
Subconsultas
Maria
 
7. Using Sub Queries
Evelyn Oluchukwu
 
SUBQUERY materi kegiatan pembelajaran 1.1
mochammadagri
 
Sub query_SQL
CoT
 
SUBQUERIES.pptx
RenugadeviR5
 
Unit 3-p2relationaldatabasescodeing.pptx
artipunia12
 
Using subqueries to solve queries
Syed Zaid Irshad
 
Subqueries -Oracle DataBase
Salman Memon
 
Week6_Theory.pptx
MDTOUKERAHMED
 
Ad

Recently uploaded (20)

PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Next level data operations using Power Automate magic
Andries den Haan
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
Practical Applications of AI in Local Government
OnBoard
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Ad

SQL Subqueries - Oracle SQL Fundamentals