SlideShare a Scribd company logo
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

PPT
Subqueries -Oracle DataBase
PPT
Producing Readable Output with iSQL*Plus - Oracle Data Base
PPT
plsql Les07
Subqueries -Oracle DataBase
Producing Readable Output with iSQL*Plus - Oracle Data Base
plsql Les07

What's hot (20)

PPT
Aggregating Data Using Group Functions
PPT
plsql Les09
PPT
Manipulating Data Oracle Data base
PPT
Writing Basic SQL SELECT Statements
PPT
Displaying Data from Multiple Tables - Oracle Data Base
PPT
plsql Les08
PPT
Restricting and Sorting Data - Oracle Data Base
PPT
Creating Views - oracle database
PPT
Including Constraints -Oracle Data base
PPT
Sql oracle
PPT
Database Objects
PPT
plsql les06
PPT
Single-Row Functions in orcale Data base
PPT
Les02
PPT
Les08 (manipulating data)
PPT
plsql les01
PPT
e computer notes - Subqueries
PPTX
Oracle views
PPT
Les01
Aggregating Data Using Group Functions
plsql Les09
Manipulating Data Oracle Data base
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables - Oracle Data Base
plsql Les08
Restricting and Sorting Data - Oracle Data Base
Creating Views - oracle database
Including Constraints -Oracle Data base
Sql oracle
Database Objects
plsql les06
Single-Row Functions in orcale Data base
Les02
Les08 (manipulating data)
plsql les01
e computer notes - Subqueries
Oracle views
Les01
Ad

Viewers also liked (10)

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

Similar to Les06 (20)

PPT
SUBQUERY materi kegiatan pembelajaran 1.1
PPT
PPT
Les06 (using subqueries to solve queries)
PPT
PPT
Les06- Subqueries.ppt
PPT
Subconsultas
PPT
Les06
PPT
Les07.ppt 0125566655656959652323265656565
PPT
Les06[1]Subqueries
PPT
Using subqueries to solve queries
PPT
7992267.ppt
PPT
SQL WORKSHOP::Lecture 6
PPTX
Ch 6 Sub Query.pptx
PPT
SQL subquery
PDF
Lesson06 使用子查询
PPTX
Week6_Theory.pptx
PPT
Oracle examples
PPTX
Unit 3-p2relationaldatabasescodeing.pptx
PPTX
Week8_Theory.pptx
SUBQUERY materi kegiatan pembelajaran 1.1
Les06 (using subqueries to solve queries)
Les06- Subqueries.ppt
Subconsultas
Les06
Les07.ppt 0125566655656959652323265656565
Les06[1]Subqueries
Using subqueries to solve queries
7992267.ppt
SQL WORKSHOP::Lecture 6
Ch 6 Sub Query.pptx
SQL subquery
Lesson06 使用子查询
Week6_Theory.pptx
Oracle examples
Unit 3-p2relationaldatabasescodeing.pptx
Week8_Theory.pptx

More from Abrianto Nugraha (20)

PPT
Ds sn is-02
PPT
Ds sn is-01
PPT
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
PPTX
04 pemodelan spk
PPTX
02 sistem pengambilan-keputusan_revised
PPTX
01 pengantar sistem-pendukung_keputusan
PPT
Pertemuan 7
PPT
Pertemuan 7 dan_8
PPT
Pertemuan 5
PPT
Pertemuan 6
PPT
Pertemuan 4
PPT
Pertemuan 3
PPT
Pertemuan 2
PPT
Pertemuan 1
DOCX
Modul 1 mengambil nilai parameter
DOCX
Modul 3 object oriented programming dalam php
DOCX
Modul 2 menyimpan ke database
Ds sn is-02
Ds sn is-01
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
04 pemodelan spk
02 sistem pengambilan-keputusan_revised
01 pengantar sistem-pendukung_keputusan
Pertemuan 7
Pertemuan 7 dan_8
Pertemuan 5
Pertemuan 6
Pertemuan 4
Pertemuan 3
Pertemuan 2
Pertemuan 1
Modul 1 mengambil nilai parameter
Modul 3 object oriented programming dalam php
Modul 2 menyimpan ke database

Recently uploaded (20)

PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Introduction to Building Materials
PDF
Computing-Curriculum for Schools in Ghana
PDF
IGGE1 Understanding the Self1234567891011
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Trump Administration's workforce development strategy
PDF
advance database management system book.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
Classroom Observation Tools for Teachers
PDF
1_English_Language_Set_2.pdf probationary
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Introduction to Building Materials
Computing-Curriculum for Schools in Ghana
IGGE1 Understanding the Self1234567891011
A systematic review of self-coping strategies used by university students to ...
Hazard Identification & Risk Assessment .pdf
Supply Chain Operations Speaking Notes -ICLT Program
Trump Administration's workforce development strategy
advance database management system book.pdf
Empowerment Technology for Senior High School Guide
Classroom Observation Tools for Teachers
1_English_Language_Set_2.pdf probationary
History, Philosophy and sociology of education (1).pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
What if we spent less time fighting change, and more time building what’s rig...
Unit 4 Skeletal System.ppt.pptxopresentatiom
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Final Presentation General Medicine 03-08-2024.pptx

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