SlideShare a Scribd company logo
1 of 19
Download to read offline
Copyright © 2004, Oracle. All rights reserved.
Using Subqueries to Solve Queries
Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do
the following:
• Define subqueries
• Describe the types of problems that subqueries
can solve
• List the types of subqueries
• Write single-row and multiple-row subqueries
Copyright © 2004, Oracle. All rights reserved.
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 © 2004, Oracle. All rights reserved.
• The subquery (inner query) executes once 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);
Subquery Syntax
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
Using a Subquery
11000
Copyright © 2004, Oracle. All rights reserved.
Guidelines for Using Subqueries
• Enclose subqueries in parentheses.
• Place subqueries on the right side of the
comparison condition.
• The ORDER BY clause in the subquery is not
needed unless you are performing Top-N analysis.
• Use single-row operators with single-row
subqueries, and use multiple-row operators with
multiple-row subqueries.
Copyright © 2004, Oracle. All rights reserved.
Types of Subqueries
• Single-row subquery
• Multiple-row subquery
Main query
Subquery
returns
ST_CLERK
ST_CLERK
SA_MAN
Main query
Subquery
returns
Copyright © 2004, Oracle. All rights reserved.
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>
MeaningOperator
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary >
(SELECT salary
FROM employees
WHERE employee_id = 143);
Executing Single-Row Subqueries
ST_CLERK
2600
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id, salary
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees);
Using Group Functions in a Subquery
2500
Copyright © 2004, Oracle. All rights reserved.
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 subqueries first.
• The Oracle server returns results into the HAVING
clause of the main query.
2500
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
What Is Wrong with This Statement?
ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
Single-row operator with multiple-row subquery
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
Will This Statement Return Rows?
no rows selected
Subquery returns no values.
Copyright © 2004, Oracle. All rights reserved.
Multiple-Row Subqueries
• Return more than one row
• Use multiple-row comparison operators
Compare value to every value returned by
the subquery
ALL
Equal to any member in the listIN
Compare value to each value returned by the
subquery
ANY
MeaningOperator
Copyright © 2004, Oracle. All rights reserved.
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 © 2004, Oracle. All rights reserved.
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 © 2004, Oracle. All rights reserved.
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
no rows selected
Null Values in a Subquery
Copyright © 2004, Oracle. All rights reserved.
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
question
• Write subqueries when a query is based on
unknown values
Copyright © 2004, Oracle. All rights reserved.
Practice 6: Overview
This practice covers the following topics:
• Creating subqueries to query values based on
unknown criteria
• Using subqueries to find out which values exist in
one set of data and not in another

More Related Content

What's hot

Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group FunctionsSalman Memon
 
Manipulating Data Oracle Data base
Manipulating Data Oracle Data baseManipulating Data Oracle Data base
Manipulating Data Oracle Data baseSalman Memon
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseSalman Memon
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseSalman Memon
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle databaseSalman Memon
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Salman Memon
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)Achmad Solichin
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueriesecomputernotes
 

What's hot (20)

Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
 
Les01
Les01Les01
Les01
 
Manipulating Data Oracle Data base
Manipulating Data Oracle Data baseManipulating Data Oracle Data base
Manipulating Data Oracle Data base
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data Base
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Sql oracle
Sql oracleSql oracle
Sql oracle
 
Database Objects
Database ObjectsDatabase Objects
Database Objects
 
plsql les06
 plsql les06 plsql les06
plsql les06
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Les02
Les02Les02
Les02
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
plsql les01
 plsql les01 plsql les01
plsql les01
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Oracle views
Oracle viewsOracle views
Oracle views
 
Les01
Les01Les01
Les01
 

Viewers also liked

Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260dlwodud77
 
Supply chain mgmt
Supply chain mgmtSupply chain mgmt
Supply chain mgmtAjit Dahal
 
Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260dlwodud77
 
Jae Lee flipbook Film 260
Jae Lee flipbook Film 260Jae Lee flipbook Film 260
Jae Lee flipbook Film 260dlwodud77
 
Financial needs & sources of finance of a part 1
Financial needs & sources of finance of a part 1Financial needs & sources of finance of a part 1
Financial needs & sources of finance of a part 1Ajit Dahal
 
Sources of finance part 2
Sources of finance part 2Sources of finance part 2
Sources of finance part 2Ajit Dahal
 
Advantages of e hrm
Advantages of e hrmAdvantages of e hrm
Advantages of e hrmAjit Dahal
 

Viewers also liked (10)

Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260
 
Supply chain mgmt
Supply chain mgmtSupply chain mgmt
Supply chain mgmt
 
Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260Jae Lee Flipbook Film 260
Jae Lee Flipbook Film 260
 
Jae Lee flipbook Film 260
Jae Lee flipbook Film 260Jae Lee flipbook Film 260
Jae Lee flipbook Film 260
 
Siklus hidup sistem
Siklus hidup sistemSiklus hidup sistem
Siklus hidup sistem
 
Financial needs & sources of finance of a part 1
Financial needs & sources of finance of a part 1Financial needs & sources of finance of a part 1
Financial needs & sources of finance of a part 1
 
Sources of finance part 2
Sources of finance part 2Sources of finance part 2
Sources of finance part 2
 
Sia 3-siklus pokok sia
Sia 3-siklus pokok siaSia 3-siklus pokok sia
Sia 3-siklus pokok sia
 
SIA - Siklus Konversi
SIA - Siklus KonversiSIA - Siklus Konversi
SIA - Siklus Konversi
 
Advantages of e hrm
Advantages of e hrmAdvantages of e hrm
Advantages of e hrm
 

Similar to Les06

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
 
Lesson06 使用子查询
Lesson06 使用子查询Lesson06 使用子查询
Lesson06 使用子查询renguzi
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queriesSyed Zaid Irshad
 
