SlideShare a Scribd company logo
1 of 27
Copyright  Oracle Corporation, 1997. All rights reserved.
5
Subqueries
5-2 Copyright  Oracle Corporation, 1997. All rights reserved.
Objectives
At the end of this lesson, you should be
able to:
• Describe the types of problems that
subqueries can solve
• Define subqueries
• List the types of subqueries
• Write single-row and multiple-row
subqueries
5-3 Copyright  Oracle Corporation, 1997. All rights reserved.
Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’s?”
“Which employees have a salary greater
than Jones’s salary?”
Main Query
?
“What is Jones’s salary?”
?
Subquery
5-4 Copyright  Oracle Corporation, 1997. All rights reserved.
Subqueries
• The subquery (inner query) executes
once before the main query.
• The result of the subquery is used by
the main query (outer query).
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
5-5 Copyright  Oracle Corporation, 1997. All rights reserved.
2975
SQL> SELECT ename
2 FROM emp
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
Using a Subquery
ENAME
----------
KING
FORD
SCOTT
5-6 Copyright  Oracle Corporation, 1997. All rights reserved.
Guidelines for Using Subqueries
• Enclose subqueries in parentheses.
• Place subqueries on the right side of
the comparison operator.
• Do not add an ORDER BY clause to a
subquery.
• Use single-row operators with single-
row subqueries.
• Use multiple-row operators with
multiple-row subqueries.
5-7 Copyright  Oracle Corporation, 1997. All rights reserved.
Types of Subqueries
• Single-row subquery
Main query
Subquery
returns
CLERK
• Multiple-row subquery
CLERK
MANAGER
Main query
Subquery
returns
• Multiple-column subquery
CLERK 7900
MANAGER 7698
Main query
Subquery
returns
5-8 Copyright  Oracle Corporation, 1997. All rights reserved.
Single-Row Subqueries
• Return only one row
• Use single-row comparison operators
Operator
=
>
>=
<
<=
<>
Meaning
Equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Not equal to
5-9 Copyright  Oracle Corporation, 1997. All rights reserved.
Executing Single-Row Subqueries
CLERK
1100
ENAME JOB
---------- ---------
MILLER CLERK
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal >
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);
5-10 Copyright  Oracle Corporation, 1997. All rights reserved.
Using Group Functions
in a Subquery
800
ENAME JOB SAL
---------- --------- ---------
SMITH CLERK 800
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);
5-11 Copyright  Oracle Corporation, 1997. All rights reserved.
HAVING Clause with Subqueries
• The Oracle10 Server executes
subqueries first.
• The Oracle10 Server returns results into
the main query’s HAVING clause.
800
SQL> SELECT deptno, MIN(sal)
2 FROM emp
3 GROUP BY deptno
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
5-12 Copyright  Oracle Corporation, 1997. All rights reserved.
What Is Wrong
with This Statement?
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows selected
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp
6 GROUP BY deptno);
5-13 Copyright  Oracle Corporation, 1997. All rights reserved.
Will This Statement Work?
no rows selected
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
5-14 Copyright  Oracle Corporation, 1997. All rights reserved.
Multiple-Row Subqueries
• Return more than one row
• Use multiple-row comparison operators
Operator
IN
ANY
ALL
Meaning
Equal to any member in the list
Compare value to each value returned by
the subquery
Compare value to every value returned by
the subquery
5-15 Copyright  Oracle Corporation, 1997. All rights reserved.
Using ANY Operator
in Multiple-Row Subqueries
950
800
1100
1300
EMPNO ENAME JOB
--------- ---------- ---------
7654 MARTIN SALESMAN
7521 WARD SALESMAN
SQL> SELECT empno, ename, job
2 FROM emp
3 WHERE sal < ANY
4 (SELECT sal
5 FROM emp
6 WHERE job = 'CLERK')
7 AND job <> 'CLERK';
5-16 Copyright  Oracle Corporation, 1997. All rights reserved.
Using ALL Operator
in Multiple-Row Subqueries
2916.6667
2175
1566.6667
EMPNO ENAME JOB
--------- ---------- ---------
7839 KING PRESIDENT
7566 JONES MANAGER
7902 FORD ANALYST
7788 SCOTT ANALYST
SQL> SELECT empno, ename, job
2 FROM emp
3 WHERE sal > ALL
4 (SELECT avg(sal)
5 FROM emp
6 GROUP BY deptno)
5-17 Copyright  Oracle Corporation, 1997. All rights reserved.
Multiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
Main query
compares
MANAGER 10
Values from a multiple-row and
multiple-column subquery
SALESMAN 30
MANAGER 10
CLERK 20
to
5-18 Copyright  Oracle Corporation, 1997. All rights reserved.
Using Multiple-Column
Subqueries
Display the name, department number, salary,
and commission of any employee whose salary
and commission matches both the commission
and salary of any employee in department 30.
SQL> SELECT ename, deptno, sal, comm
2 FROM emp
3 WHERE (sal, NVL(comm,-1)) IN
4 (SELECT sal, NVL(comm,-1)
5 FROM emp
6 WHERE deptno = 30);
5-19 Copyright  Oracle Corporation, 1997. All rights reserved.
Column Comparisons
Pairwise
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
Nonpairwise
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
5-20 Copyright  Oracle Corporation, 1997. All rights reserved.
Nonpairwise Comparison
Subquery
SQL> SELECT ename, deptno, sal, comm
2 FROM emp
3 WHERE sal IN (SELECT sal
4 FROM emp
5 WHERE deptno = 30)
6 AND
7 NVL(comm,-1) IN (SELECT NVL(comm,-1)
8 FROM emp
9 WHERE deptno = 30);
Display the name, department number, salary,
and commission of any employee whose salary
and commission matches the commission and
salary of any employee in department 30.
5-21 Copyright  Oracle Corporation, 1997. All rights reserved.
Modifying the EMP Table
• Assume that salary and commission for Clark
are modified.
• Salary is changed to $1500 and commission to
$300.
ENAME SAL COMM
---------- --------- ---------
...
CLARK 1500 300
...
ALLEN 1600 300
TURNER 1500 0
...
14 rows selected.
5-22 Copyright  Oracle Corporation, 1997. All rights reserved.
Pairwise Subquery
SQL> SELECT ename, deptno, sal, comm
2 FROM emp
3 WHERE (sal, NVL(comm,-1)) IN
4 (SELECT sal, NVL(comm,-1)
5 FROM emp
6 WHERE deptno = 30);
ENAME DEPTNO SAL COMM
---------- --------- --------- ---------
JAMES 30 950
WARD 30 1250 500
MARTIN 30 1250 1400
TURNER 30 1500 0
ALLEN 30 1600 300
BLAKE 30 2850
6 rows selected.
5-23 Copyright  Oracle Corporation, 1997. All rights reserved.
SQL> SELECT ename,deptno, sal, comm
2 FROM emp
3 WHERE sal IN (SELECT sal
4 FROM emp
5 WHERE deptno = 30)
6 AND
7 NVL(comm,-1) IN (SELECT NVL(comm,-1)
8 FROM emp
9 WHERE deptno = 30);
Nonpairwise Subquery
ENAME DEPTNO SAL COMM
---------- --------- --------- ---------
JAMES 30 950
BLAKE 30 2850
TURNER 30 1500 0
CLARK 10 1500 300
...
7 rows selected.
5-24 Copyright  Oracle Corporation, 1997. All rights reserved.
Null Values in a Subquery
SQL> SELECT employee.ename
2 FROM emp employee
3 WHERE employee.empno NOT IN
(SELECT manager.mgr
FROM emp manager);
no rows selected.
5-25 Copyright  Oracle Corporation, 1997. All rights reserved.
Using a Subquery
in the FROM Clause
ENAME SAL DEPTNO SALAVG
---------- --------- --------- ----------
KING 5000 10 2916.6667
JONES 2975 20 2175
SCOTT 3000 20 2175
...
6 rows selected.
SQL> SELECT a.ename, a.sal, a.deptno, b.salavg
2 FROM emp a, (SELECT deptno, avg(sal) salavg
3 FROM emp
4 GROUP BY deptno) b
5 WHERE a.deptno = b.deptno
6 AND a.sal > b.salavg;
5-26 Copyright  Oracle Corporation, 1997. All rights reserved.
Summary
• A multiple-column subquery returns
more than one column.
• Column comparisons in a multiple-
column comparisons can be pairwise or
nonpairwise.
• A multiple-column subquery can also be
used in the FROM clause of a SELECT
statement.
5-27 Copyright  Oracle Corporation, 1997. All rights reserved.
Practice Overview
Creating subqueries

