SlideShare a Scribd company logo
Subqueries  http://ecomputernotes.com
Objectives  After completing this lesson, you should be able to  do the following:  "  Describe the types of problem that subqueries can  solve  "  Define subqueries  "  List the types of subqueries  "  Write single - row and multiple - row subqueries  http://ecomputernotes.com
Using a Subquery  to Solve a Problem  Who has a salary greater than Abel¶s?  Main Query:  Which employees have salaries greater  ?  than Abel¶s salary?  Subquery  ?  What is Abel¶s salary?  http://ecomputernotes.com
Subquery Syntax  SELECT  select_list  FRO M  table  WHERE  expr operator  (SELECT  select_list  FRO M  table );  "T he subquery (inner query) executes once before  the main query.  "T he result of the subquery is used by the main  query (outer query).  http://ecomputernotes.com
Using a Subquery  SELECT last_name  11000  FROM  employees  WHERE  salary >  (SELECT salary  FROM  employees  WHERE  last_name = 'Abel');  http://ecomputernotes.com
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.  "U se single-row operators with single-row  subqueries and use multiple-row operators with multiple-row subqueries.  http://ecomputernotes.com
Types of Subqueries  "  Single-row subquery  Main query  returns  Subquery  ST_CLERK  "  Multiple-row subquery  Main query  returns  ST_CLERK  Subquery  SA_MAN  http://ecomputernotes.com
Single-Row Subqueries  &quot;  Return only one row  &quot;  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  http://ecomputernotes.com
Executing Single-Row Subqueries  SELECT last_name, job_id, salary  FROM  employees  WHERE  job_id =  ST_CLERK  (SELECT job_id  FROM  employees  WHERE  employee_id = 141)  AND  salary >  2600  (SELECT salary  FROM  employees  WHERE  employee_id = 143);  http://ecomputernotes.com
Using Group Functions in a Subquery  SELECT last_name, job_id, salary  FROM  employees  2500  WHERE  salary =  (SELECT MIN(salary)  FROM  employees);  http://ecomputernotes.com
The   HAVING   Clause with Subqueries  &quot;  The Oracle server executes subqueries first.  &quot;  The Oracle server returns results into the   HAVING  clause of the main query.  SELECT  department_id, MIN(salary)  FROM  employees  GROUP BY department_id  2500  HAVING  MIN(salary) >  (SELECT MIN(salary)  FRO M  employees  WHERE  department_id = 50);  http://ecomputernotes.com
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 rowone row  http://ecomputernotes.com
Will this Statement Return Rows?  SELECT last_name, job_id  FROM  employees  WHERE  job_id =  (SELECT job_id  FROM  employees  WHERE  last_name = 'Haas');  no rows selected  http://ecomputernotes.com
Multiple-Row Subqueries  &quot;  Return more than one row  &quot;  Use multiple-row comparison operators  Operator  Meaning  IN  Equal to any member in the list  ANY  Compare value to each value returned by  the subquery  Compare value to every value returned  ALL  by the subquery  http://ecomputernotes.com
Using the   ANY   Operator in Multiple-Row Subqueries  SELECT employee_id, last_name, job_id, salary  FROM  employees  9000, 6000, 4200  WHERE  salary < ANY  (SELECT salary  FROM  employees  WHERE  job_id = 'IT_PROG') AND  job_id <> 'IT_PROG';  «  http://ecomputernotes.com
Using the   ALL   Operator in Multiple-Row Subqueries  SELECT employee_id, last_name, job_id, salary  FROM  employees  9000, 6000, 4200  WHERE  salary < ALL  (SELECT salary  FROM  employees  WHERE  job_id = 'IT_PROG') AND  job_id <> 'IT_PROG';  http://ecomputernotes.com
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  http://ecomputernotes.com

More Related Content

What's hot (20)

PPT
Les06 (using subqueries to solve queries)
Achmad Solichin
 
PPT
Les06
Vijay Kumar
 
PPTX
Lab5 sub query
Balqees Al.Mubarak
 
PPTX
Sub query example with advantage and disadvantages
Sarfaraz Ghanta
 
PPT
Subconsultas
Maria
 
PDF
Sql query tuning or query optimization
Vivek Singh
 
PPT
Aggregating Data Using Group Functions
Salman Memon
 
PPT
SQL WORKSHOP::Lecture 6
Umair Amjad
 
PPT
Manipulating Data Oracle Data base
Salman Memon
 
PPT
Displaying Data from Multiple Tables - Oracle Data Base
Salman Memon
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Difference between group by and order by in sql server
Umar Ali
 
PPT
Producing Readable Output with iSQL*Plus - Oracle Data Base
Salman Memon
 
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Lab5 sub query
Balqees Al.Mubarak
 
