SlideShare a Scribd company logo
Copyright  Oracle Corporation, 1998. All rights reserved.
6
Subqueries
6-2 Copyright  Oracle Corporation, 1998. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Describe the types of problems that
subqueries can solve
• Define subqueries
• List the types of subqueries
• Write single-row and multiple-row
subqueries
6-3 Copyright  Oracle Corporation, 1998. All rights reserved.
Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’?”
“Which employees have a salary greater
than Jones’ salary?”
Main Query
?
“What is Jones’ salary?”
?
Subquery
6-4 Copyright  Oracle Corporation, 1998. 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);
6-5 Copyright  Oracle Corporation, 1998. All rights reserved.
Using a Subquery
Display the names of those employees
who earn more than
employee no 7566.
6-6 Copyright  Oracle Corporation, 1998. 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-7 Copyright  Oracle Corporation, 1998. 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.
6-8 Copyright  Oracle Corporation, 1998. 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
6-9 Copyright  Oracle Corporation, 1998. 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
6-10 Copyright  Oracle Corporation, 1998. All rights reserved.
Executing Single-Row Subqueries
Display name and job of all those employees
who have same job as employee no 7369
and
who have salary greater than the salary of
employee no 7876.
6-11 Copyright  Oracle Corporation, 1998. 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);
6-12 Copyright  Oracle Corporation, 1998. All rights reserved.
Using Group Functions
in a Subquery
Display name, job and salary of those employees
who have
Salary equal to the minimum salary.
6-13 Copyright  Oracle Corporation, 1998. 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);
6-14 Copyright  Oracle Corporation, 1998. All rights reserved.
HAVING Clause with Subqueries
• The Oracle Server executes subqueries first.
• The Oracle Server returns results into the
HAVING clause of the main query.
800
Display department no, minimum salary
of each department
who have minimum salary
greater than the
minimum salary of department no 20.
6-15 Copyright  Oracle Corporation, 1998. All rights reserved.
HAVING Clause with Subqueries
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);
6-16 Copyright  Oracle Corporation, 1998. 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);
6-17 Copyright  Oracle Corporation, 1998. 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');
6-18 Copyright  Oracle Corporation, 1998. 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
6-19 Copyright  Oracle Corporation, 1998. All rights reserved.
Using the IN Operator
Use the IN operator to test for values in a
list.
SQL> SELECT empno, ename, sal, mgr
2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);
EMPNO ENAME SAL MGR
--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
7876 ADAMS 1100 7788
Subquery
6-20 Copyright  Oracle Corporation, 1998. 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';
EMP
DEPTNO JOB SAL
------ --------- -------
10 MANAGER 2450
10 PRESIDENT 5000
10 CLERK 1300
20 CLERK 800
20 CLERK 1100
20 ANALYST 3000
20 ANALYST 3000
20 MANAGER 2975
30 SALESMAN 1600
30 MANAGER 2850
30 SALESMAN 1250
30 CLERK 950
30 SALESMAN 1500
30 SALESMAN 1250
6-21 Copyright  Oracle Corporation, 1998. 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);
DEPTNO SAL
------ -------
10 2450
10 5000
10 1300
20 800
20 1100
20 3000
20 3000
20 2975
30 1600
30 2850
30 1250
30 950
30 1500
30 1250
DEPTNO AVG(SAL)
------- ---------
10 2916.6667
20 2175
30 1566.6667
6-22 Copyright  Oracle Corporation, 1998. All rights reserved.
Select all customers where rating is bigger than
the rating of all customers living in Berlin.
SELECT *
FROM XYZ
WHERE rating > ALL ( SELECT rating FROM XYZ
WHERE stadt='Berlin' );
6-23 Copyright  Oracle Corporation, 1998. All rights reserved.
Select all customers with rating bigger than
rating of at least one of living in Berlin.
SELECT *
FROM tkunden
WHERE rating > ANY ( SELECT rating FROM
tkunden WHERE stadt='Berlin' );

More Related Content

Similar to Les06- Subqueries.ppt

Les07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.pptLes07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.ppt
gznfrch1
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
Balqees Al.Mubarak
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
siavosh kaviani
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
HuzaifaMushtaq3
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
eemantariq2
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
NishaTariq1
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
siavosh kaviani
 
Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
Oracle examples
Oracle examplesOracle examples
Oracle examples
MaRwa Samih AL-Amri
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
Maria
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
siavosh kaviani
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
Salman Memon
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
Andrej Pashchenko
 
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
DrZeeshanBhatti
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
siavosh kaviani
 
Regular Expressions with full Unicode support
Regular Expressions with full Unicode supportRegular Expressions with full Unicode support
Regular Expressions with full Unicode support
MartinHanssonOracle
 