More Related Content

Similar to 7992267.ppt

Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Datasiavosh kaviani
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsAndrej Pashchenko
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6Umair Amjad
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptxRamishaRauf
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBaseSalman Memon
 
The Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLThe Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLConnor McDonald
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueriessiavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Datasiavosh kaviani
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statementssiavosh kaviani
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQLConnor McDonald
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلMohamed Moustafa
 
Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Connor McDonald
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.pptmeenu851211
 

Similar to 7992267.ppt (20)

Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6SQL WORKSHOP::Lecture 6
SQL WORKSHOP::Lecture 6
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
The Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLThe Five Best Things To Happen To SQL
The Five Best Things To Happen To SQL
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
Les01.ppt
Les01.pptLes01.ppt
Les01.ppt
 
Les01.pptx
Les01.pptxLes01.pptx
Les01.pptx
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
Sql2
Sql2Sql2
Sql2
 
Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 

Recently uploaded

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 

Recently uploaded (20)

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 

7992267.ppt

  • 1. Copyright  Oracle Corporation, 1997. All rights reserved. 5 Subqueries
  • 2. 5-2 Copyright  Oracle Corporation, 1997. All rights reserved. Objectives At the end of this lesson, you should be able to: • Describe the types of problems that subqueries can solve • Define subqueries • List the types of subqueries • Write single-row and multiple-row subqueries
  • 3. 5-3 Copyright  Oracle Corporation, 1997. All rights reserved. Using a Subquery to Solve a Problem “Who has a salary greater than Jones’s?” “Which employees have a salary greater than Jones’s salary?” Main Query ? “What is Jones’s salary?” ? Subquery
  • 4. 5-4 Copyright  Oracle Corporation, 1997. All rights reserved. Subqueries • The subquery (inner query) executes once before the main query. • The result of the subquery is used by the main query (outer query). SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
  • 5. 5-5 Copyright  Oracle Corporation, 1997. All rights reserved. 2975 SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno=7566); Using a Subquery ENAME ---------- KING FORD SCOTT
  • 6. 5-6 Copyright  Oracle Corporation, 1997. All rights reserved. Guidelines for Using Subqueries • Enclose subqueries in parentheses. • Place subqueries on the right side of the comparison operator. • Do not add an ORDER BY clause to a subquery. • Use single-row operators with single- row subqueries. • Use multiple-row operators with multiple-row subqueries.
  • 7. 5-7 Copyright  Oracle Corporation, 1997. All rights reserved. Types of Subqueries • Single-row subquery Main query Subquery returns CLERK • Multiple-row subquery CLERK MANAGER Main query Subquery returns • Multiple-column subquery CLERK 7900 MANAGER 7698 Main query Subquery returns
  • 8. 5-8 Copyright  Oracle Corporation, 1997. All rights reserved. Single-Row Subqueries • Return only one row • Use single-row comparison operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  • 9. 5-9 Copyright  Oracle Corporation, 1997. All rights reserved. Executing Single-Row Subqueries CLERK 1100 ENAME JOB ---------- --------- MILLER CLERK SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8 (SELECT sal 9 FROM emp 10 WHERE empno = 7876);
  • 10. 5-10 Copyright  Oracle Corporation, 1997. All rights reserved. Using Group Functions in a Subquery 800 ENAME JOB SAL ---------- --------- --------- SMITH CLERK 800 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp);
  • 11. 5-11 Copyright  Oracle Corporation, 1997. All rights reserved. HAVING Clause with Subqueries • The Oracle10 Server executes subqueries first. • The Oracle10 Server returns results into the main query’s HAVING clause. 800 SQL> SELECT deptno, MIN(sal) 2 FROM emp 3 GROUP BY deptno 4 HAVING MIN(sal) > 5 (SELECT MIN(sal) 6 FROM emp 7 WHERE deptno = 20);
  • 12. 5-12 Copyright  Oracle Corporation, 1997. All rights reserved. What Is Wrong with This Statement? ERROR: ORA-01427: single-row subquery returns more than one row no rows selected SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp 6 GROUP BY deptno);
  • 13. 5-13 Copyright  Oracle Corporation, 1997. All rights reserved. Will This Statement Work? no rows selected SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE ename='SMYTHE');
  • 14. 5-14 Copyright  Oracle Corporation, 1997. All rights reserved. Multiple-Row Subqueries • Return more than one row • Use multiple-row comparison operators Operator IN ANY ALL Meaning Equal to any member in the list Compare value to each value returned by the subquery Compare value to every value returned by the subquery
  • 15. 5-15 Copyright  Oracle Corporation, 1997. All rights reserved. Using ANY Operator in Multiple-Row Subqueries 950 800 1100 1300 EMPNO ENAME JOB --------- ---------- --------- 7654 MARTIN SALESMAN 7521 WARD SALESMAN SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4 (SELECT sal 5 FROM emp 6 WHERE job = 'CLERK') 7 AND job <> 'CLERK';
  • 16. 5-16 Copyright  Oracle Corporation, 1997. All rights reserved. Using ALL Operator in Multiple-Row Subqueries 2916.6667 2175 1566.6667 EMPNO ENAME JOB --------- ---------- --------- 7839 KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal > ALL 4 (SELECT avg(sal) 5 FROM emp 6 GROUP BY deptno)
  • 17. 5-17 Copyright  Oracle Corporation, 1997. All rights reserved. Multiple-Column Subqueries Main query MANAGER 10 Subquery SALESMAN 30 MANAGER 10 CLERK 20 Main query compares MANAGER 10 Values from a multiple-row and multiple-column subquery SALESMAN 30 MANAGER 10 CLERK 20 to
  • 18. 5-18 Copyright  Oracle Corporation, 1997. All rights reserved. Using Multiple-Column Subqueries Display the name, department number, salary, and commission of any employee whose salary and commission matches both the commission and salary of any employee in department 30. SQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE (sal, NVL(comm,-1)) IN 4 (SELECT sal, NVL(comm,-1) 5 FROM emp 6 WHERE deptno = 30);
  • 19. 5-19 Copyright  Oracle Corporation, 1997. All rights reserved. Column Comparisons Pairwise SAL COMM 1600 300 1250 500 1250 1400 2850 1500 0 950 Nonpairwise SAL COMM 1600 300 1250 500 1250 1400 2850 1500 0 950
  • 20. 5-20 Copyright  Oracle Corporation, 1997. All rights reserved. Nonpairwise Comparison Subquery SQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE sal IN (SELECT sal 4 FROM emp 5 WHERE deptno = 30) 6 AND 7 NVL(comm,-1) IN (SELECT NVL(comm,-1) 8 FROM emp 9 WHERE deptno = 30); Display the name, department number, salary, and commission of any employee whose salary and commission matches the commission and salary of any employee in department 30.
  • 21. 5-21 Copyright  Oracle Corporation, 1997. All rights reserved. Modifying the EMP Table • Assume that salary and commission for Clark are modified. • Salary is changed to $1500 and commission to $300. ENAME SAL COMM ---------- --------- --------- ... CLARK 1500 300 ... ALLEN 1600 300 TURNER 1500 0 ... 14 rows selected.
  • 22. 5-22 Copyright  Oracle Corporation, 1997. All rights reserved. Pairwise Subquery SQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE (sal, NVL(comm,-1)) IN 4 (SELECT sal, NVL(comm,-1) 5 FROM emp 6 WHERE deptno = 30); ENAME DEPTNO SAL COMM ---------- --------- --------- --------- JAMES 30 950 WARD 30 1250 500 MARTIN 30 1250 1400 TURNER 30 1500 0 ALLEN 30 1600 300 BLAKE 30 2850 6 rows selected.
  • 23. 5-23 Copyright  Oracle Corporation, 1997. All rights reserved. SQL> SELECT ename,deptno, sal, comm 2 FROM emp 3 WHERE sal IN (SELECT sal 4 FROM emp 5 WHERE deptno = 30) 6 AND 7 NVL(comm,-1) IN (SELECT NVL(comm,-1) 8 FROM emp 9 WHERE deptno = 30); Nonpairwise Subquery ENAME DEPTNO SAL COMM ---------- --------- --------- --------- JAMES 30 950 BLAKE 30 2850 TURNER 30 1500 0 CLARK 10 1500 300 ... 7 rows selected.
  • 24. 5-24 Copyright  Oracle Corporation, 1997. All rights reserved. Null Values in a Subquery SQL> SELECT employee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN (SELECT manager.mgr FROM emp manager); no rows selected.
  • 25. 5-25 Copyright  Oracle Corporation, 1997. All rights reserved. Using a Subquery in the FROM Clause ENAME SAL DEPTNO SALAVG ---------- --------- --------- ---------- KING 5000 10 2916.6667 JONES 2975 20 2175 SCOTT 3000 20 2175 ... 6 rows selected. SQL> SELECT a.ename, a.sal, a.deptno, b.salavg 2 FROM emp a, (SELECT deptno, avg(sal) salavg 3 FROM emp 4 GROUP BY deptno) b 5 WHERE a.deptno = b.deptno 6 AND a.sal > b.salavg;
  • 26. 5-26 Copyright  Oracle Corporation, 1997. All rights reserved. Summary • A multiple-column subquery returns more than one column. • Column comparisons in a multiple- column comparisons can be pairwise or nonpairwise. • A multiple-column subquery can also be used in the FROM clause of a SELECT statement.
  • 27. 5-27 Copyright  Oracle Corporation, 1997. All rights reserved. Practice Overview Creating subqueries