Sub query example with advantage and disadvantages
Sarfaraz Ghanta
 
Subconsultas
Maria
 
Sql query tuning or query optimization
Vivek Singh
 
Aggregating Data Using Group Functions
Salman Memon
 
SQL WORKSHOP::Lecture 6
Umair Amjad
 
Manipulating Data Oracle Data base
Salman Memon
 
Displaying Data from Multiple Tables - Oracle Data Base
Salman Memon
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Difference between group by and order by in sql server
Umar Ali
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Salman Memon
 

Viewers also liked (20)

PDF
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
PDF
Sql db optimization
Nikhildas P C
 
PPTX
MS SQL SERVER: Creating Views
sqlserver content
 
PPTX
Consultas en MS SQL Server 2012
CarlosFloresRoman
 
PPS
Sql xp 04
Niit Care
 
PPT
Sql tuning guideline
Sidney Chen
 
PPTX
Statistics
Riteshkiit
 
PPTX
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
PPSX
Locking in SQL Server
Prashant Gogoi
 
PPT
Review of SQL
Information Technology
 
RTF
Triggers-Sequences-SQL
Patrick Seery
 
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
PPT
Lecture 4. MS SQL. DML Triggers
Alexey Furmanov
 
PPT
Locking And Concurrency
sqlserver.co.il
 
PPTX
Sql query analyzer & maintenance
nspyrenet
 
PDF
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
PPTX
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Framgia Vietnam
 
PPTX
Database Transactions and SQL Server Concurrency
Boris Hristov
 
PPTX
Data Compression In SQL
Boosh Booshan
 
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
Sql db optimization
Nikhildas P C
 
MS SQL SERVER: Creating Views
sqlserver content
 
Consultas en MS SQL Server 2012
CarlosFloresRoman
 
Sql xp 04
Niit Care
 
Sql tuning guideline
Sidney Chen
 
Statistics
Riteshkiit
 
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
Locking in SQL Server
Prashant Gogoi
 
Review of SQL
Information Technology
 
Triggers-Sequences-SQL
Patrick Seery
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
Lecture 4. MS SQL. DML Triggers
Alexey Furmanov
 
Locking And Concurrency
sqlserver.co.il
 
Sql query analyzer & maintenance
nspyrenet
 
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Framgia Vietnam
 
Database Transactions and SQL Server Concurrency
Boris Hristov
 
Data Compression In SQL
Boosh Booshan
 
Ad

Similar to e computer notes - Subqueries (20)

PPT
e computer notes - Using set operator
ecomputernotes
 
PPT
e computer notes - Restricting and sorting data
ecomputernotes
 
PPT
e computer notes - Advanced subqueries
ecomputernotes
 
PPT
e computer notes - Aggregating data using group functions
ecomputernotes
 
PPT
e computer notes - From multiple tables
ecomputernotes
 
PPT
e computer notes - Creating views
ecomputernotes
 
PPT
e computer notes - Producing readable output with i sql plus
ecomputernotes
 
PPT
Les18
Vijay Kumar
 
PPT
e computer notes - Enhancements to the group by clause
ecomputernotes
 
PPT
Using subqueries to solve queries
Syed Zaid Irshad
 
PPT
e computer notes - Writing basic sql select statements
ecomputernotes
 
PPT
Les02 (restricting and sorting data)
Achmad Solichin
 
PPT
Les02
Sudharsan S
 
PPT
SUBQUERY materi kegiatan pembelajaran 1.1
mochammadagri
 
PPT
Les06
Sudharsan S
 
PPT
Chinabankppt
newrforce
 
PPT
Aggregate Functions,Final
mukesh24pandey
 
PPTX
Ch 6 Sub Query.pptx
RamishaRauf
 
PPT
Les15
Vijay Kumar
 
e computer notes - Using set operator
ecomputernotes
 
e computer notes - Restricting and sorting data
ecomputernotes
 
e computer notes - Advanced subqueries
ecomputernotes
 
e computer notes - Aggregating data using group functions
ecomputernotes
 
e computer notes - From multiple tables
ecomputernotes
 
e computer notes - Creating views
ecomputernotes
 
e computer notes - Producing readable output with i sql plus
ecomputernotes
 
e computer notes - Enhancements to the group by clause
ecomputernotes
 
Using subqueries to solve queries
Syed Zaid Irshad
 
e computer notes - Writing basic sql select statements
ecomputernotes
 
Les02 (restricting and sorting data)
Achmad Solichin
 
SUBQUERY materi kegiatan pembelajaran 1.1
mochammadagri
 
Chinabankppt
newrforce
 