Similar to Les06- Subqueries.ppt (20)

Les07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.pptLes07- Multiple-Column Subqueries.ppt
Les07- Multiple-Column Subqueries.ppt
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Les06
Les06Les06
Les06
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
Les02.ppt
Les02.pptLes02.ppt
Les02.ppt
 
Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565
 
Oracle examples
Oracle examplesOracle examples
Oracle examples
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
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
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
 
Les06
Les06Les06
Les06
 
Regular Expressions with full Unicode support
Regular Expressions with full Unicode supportRegular Expressions with full Unicode support
Regular Expressions with full Unicode support
 

Recently uploaded

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

Les06- Subqueries.ppt

  • 1. Copyright  Oracle Corporation, 1998. All rights reserved. 6 Subqueries
  • 2. 6-2 Copyright  Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Describe the types of problems that subqueries can solve • Define subqueries • List the types of subqueries • Write single-row and multiple-row subqueries
  • 3. 6-3 Copyright  Oracle Corporation, 1998. All rights reserved. Using a Subquery to Solve a Problem “Who has a salary greater than Jones’?” “Which employees have a salary greater than Jones’ salary?” Main Query ? “What is Jones’ salary?” ? Subquery
  • 4. 6-4 Copyright  Oracle Corporation, 1998. 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. 6-5 Copyright  Oracle Corporation, 1998. All rights reserved. Using a Subquery Display the names of those employees who earn more than employee no 7566.
  • 6. 6-6 Copyright  Oracle Corporation, 1998. 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
  • 7. 6-7 Copyright  Oracle Corporation, 1998. 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.
  • 8. 6-8 Copyright  Oracle Corporation, 1998. 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
  • 9. 6-9 Copyright  Oracle Corporation, 1998. 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
  • 10. 6-10 Copyright  Oracle Corporation, 1998. All rights reserved. Executing Single-Row Subqueries Display name and job of all those employees who have same job as employee no 7369 and who have salary greater than the salary of employee no 7876.
  • 11. 6-11 Copyright  Oracle Corporation, 1998. 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);
  • 12. 6-12 Copyright  Oracle Corporation, 1998. All rights reserved. Using Group Functions in a Subquery Display name, job and salary of those employees who have Salary equal to the minimum salary.
  • 13. 6-13 Copyright  Oracle Corporation, 1998. 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);
  • 14. 6-14 Copyright  Oracle Corporation, 1998. All rights reserved. HAVING Clause with Subqueries • The Oracle Server executes subqueries first. • The Oracle Server returns results into the HAVING clause of the main query. 800 Display department no, minimum salary of each department who have minimum salary greater than the minimum salary of department no 20.
  • 15. 6-15 Copyright  Oracle Corporation, 1998. All rights reserved. HAVING Clause with Subqueries 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);
  • 16. 6-16 Copyright  Oracle Corporation, 1998. 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);
  • 17. 6-17 Copyright  Oracle Corporation, 1998. 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');
  • 18. 6-18 Copyright  Oracle Corporation, 1998. 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
  • 19. 6-19 Copyright  Oracle Corporation, 1998. All rights reserved. Using the IN Operator Use the IN operator to test for values in a list. SQL> SELECT empno, ename, sal, mgr 2 FROM emp 3 WHERE mgr IN (7902, 7566, 7788); EMPNO ENAME SAL MGR --------- ---------- --------- --------- 7902 FORD 3000 7566 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788 Subquery
  • 20. 6-20 Copyright  Oracle Corporation, 1998. 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'; EMP DEPTNO JOB SAL ------ --------- ------- 10 MANAGER 2450 10 PRESIDENT 5000 10 CLERK 1300 20 CLERK 800 20 CLERK 1100 20 ANALYST 3000 20 ANALYST 3000 20 MANAGER 2975 30 SALESMAN 1600 30 MANAGER 2850 30 SALESMAN 1250 30 CLERK 950 30 SALESMAN 1500 30 SALESMAN 1250
  • 21. 6-21 Copyright  Oracle Corporation, 1998. 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); DEPTNO SAL ------ ------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 DEPTNO AVG(SAL) ------- --------- 10 2916.6667 20 2175 30 1566.6667
  • 22. 6-22 Copyright  Oracle Corporation, 1998. All rights reserved. Select all customers where rating is bigger than the rating of all customers living in Berlin. SELECT * FROM XYZ WHERE rating > ALL ( SELECT rating FROM XYZ WHERE stadt='Berlin' );
  • 23. 6-23 Copyright  Oracle Corporation, 1998. All rights reserved. Select all customers with rating bigger than rating of at least one of living in Berlin. SELECT * FROM tkunden WHERE rating > ANY ( SELECT rating FROM tkunden WHERE stadt='Berlin' );