Subconsultas
SubconsultasSubconsultas
SubconsultasMaria
 
Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565nourhandardeer3
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptDrZeeshanBhatti
 
Les06- Subqueries.ppt
Les06- Subqueries.pptLes06- Subqueries.ppt
Les06- Subqueries.pptgznfrch1
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句renguzi
 
Oracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptOracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptDrZeeshanBhatti
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptxRamishaRauf
 

Similar to Les06 (20)

Les02
Les02Les02
Les02
 
Les06
Les06Les06
Les06
 
Les06
Les06Les06
Les06
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 
Lesson06 使用子查询
Lesson06 使用子查询Lesson06 使用子查询
Lesson06 使用子查询
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 
Oracle examples
Oracle examplesOracle examples
Oracle examples
 
Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565
 
Les04
Les04Les04
Les04
 
Les04
Les04Les04
Les04
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Les02.ppt
Les02.pptLes02.ppt
Les02.ppt
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
Les06- Subqueries.ppt
Les06- Subqueries.pptLes06- Subqueries.ppt
Les06- Subqueries.ppt
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Oracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptOracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.ppt
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 

More from Abrianto Nugraha (20)

Ds sn is-02
Ds sn is-02Ds sn is-02
Ds sn is-02
 
Ds sn is-01
Ds sn is-01Ds sn is-01
Ds sn is-01
 
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkapPertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
 
04 pemodelan spk
04 pemodelan spk04 pemodelan spk
04 pemodelan spk
 
02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised
 
01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan
 
Pertemuan 7
Pertemuan 7Pertemuan 7
Pertemuan 7
 
Pertemuan 7 dan_8
Pertemuan 7 dan_8Pertemuan 7 dan_8
Pertemuan 7 dan_8
 
Pertemuan 5
Pertemuan 5Pertemuan 5
Pertemuan 5
 
Pertemuan 6
Pertemuan 6Pertemuan 6
Pertemuan 6
 
Pertemuan 4
Pertemuan 4Pertemuan 4
Pertemuan 4
 
Pertemuan 3
Pertemuan 3Pertemuan 3
Pertemuan 3
 
Pertemuan 2
Pertemuan 2Pertemuan 2
Pertemuan 2
 
Pertemuan 1
Pertemuan 1Pertemuan 1
Pertemuan 1
 
Modul 1 mengambil nilai parameter
Modul 1   mengambil nilai parameterModul 1   mengambil nilai parameter
Modul 1 mengambil nilai parameter
 
Modul 3 object oriented programming dalam php
Modul 3   object oriented programming dalam phpModul 3   object oriented programming dalam php
Modul 3 object oriented programming dalam php
 
Modul 2 menyimpan ke database
Modul 2  menyimpan ke databaseModul 2  menyimpan ke database
Modul 2 menyimpan ke database
 
Pbo 7
Pbo 7Pbo 7
Pbo 7
 
Pbo 6
Pbo 6Pbo 6
Pbo 6
 
Pbo 4
Pbo 4Pbo 4
Pbo 4
 

Recently uploaded

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Les06

  • 1. Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries
  • 2. Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Define subqueries • Describe the types of problems that subqueries can solve • List the types of subqueries • Write single-row and multiple-row subqueries
  • 3. Copyright © 2004, Oracle. All rights reserved. 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:
  • 4. Copyright © 2004, Oracle. All rights reserved. • The subquery (inner query) executes once 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); Subquery Syntax
  • 5. Copyright © 2004, Oracle. All rights reserved. SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel'); Using a Subquery 11000
  • 6. Copyright © 2004, Oracle. All rights reserved. Guidelines for Using Subqueries • Enclose subqueries in parentheses. • Place subqueries on the right side of the comparison condition. • The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis. • Use single-row operators with single-row subqueries, and use multiple-row operators with multiple-row subqueries.
  • 7. Copyright © 2004, Oracle. All rights reserved. Types of Subqueries • Single-row subquery • Multiple-row subquery Main query Subquery returns ST_CLERK ST_CLERK SA_MAN Main query Subquery returns
  • 8. Copyright © 2004, Oracle. All rights reserved. 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> MeaningOperator
  • 9. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, salary FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > (SELECT salary FROM employees WHERE employee_id = 143); Executing Single-Row Subqueries ST_CLERK 2600
  • 10. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees); Using Group Functions in a Subquery 2500
  • 11. Copyright © 2004, Oracle. All rights reserved. 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 subqueries first. • The Oracle server returns results into the HAVING clause of the main query. 2500
  • 12. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name FROM employees WHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id); What Is Wrong with This Statement? ERROR at line 4: ORA-01427: single-row subquery returns more than one row Single-row operator with multiple-row subquery
  • 13. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); Will This Statement Return Rows? no rows selected Subquery returns no values.
  • 14. Copyright © 2004, Oracle. All rights reserved. Multiple-Row Subqueries • Return more than one row • Use multiple-row comparison operators Compare value to every value returned by the subquery ALL Equal to any member in the listIN Compare value to each value returned by the subquery ANY MeaningOperator
  • 15. Copyright © 2004, Oracle. All rights reserved. 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 …
  • 16. Copyright © 2004, Oracle. All rights reserved. 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
  • 17. Copyright © 2004, Oracle. All rights reserved. SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr); no rows selected Null Values in a Subquery
  • 18. Copyright © 2004, Oracle. All rights reserved. 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 question • Write subqueries when a query is based on unknown values
  • 19. Copyright © 2004, Oracle. All rights reserved. Practice 6: Overview This practice covers the following topics: • Creating subqueries to query values based on unknown criteria • Using subqueries to find out which values exist in one set of data and not in another