SlideShare a Scribd company logo
Copyright © 2007, Oracle. All rights reserved.
Using Subqueries to Solve Queries
Copyright © 2007, Oracle. All rights reserved.
7 - 2
Objectives
After completing this lesson, you should be able to do the
following:
• Define subqueries
• Describe the types of problems that the subqueries can
solve
• List the types of subqueries
• Write single-row and multiple-row subqueries
Copyright © 2007, Oracle. All rights reserved.
7 - 3
Lesson Agenda
• Subquery: Types, syntax, and guidelines
• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 4
Using a Subquery to Solve a Problem
Who has a salary greater than Abel’s?
Which employees have salaries greater than Abel’s
salary?
Main query:
What is Abel’s salary?
Subquery:
Copyright © 2007, Oracle. All rights reserved.
7 - 5
Subquery Syntax
• The subquery (inner query) executes before the main query
(outer query).
• The result of the subquery is used by the main query.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Copyright © 2007, Oracle. All rights reserved.
7 - 6
SELECT last_name, salary
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
Using a Subquery
11000
Copyright © 2007, Oracle. All rights reserved.
7 - 7
Guidelines for Using Subqueries
• Enclose subqueries in parentheses.
• Place subqueries on the right side of the comparison
condition for readability (However, the subquery can appear
on either side of the comparison operator.).
• Use single-row operators with single-row subqueries and
multiple-row operators with multiple-row subqueries.
Copyright © 2007, Oracle. All rights reserved.
7 - 8
Types of Subqueries
• Single-row subquery
• Multiple-row subquery
Main query
Subquery
returns
ST_CLERK
ST_CLERK
SA_MAN
Main query
Subquery
returns
Copyright © 2007, Oracle. All rights reserved.
7 - 9
Lesson Agenda
• Subquery: Types, syntax, and guidelines
• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 10
Single-Row Subqueries
• Return only one row
• Use single-row comparison operators
Greater than or equal to
>=
Less than
<
Less than or equal to
<=
Equal to
=
Not equal to
<>
Greater than
>
Meaning
Operator
Copyright © 2007, Oracle. All rights reserved.
7 - 11
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = ‘Taylor’)
AND salary >
(SELECT salary
FROM employees
WHERE last_name = ‘Taylor’);
Executing Single-Row Subqueries
SA_REP
8600
Copyright © 2007, Oracle. All rights reserved.
7 - 12
SELECT last_name, job_id, salary
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees);
Using Group Functions in a Subquery
2500
Copyright © 2007, Oracle. All rights reserved.
7 - 13
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
The HAVING Clause with Subqueries
• The Oracle server executes the subqueries first.
• The Oracle server returns results into the HAVING clause of
the main query.
2500
…
Copyright © 2007, Oracle. All rights reserved.
7 - 14
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
What Is Wrong with This Statement?
Single-row operator
with multiple-row
subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 15
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
No Rows Returned by the Inner Query
Subquery returns no rows because there is no
employee named “Haas.”
Copyright © 2007, Oracle. All rights reserved.
7 - 16
Lesson Agenda
• Subquery: Types, syntax, and guidelines
• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 17
Multiple-Row Subqueries
• Return more than one row
• Use multiple-row comparison operators
Must be preceded by =, !=, >, <, <=, >=.
Compares a value to every value in a list or
returned by a query. Evaluates to TRUE if the
query returns no rows.
ALL
Equal to any member in the list
IN
Must be preceded by =, !=, >, <, <=, >=.
Compares a value to each value in a list or
returned by a query. Evaluates to FALSE if the
query returns no rows.
ANY
Meaning
Operator
Copyright © 2007, Oracle. All rights reserved.
7 - 18
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
Using the ANY Operator
in Multiple-Row Subqueries
9000, 6000, 4200
…
Copyright © 2007, Oracle. All rights reserved.
7 - 19
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
Using the ALL Operator
in Multiple-Row Subqueries
9000, 6000, 4200
Copyright © 2007, Oracle. All rights reserved.
7 - 20
Lesson Agenda
• Subquery: Types, syntax, and guidelines
• Single-row subqueries:
– Group functions in a subquery
– HAVING clause with subqueries
• Multiple-row subqueries
– Use ALL or ANY operator
• Null values in a subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 21
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
Null Values in a Subquery
Copyright © 2007, Oracle. All rights reserved.
7 - 23
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Summary
In this lesson, you should have learned how to:
• Identify when a subquery can help solve a problem
• Write subqueries when a query is based on unknown values
Copyright © 2007, Oracle. All rights reserved.
7 - 24
Practice 7: Overview
This practice covers the following topics:
• Creating subqueries to query values based on unknown
criteria
• Using subqueries to find out the values that exist in one set
of data and not in another

More Related Content

Similar to Les07.ppt 0125566655656959652323265656565

Les06- Subqueries.ppt
Les06- Subqueries.pptLes06- Subqueries.ppt
Les06- Subqueries.ppt
gznfrch1
 
