SlideShare une entreprise Scribd logo
1  sur  11
Oracle Sub Queries
Type of Sub - Queries
• There are two type of sub – queries
• Scalar subqueries(Single Row Subqueries) return a single value, or exactly
one row and exactly one column.
• Multi row subqueries return either:
• One column with multiple rows (i.e. a list of values), or
• Multiple columns with multiple rows (i.e. tables).
• Correlated subqueries, where the inner query relies on information obtained
from the outer query.
What is sub-queries
• subquery is a query within a query.
• You can create subqueries within your SQL statements.
• These subqueries can reside in the WHERE clause, the FROM clause,
or the SELECT clause.
Advantages of Oracle subqueries
• Provide an alternative way to query data that would require
complex joins and unions.
• Make the complex queries more readable.
• Allow a complex query to be structured in a way that it is possible to
isolate each part.
What is co-related sub-queries
• A correlated subquery is a subquery that uses values from the outer
query.
• A correlated subquery may be evaluated once for each row selected
by the outer query.
Difference between sub-queries and
correlated sub-queries
Sub-Queries
1. The inner query is executed only once.
2. The inner query will get executed first and
based on inner query results out query gets
executed.
3. Inner query is not dependent on outer query
Correlated Sub-Queries
1. Inner query is executed multiple time based on
no of rows of outer query.
2. The outer query will get executed first and for
every row of outer query, inner query will get
executed.
3. Inner query is dependent on outer query.
Example
• Sub-Queries Where Clouse
SELECT first_name, last_name
FROM collectors
WHERE id IN (
SELECT collector_id
FROM sales);
Example
• Correlated Sub-Queries Where Clouse
Select *
From Employee
Where Sal > (SELECT AVG(SAL)
FROM EMPLOYEE);
Example
• Correlated Sub-Queries From Clouse
Select EMPNO, ENAME, SAL, DEPTNO, (SELECT DNAME FROM
DEPARTMENT WHERE DEPTNO = A.DEPTNO)
From Employee A;
Example
• Correlated Sub-Queries in case of update
UPDATE DEPARTMENT A
SET TOTAL_SAL = (SELECT SUM(SAL)
FROM EMPLOYEE
WHERE DEPTNO = A.DEPTNO)
Example
• Use of Correlated Sub-Queries in case of deleting duplicate rows
DELETE FROM EMPLOYEE A
WHERE ROWNUM <> (SELECT MIN(ROWNUM)
FROM EMPLOYEE
WHERE EMPNO = A.EMPNO)

Contenu connexe

Similaire à Oracle Sub Queries for interview preparation

Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptxLECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
AthosBeatus
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
Lucidworks
 
Apache Solr Workshop
Apache Solr WorkshopApache Solr Workshop
Apache Solr Workshop
JSGB
 

Similaire à Oracle Sub Queries for interview preparation (20)

Query processing
Query processingQuery processing
Query processing
 
Web hacking series part 3
Web hacking series part 3Web hacking series part 3
Web hacking series part 3
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptxLECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
IR SQLite Session #1
IR SQLite Session #1IR SQLite Session #1
IR SQLite Session #1
 
Sub-Queries
Sub-QueriesSub-Queries
Sub-Queries
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-having
 
Sql parametrized queries
Sql parametrized queriesSql parametrized queries
Sql parametrized queries
 
Mentor Your Indexes
Mentor Your IndexesMentor Your Indexes
Mentor Your Indexes
 
SQL injection basics
SQL injection basicsSQL injection basics
SQL injection basics
 
Parallel SQL and Streaming Expressions in Apache Solr 6
Parallel SQL and Streaming Expressions in Apache Solr 6Parallel SQL and Streaming Expressions in Apache Solr 6
Parallel SQL and Streaming Expressions in Apache Solr 6
 
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
Automatically Build Solr Synonyms List using Machine Learning - Chao Han, Luc...
 
SQL_Part1
SQL_Part1SQL_Part1
SQL_Part1
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Apache Solr Workshop
Apache Solr WorkshopApache Solr Workshop
Apache Solr Workshop
 

Dernier

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 

Dernier (20)

Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 

Oracle Sub Queries for interview preparation

  • 2. Type of Sub - Queries • There are two type of sub – queries • Scalar subqueries(Single Row Subqueries) return a single value, or exactly one row and exactly one column. • Multi row subqueries return either: • One column with multiple rows (i.e. a list of values), or • Multiple columns with multiple rows (i.e. tables). • Correlated subqueries, where the inner query relies on information obtained from the outer query.
  • 3. What is sub-queries • subquery is a query within a query. • You can create subqueries within your SQL statements. • These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.
  • 4. Advantages of Oracle subqueries • Provide an alternative way to query data that would require complex joins and unions. • Make the complex queries more readable. • Allow a complex query to be structured in a way that it is possible to isolate each part.
  • 5. What is co-related sub-queries • A correlated subquery is a subquery that uses values from the outer query. • A correlated subquery may be evaluated once for each row selected by the outer query.
  • 6. Difference between sub-queries and correlated sub-queries Sub-Queries 1. The inner query is executed only once. 2. The inner query will get executed first and based on inner query results out query gets executed. 3. Inner query is not dependent on outer query Correlated Sub-Queries 1. Inner query is executed multiple time based on no of rows of outer query. 2. The outer query will get executed first and for every row of outer query, inner query will get executed. 3. Inner query is dependent on outer query.
  • 7. Example • Sub-Queries Where Clouse SELECT first_name, last_name FROM collectors WHERE id IN ( SELECT collector_id FROM sales);
  • 8. Example • Correlated Sub-Queries Where Clouse Select * From Employee Where Sal > (SELECT AVG(SAL) FROM EMPLOYEE);
  • 9. Example • Correlated Sub-Queries From Clouse Select EMPNO, ENAME, SAL, DEPTNO, (SELECT DNAME FROM DEPARTMENT WHERE DEPTNO = A.DEPTNO) From Employee A;
  • 10. Example • Correlated Sub-Queries in case of update UPDATE DEPARTMENT A SET TOTAL_SAL = (SELECT SUM(SAL) FROM EMPLOYEE WHERE DEPTNO = A.DEPTNO)
  • 11. Example • Use of Correlated Sub-Queries in case of deleting duplicate rows DELETE FROM EMPLOYEE A WHERE ROWNUM <> (SELECT MIN(ROWNUM) FROM EMPLOYEE WHERE EMPNO = A.EMPNO)