Aggregate Functions,Final
mukesh24pandey
 
Ch 6 Sub Query.pptx
RamishaRauf
 
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
ecomputernotes
 
PPT
computer notes - Data Structures - 39
ecomputernotes
 
PPT
computer notes - Data Structures - 11
ecomputernotes
 
PPT
computer notes - Data Structures - 20
ecomputernotes
 
PPT
computer notes - Data Structures - 15
ecomputernotes
 
DOC
Computer notes - Including Constraints
ecomputernotes
 
DOC
Computer notes - Date time Functions
ecomputernotes
 
DOC
Computer notes - Subqueries
ecomputernotes
 
DOC
Computer notes - Other Database Objects
ecomputernotes
 
PPT
computer notes - Data Structures - 28
ecomputernotes
 
PPT
computer notes - Data Structures - 19
ecomputernotes
 
PPT
computer notes - Data Structures - 31
ecomputernotes
 
PPT
computer notes - Data Structures - 4
ecomputernotes
 
PPT
computer notes - Data Structures - 13
ecomputernotes
 
DOC
Computer notes - Advanced Subqueries
ecomputernotes
 
DOC
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
PPT
computer notes - Data Structures - 16
ecomputernotes
 
PPT
computer notes - Data Structures - 22
ecomputernotes
 
PPT
computer notes - Data Structures - 35
ecomputernotes
 
PPT
computer notes - Data Structures - 36
ecomputernotes
 
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
ecomputernotes
 

Recently uploaded (20)

PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 

e computer notes - Subqueries

  • 2. Objectives After completing this lesson, you should be able to do the following: &quot; Describe the types of problem that subqueries can solve &quot; Define subqueries &quot; List the types of subqueries &quot; Write single - row and multiple - row subqueries http://ecomputernotes.com
  • 3. Using a Subquery to Solve a Problem Who has a salary greater than Abel¶s? Main Query: Which employees have salaries greater ? than Abel¶s salary? Subquery ? What is Abel¶s salary? http://ecomputernotes.com
  • 4. Subquery Syntax SELECT select_list FRO M table WHERE expr operator (SELECT select_list FRO M table ); &quot;T he subquery (inner query) executes once before the main query. &quot;T he result of the subquery is used by the main query (outer query). http://ecomputernotes.com
  • 5. Using a Subquery SELECT last_name 11000 FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel'); http://ecomputernotes.com
  • 6. Guidelines for Using Subqueries &quot; Enclose subqueries in parentheses. &quot; Place subqueries on the right side of the comparison condition. &quot; The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis. &quot;U se single-row operators with single-row subqueries and use multiple-row operators with multiple-row subqueries. http://ecomputernotes.com
  • 7. Types of Subqueries &quot; Single-row subquery Main query returns Subquery ST_CLERK &quot; Multiple-row subquery Main query returns ST_CLERK Subquery SA_MAN http://ecomputernotes.com
  • 8. Single-Row Subqueries &quot; Return only one row &quot; 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 http://ecomputernotes.com
  • 9. Executing Single-Row Subqueries SELECT last_name, job_id, salary FROM employees WHERE job_id = ST_CLERK (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > 2600 (SELECT salary FROM employees WHERE employee_id = 143); http://ecomputernotes.com
  • 10. Using Group Functions in a Subquery SELECT last_name, job_id, salary FROM employees 2500 WHERE salary = (SELECT MIN(salary) FROM employees); http://ecomputernotes.com
  • 11. The HAVING Clause with Subqueries &quot; The Oracle server executes subqueries first. &quot; The Oracle server returns results into the HAVING clause of the main query. SELECT department_id, MIN(salary) FROM employees GROUP BY department_id 2500 HAVING MIN(salary) > (SELECT MIN(salary) FRO M employees WHERE department_id = 50); http://ecomputernotes.com
  • 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 rowone row http://ecomputernotes.com
  • 13. Will this Statement Return Rows? SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); no rows selected http://ecomputernotes.com
  • 14. Multiple-Row Subqueries &quot; Return more than one row &quot; Use multiple-row comparison operators Operator Meaning IN Equal to any member in the list ANY Compare value to each value returned by the subquery Compare value to every value returned ALL by the subquery http://ecomputernotes.com
  • 15. Using the ANY Operator in Multiple-Row Subqueries SELECT employee_id, last_name, job_id, salary FROM employees 9000, 6000, 4200 WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; « http://ecomputernotes.com
  • 16. Using the ALL Operator in Multiple-Row Subqueries SELECT employee_id, last_name, job_id, salary FROM employees 9000, 6000, 4200 WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; http://ecomputernotes.com
  • 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 http://ecomputernotes.com