Silicon Valley JUG meetup July 18, 2018
Silicon Valley JUG meetup July 18, 2018Silicon Valley JUG meetup July 18, 2018
Silicon Valley JUG meetup July 18, 2018
Oracle Developers
 
Les11.ppt
Les11.pptLes11.ppt
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
Syed Zaid Irshad
 
Sql oracle
Sql oracleSql oracle
Sql oracle
Md.Abu Noman Shuvo
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
Vikas Gupta
 
Les06
Les06Les06
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
Ankit Surti
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Sql views
Sql viewsSql views
Sql views
arshid045
 
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Oracle Developers
 
plsql les02
 plsql les02 plsql les02
plsql les02
sasa_eldoby
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
RamishaRauf
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6Umair Amjad
 
Extending Oracle E-Business Suite with Ruby on Rails
Extending Oracle E-Business Suite with Ruby on RailsExtending Oracle E-Business Suite with Ruby on Rails
Extending Oracle E-Business Suite with Ruby on Rails
Raimonds Simanovskis
 
13 java beans
13 java beans13 java beans
13 java beanssnopteck
 
Consultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosConsultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datos
Caleb Gutiérrez
 

Similar to Les07.ppt 0125566655656959652323265656565 (20)

Les06- Subqueries.ppt
Les06- Subqueries.pptLes06- Subqueries.ppt
Les06- Subqueries.ppt
 
Les10
Les10Les10
Les10
 
Silicon Valley JUG meetup July 18, 2018
Silicon Valley JUG meetup July 18, 2018Silicon Valley JUG meetup July 18, 2018
Silicon Valley JUG meetup July 18, 2018
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Sql oracle
Sql oracleSql oracle
Sql oracle
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Les06
Les06Les06
Les06
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Sql views
Sql viewsSql views
Sql views
 
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6
 
Extending Oracle E-Business Suite with Ruby on Rails
Extending Oracle E-Business Suite with Ruby on RailsExtending Oracle E-Business Suite with Ruby on Rails
Extending Oracle E-Business Suite with Ruby on Rails
 
13 java beans
13 java beans13 java beans
13 java beans
 
Consultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosConsultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datos
 
Les09
Les09Les09
Les09
 

Recently uploaded

ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
muralinath2
 
Nutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technologyNutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technology
Lokesh Patil
 
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdfUnveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Erdal Coalmaker
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
sonaliswain16
 
Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.
Nistarini College, Purulia (W.B) India
 
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
muralinath2
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
silvermistyshot
 
NuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final versionNuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final version
pablovgd
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
IvanMallco1
 
Richard's entangled aventures in wonderland
Richard's entangled aventures in wonderlandRichard's entangled aventures in wonderland
Richard's entangled aventures in wonderland
Richard Gill
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
Scintica Instrumentation
 
Structural Classification Of Protein (SCOP)
Structural Classification Of Protein  (SCOP)Structural Classification Of Protein  (SCOP)
Structural Classification Of Protein (SCOP)
aishnasrivastava
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
Areesha Ahmad
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
Columbia Weather Systems
 
Structures and textures of metamorphic rocks
Structures and textures of metamorphic rocksStructures and textures of metamorphic rocks
Structures and textures of metamorphic rocks
kumarmathi863
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
moosaasad1975
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
YOGESH DOGRA
 
in vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptxin vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptx
yusufzako14
 
Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
ossaicprecious19
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
muralinath2
 

Recently uploaded (20)

ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
 
Nutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technologyNutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technology
 
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdfUnveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdf
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
 
Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.Nucleic Acid-its structural and functional complexity.
Nucleic Acid-its structural and functional complexity.
 
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
Circulatory system_ Laplace law. Ohms law.reynaults law,baro-chemo-receptors-...
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
 
NuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final versionNuGOweek 2024 Ghent - programme - final version
NuGOweek 2024 Ghent - programme - final version
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
 
Richard's entangled aventures in wonderland
Richard's entangled aventures in wonderlandRichard's entangled aventures in wonderland
Richard's entangled aventures in wonderland
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
 
Structural Classification Of Protein (SCOP)
Structural Classification Of Protein  (SCOP)Structural Classification Of Protein  (SCOP)
Structural Classification Of Protein (SCOP)
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
 
Structures and textures of metamorphic rocks
Structures and textures of metamorphic rocksStructures and textures of metamorphic rocks
Structures and textures of metamorphic rocks
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
 
in vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptxin vitro propagation of plants lecture note.pptx
in vitro propagation of plants lecture note.pptx
 
Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
 

