SlideShare a Scribd company logo
7. Using Sub Queries
USING SUB-QUERIES, TO SOLVE QUERIES.
OBJECTIVES
At the end of this module, students should be able
to:
1. Define sub-queries.
2. Know type of problems that sub-queries
can solve.
3. List the types of sub-queries
4. Write single row and multiple row sub-
queries.
SUB-QUERIES
 A sub-query –also called a nested SELECT
statement- is defined as a query, within
another query.
 It may appear in the WHERE clause, in the HAVING
clause, or in the FROM clause.
 When placing a restriction on a query block,
we always specify the conditions for the
restriction in the WHERE clause.
EXAMPLE:
 Under certain conditions, you may need to
write a query to determine the WHERE condition.
EXERCISE:
 Using a single SELECT statement, query the
employees table for those employees who
earn a salary below Nancy’s salary.
 However, if you want to query for the details
of all the staff who earn a salary less than that
of Nancy you would use a sub-query for
Nancy’s salary, and the result in the WHERE
condition of our SELECT statement.
 We have two types of sub-queries. The single
row sub-queries and the multiple row sub-
queries.
SINGLE ROW SUB_QUERIES
 The single row sub-queries, would return only
one value, from the inner SELECT statement. In a
single row sub-query, the outer query,
contains a single row comparison operator,
like an equal to, equal to or greater than, or
less than.
EXAMPLE:
 This is a single row sub-query because the
inner SELECT statement will return only one value
and the outer SELECT statement uses a single
comparison operator to pick up that value.
MULTIPLE ROW SUB_QUERIES
 A multiple row sub-query on the contrary
would return more than one row from the
inner SELECT statement.
 The outer SELECT will therefore need a multiple
comparison operator like ANY, IN or ALL to pick the
multiple values returned by the inner query.
EXAMPLE:
 The Oracle server first takes the inner
query and searches for the DISTINCT values in
the salary of employees, with STORE CLERK as the
job_id.
 It then places this numbers in the bracket
next to the ANY keyword. And that is what
gives us the result displayed, on the
previous screen.

More Related Content

PPTX
MySQL JOIN & UNION
PPTX
Sql(structured query language)
PPTX
SQL : Structured Query Language
PPTX
Data Manipulation Language
PPTX
Sql joins
PPT
Join sql
PPTX
Oracle: Joins
DOC
SQL Joins
MySQL JOIN & UNION
Sql(structured query language)
SQL : Structured Query Language
Data Manipulation Language
Sql joins
Join sql
Oracle: Joins
SQL Joins

What's hot (15)

PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
PPT
Sql join
PDF
SQL JOINS
PPTX
Joins And Its Types
PPT
SQL Introduction to displaying data from multiple tables
PPTX
SQL UNION
PPTX
SQL JOIN
PPTX
Null values, insert, delete and update in database
PPTX
MULTIPLE TABLES
PPT
Oracle sql joins
PPTX
STRUCTURE OF SQL QUERIES
PPSX
Join query
PDF
Sql integrity constraints
Database Management - Lecture 2 - SQL select, insert, update and delete
Sql join
SQL JOINS
Joins And Its Types
SQL Introduction to displaying data from multiple tables
SQL UNION
SQL JOIN
Null values, insert, delete and update in database
MULTIPLE TABLES
Oracle sql joins
STRUCTURE OF SQL QUERIES
Join query
Sql integrity constraints
Ad

Similar to 7. Using Sub Queries (20)

PPT
SQL subquery
PPTX
Ch 6 Sub Query.pptx
PPTX
Week6_Theory.pptx
PPT
Les06[1]Subqueries
PPT
SUBQUERY materi kegiatan pembelajaran 1.1
PPT
SQL WORKSHOP::Lecture 6
PPT
Les06- Subqueries.ppt
PPT
Les07.ppt 0125566655656959652323265656565
PDF
SQL Subqueries - Oracle SQL Fundamentals
PPTX
Unit 3-p2relationaldatabasescodeing.pptx
PPT
Subqueries -Oracle DataBase
PPTX
Lab5 sub query
PPTX
SUBQUERIES.pptx
PPTX
Module 3.1.pptx
PDF
Sub query_SQL
 
PPT
PPT
Subconsultas
PPT
Les06
PPT
Les06 (using subqueries to solve queries)
SQL subquery
Ch 6 Sub Query.pptx
Week6_Theory.pptx
Les06[1]Subqueries
SUBQUERY materi kegiatan pembelajaran 1.1
SQL WORKSHOP::Lecture 6
Les06- Subqueries.ppt
Les07.ppt 0125566655656959652323265656565
SQL Subqueries - Oracle SQL Fundamentals
Unit 3-p2relationaldatabasescodeing.pptx
Subqueries -Oracle DataBase
Lab5 sub query
SUBQUERIES.pptx
Module 3.1.pptx
Sub query_SQL
 
Subconsultas
Les06
Les06 (using subqueries to solve queries)
Ad

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PDF
Classroom Observation Tools for Teachers
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Computing-Curriculum for Schools in Ghana
Sports Quiz easy sports quiz sports quiz
Classroom Observation Tools for Teachers
VCE English Exam - Section C Student Revision Booklet
PPH.pptx obstetrics and gynecology in nursing
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
TR - Agricultural Crops Production NC III.pdf
Computing-Curriculum for Schools in Ghana

7. Using Sub Queries

  • 2. USING SUB-QUERIES, TO SOLVE QUERIES.
  • 3. OBJECTIVES At the end of this module, students should be able to: 1. Define sub-queries. 2. Know type of problems that sub-queries can solve. 3. List the types of sub-queries 4. Write single row and multiple row sub- queries.
  • 4. SUB-QUERIES  A sub-query –also called a nested SELECT statement- is defined as a query, within another query.  It may appear in the WHERE clause, in the HAVING clause, or in the FROM clause.  When placing a restriction on a query block, we always specify the conditions for the restriction in the WHERE clause.
  • 6.  Under certain conditions, you may need to write a query to determine the WHERE condition.
  • 7. EXERCISE:  Using a single SELECT statement, query the employees table for those employees who earn a salary below Nancy’s salary.  However, if you want to query for the details of all the staff who earn a salary less than that of Nancy you would use a sub-query for Nancy’s salary, and the result in the WHERE condition of our SELECT statement.
  • 8.  We have two types of sub-queries. The single row sub-queries and the multiple row sub- queries.
  • 9. SINGLE ROW SUB_QUERIES  The single row sub-queries, would return only one value, from the inner SELECT statement. In a single row sub-query, the outer query, contains a single row comparison operator, like an equal to, equal to or greater than, or less than.
  • 11.  This is a single row sub-query because the inner SELECT statement will return only one value and the outer SELECT statement uses a single comparison operator to pick up that value.
  • 12. MULTIPLE ROW SUB_QUERIES  A multiple row sub-query on the contrary would return more than one row from the inner SELECT statement.  The outer SELECT will therefore need a multiple comparison operator like ANY, IN or ALL to pick the multiple values returned by the inner query.
  • 14.  The Oracle server first takes the inner query and searches for the DISTINCT values in the salary of employees, with STORE CLERK as the job_id.  It then places this numbers in the bracket next to the ANY keyword. And that is what gives us the result displayed, on the previous screen.