SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Subqueries
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using a Subquery  to Solve a Problem ,[object Object],Which employees have salaries greater than Abel’s salary? Main Query: ? What is Abel’s salary? ? Subquery
Subquery Syntax ,[object Object],[object Object],SELECT select_list FROM table WHERE expr operator   (SELECT select_list   FROM table );
Using a Subquery SELECT last_name FROM  employees WHERE  salary > (SELECT salary FROM  employees WHERE  last_name = 'Abel'); 11000
Guidelines for Using Subqueries ,[object Object],[object Object],[object Object],[object Object]
Types of Subqueries ST_CLERK ,[object Object],ST_CLERK SA_MAN ,[object Object],Main query Subquery returns Main query Subquery returns
Single-Row Subqueries ,[object Object],[object Object],Operator = > >= < <= <> Meaning Equal to Greater than  Greater than or equal to  Less than  Less than or equal to Not equal to
Executing Single-Row Subqueries 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); ST_CLERK 2600
Using Group Functions in a Subquery SELECT last_name, job_id, salary FROM  employees WHERE  salary =  (SELECT MIN(salary) FROM  employees); 2500
The  HAVING  Clause with Subqueries ,[object Object],[object Object],SELECT  department_id, MIN(salary) FROM  employees GROUP BY department_id HAVING  MIN(salary) > (SELECT MIN(salary) FROM  employees WHERE  department_id = 50); 2500
What is Wrong  with this Statement? SELECT employee_id, last_name FROM  employees WHERE  salary = (SELECT  MIN(salary) FROM  employees GROUP BY department_id); ERROR at line 4: ORA-01427: single-row subquery returns more than one row Single-row operator with multiple-row subquery
Will this Statement Return Rows? no rows selected SELECT last_name, job_id FROM  employees WHERE  job_id = (SELECT job_id FROM  employees WHERE  last_name = 'Haas'); Subquery returns no values
Multiple-Row Subqueries ,[object Object],[object Object],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
Using the  ANY  Operator  in Multiple-Row Subqueries 9000, 6000, 4200 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  ALL  Operator  in Multiple-Row Subqueries 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'; 9000, 6000, 4200
Null Values in a Subquery SELECT emp.last_name FROM  employees emp WHERE  emp.employee_id NOT IN (SELECT mgr.manager_id FROM  employees mgr); no rows selected
Summary ,[object Object],[object Object],[object Object],SELECT select_list FROM table WHERE expr operator   (SELECT  select_list   FROM   table );
Practice 6 Overview ,[object Object],[object Object],[object Object]
 
 
 

Weitere ähnliche Inhalte

Was ist angesagt?

Sql having clause
Sql having clauseSql having clause
Sql having clause
Vivek Singh
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practice
isummation
 

Was ist angesagt? (20)

Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Functions - AS3
Functions - AS3Functions - AS3
Functions - AS3
 
Sql having clause
Sql having clauseSql having clause
Sql having clause
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practice
 
Grant
GrantGrant
Grant
 
TDD, BDD, RSpec
TDD, BDD, RSpecTDD, BDD, RSpec
TDD, BDD, RSpec
 
Refactoring
RefactoringRefactoring
Refactoring
 
Rspec
RspecRspec
Rspec
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
Confident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF MeetupConfident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF Meetup
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
Behaviour-Driven Development
Behaviour-Driven DevelopmentBehaviour-Driven Development
Behaviour-Driven Development
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Episode 4 - Introduction to SOQL in Salesforce
Episode 4  - Introduction to SOQL in SalesforceEpisode 4  - Introduction to SOQL in Salesforce
Episode 4 - Introduction to SOQL in Salesforce
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
 
Rspec presentation
Rspec presentationRspec presentation
Rspec presentation
 

Andere mochten auch (9)

Les03
Les03Les03
Les03
 
Les04
Les04Les04
Les04
 
Les19
Les19Les19
Les19
 
Les20
Les20Les20
Les20
 
Les08
Les08Les08
Les08
 
Les05
Les05Les05
Les05
 
Les02
Les02Les02
Les02
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Oracle sql developer_slides
Oracle sql developer_slidesOracle sql developer_slides
Oracle sql developer_slides
 

Ähnlich wie Les06

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
Achmad Solichin
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
ecomputernotes
 

Ähnlich wie Les06 (20)

Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Les06
Les06Les06
Les06
 
Les18
Les18Les18
Les18
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Les02
Les02Les02
Les02
 
7. Using Sub Queries
7. Using Sub Queries7. Using Sub Queries
7. Using Sub Queries
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Les06
Les06Les06
Les06
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Les02
Les02Les02
Les02
 
Les04
Les04Les04
Les04
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
Les04
Les04Les04
Les04
 
Les06
Les06Les06
Les06
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 

Mehr von Vijay Kumar (10)

Les17
Les17Les17
Les17
 
Les15
Les15Les15
Les15
 
Les16
Les16Les16
Les16
 
Les14
Les14Les14
Les14
 
Les13
Les13Les13
Les13
 
Les12
Les12Les12
Les12
 
Les10
Les10Les10
Les10
 
Les11
Les11Les11
Les11
 
Les07
Les07Les07
Les07
 
Les09
Les09Les09
Les09
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Les06

  • 2.
  • 3.
  • 4.
  • 5. Using a Subquery SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel'); 11000
  • 6.
  • 7.
  • 8.
  • 9. Executing Single-Row Subqueries 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); ST_CLERK 2600
  • 10. Using Group Functions in a Subquery SELECT last_name, job_id, salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees); 2500
  • 11.
  • 12. What is Wrong with this Statement? SELECT employee_id, last_name FROM employees WHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id); ERROR at line 4: ORA-01427: single-row subquery returns more than one row Single-row operator with multiple-row subquery
  • 13. Will this Statement Return Rows? no rows selected SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); Subquery returns no values
  • 14.
  • 15. Using the ANY Operator in Multiple-Row Subqueries 9000, 6000, 4200 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'; …
  • 16. Using the ALL Operator in Multiple-Row Subqueries 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'; 9000, 6000, 4200
  • 17. Null Values in a Subquery SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr); no rows selected
  • 18.
  • 19.
  • 20.  
  • 21.  
  • 22.  

Hinweis der Redaktion

  1. Schedule: Timing Topic 25 minutes Lecture 30 minutes Practice 55 minutes Total