Les07.ppt 0125566655656959652323265656565

  • 1. Copyright © 2007, Oracle. All rights reserved. Using Subqueries to Solve Queries
  • 2. Copyright © 2007, Oracle. All rights reserved. 7 - 2 Objectives After completing this lesson, you should be able to do the following: • Define subqueries • Describe the types of problems that the subqueries can solve • List the types of subqueries • Write single-row and multiple-row subqueries
  • 3. Copyright © 2007, Oracle. All rights reserved. 7 - 3 Lesson Agenda • Subquery: Types, syntax, and guidelines • Single-row subqueries: – Group functions in a subquery – HAVING clause with subqueries • Multiple-row subqueries – Use ALL or ANY operator • Null values in a subquery
  • 4. Copyright © 2007, Oracle. All rights reserved. 7 - 4 Using a Subquery to Solve a Problem Who has a salary greater than Abel’s? Which employees have salaries greater than Abel’s salary? Main query: What is Abel’s salary? Subquery:
  • 5. Copyright © 2007, Oracle. All rights reserved. 7 - 5 Subquery Syntax • The subquery (inner query) executes before the main query (outer query). • The result of the subquery is used by the main query. SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
  • 6. Copyright © 2007, Oracle. All rights reserved. 7 - 6 SELECT last_name, salary FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel'); Using a Subquery 11000
  • 7. Copyright © 2007, Oracle. All rights reserved. 7 - 7 Guidelines for Using Subqueries • Enclose subqueries in parentheses. • Place subqueries on the right side of the comparison condition for readability (However, the subquery can appear on either side of the comparison operator.). • Use single-row operators with single-row subqueries and multiple-row operators with multiple-row subqueries.
  • 8. Copyright © 2007, Oracle. All rights reserved. 7 - 8 Types of Subqueries • Single-row subquery • Multiple-row subquery Main query Subquery returns ST_CLERK ST_CLERK SA_MAN Main query Subquery returns
  • 9. Copyright © 2007, Oracle. All rights reserved. 7 - 9 Lesson Agenda • Subquery: Types, syntax, and guidelines • Single-row subqueries: – Group functions in a subquery – HAVING clause with subqueries • Multiple-row subqueries – Use ALL or ANY operator • Null values in a subquery
  • 10. Copyright © 2007, Oracle. All rights reserved. 7 - 10 Single-Row Subqueries • Return only one row • Use single-row comparison operators Greater than or equal to >= Less than < Less than or equal to <= Equal to = Not equal to <> Greater than > Meaning Operator
  • 11. Copyright © 2007, Oracle. All rights reserved. 7 - 11 SELECT last_name, job_id, salary FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = ‘Taylor’) AND salary > (SELECT salary FROM employees WHERE last_name = ‘Taylor’); Executing Single-Row Subqueries SA_REP 8600
  • 12. Copyright © 2007, Oracle. All rights reserved. 7 - 12 SELECT last_name, job_id, salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees); Using Group Functions in a Subquery 2500
  • 13. Copyright © 2007, Oracle. All rights reserved. 7 - 13 SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50); The HAVING Clause with Subqueries • The Oracle server executes the subqueries first. • The Oracle server returns results into the HAVING clause of the main query. 2500 …
  • 14. Copyright © 2007, Oracle. All rights reserved. 7 - 14 SELECT employee_id, last_name FROM employees WHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id); What Is Wrong with This Statement? Single-row operator with multiple-row subquery
  • 15. Copyright © 2007, Oracle. All rights reserved. 7 - 15 SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); No Rows Returned by the Inner Query Subquery returns no rows because there is no employee named “Haas.”
  • 16. Copyright © 2007, Oracle. All rights reserved. 7 - 16 Lesson Agenda • Subquery: Types, syntax, and guidelines • Single-row subqueries: – Group functions in a subquery – HAVING clause with subqueries • Multiple-row subqueries – Use ALL or ANY operator • Null values in a subquery
  • 17. Copyright © 2007, Oracle. All rights reserved. 7 - 17 Multiple-Row Subqueries • Return more than one row • Use multiple-row comparison operators Must be preceded by =, !=, >, <, <=, >=. Compares a value to every value in a list or returned by a query. Evaluates to TRUE if the query returns no rows. ALL Equal to any member in the list IN Must be preceded by =, !=, >, <, <=, >=. Compares a value to each value in a list or returned by a query. Evaluates to FALSE if the query returns no rows. ANY Meaning Operator
  • 18. Copyright © 2007, Oracle. All rights reserved. 7 - 18 SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; Using the ANY Operator in Multiple-Row Subqueries 9000, 6000, 4200 …
  • 19. Copyright © 2007, Oracle. All rights reserved. 7 - 19 SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; Using the ALL Operator in Multiple-Row Subqueries 9000, 6000, 4200
  • 20. Copyright © 2007, Oracle. All rights reserved. 7 - 20 Lesson Agenda • Subquery: Types, syntax, and guidelines • Single-row subqueries: – Group functions in a subquery – HAVING clause with subqueries • Multiple-row subqueries – Use ALL or ANY operator • Null values in a subquery
  • 21. Copyright © 2007, Oracle. All rights reserved. 7 - 21 SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr); Null Values in a Subquery
  • 22. Copyright © 2007, Oracle. All rights reserved. 7 - 23 SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); Summary In this lesson, you should have learned how to: • Identify when a subquery can help solve a problem • Write subqueries when a query is based on unknown values
  • 23. Copyright © 2007, Oracle. All rights reserved. 7 - 24 Practice 7: Overview This practice covers the following topics: • Creating subqueries to query values based on unknown criteria • Using subqueries to find out the values that exist in one set of data and not in another