Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Oracle SQL subquery types in, not in, exists


Oracle Tips by Burleson Consulting

 

SQL: Where column > (subquery)

The following subquery selects those employees whose salary is greater than the minimum salary for their jobs.

select
   emp_last_name
from
   emp
where 
   emp_salary > (select job_min_sal from job
                     where
                     emp.job_key = job.job_key)
;


EMP_LAST_NAME                                                                   
-------                                                  
king                                                                            
jackson                                                                         
korn                                                                            
linus                                                                           
tokheim                                                                         
johnson                                                                         
baker                                                                           
coleman                                                                         
brannigan                                                                       

9 rows selected.

This is known as a correlated subquery because the subquery references the outer query in the subqueries WHERE clause. Internally, correlated subqueries are very expensive to process because the inner query must be executed for every row returned by the outer query.

select
   book_title
from
   book
where 
   pub_key = (select pub_key from publisher
                     where
                     publisher.pub_key = book.pub_key)
;



BOOK_TITLE                                                                      
-----
windows sucks                                                                   
piano greats                                                                    
DOS for dummies                                                                 
The zen of auto repair                                                          
zero loss finance                                                               
operations research theory                                                      
non violins in the workplace                                                    
UNIX for experts                                                                
pay no taxes and go to jail                                                     
the fall of microsoft                                                           
writers market                                                                  
managing stress                                                                 
bears are people too                                                            
reduce spending the republican way                                              
the willow weeps no more                                                        
oracle9i sql tuning                                                             
was george washington gay?                                                      
cooking light                                                                   
never eat boogers                                                               
how to housebreak your horse                                                    

20 rows selected.

Where column < (subquery)

In this example, we display the last names of all employees whose salary is less than the minimum salary for their job grade.  Note that this is a correlated subquery because the inner query references the outer query.

select
   emp_last_name
from
   emp
where 
   emp_salary < (select job_min_sal from job
                     where
                     emp.job_key = job.job_key)
;



EMP_LAST_NAME                                                                   
--------------------                                        
lavender

Where exists (subquery)

The WHERE EXISTS subquery is used when we want to display all rows where we have a matching column in both tables.  In most cases, this type of subquery can be re-written with a standard join to improve performance.

select
   book_key
from
   book
where 
   exists (select book_key from sales)
;



BOOK_K                                                                          
----                                                                  
B101                                                                            
B102                                                                            
B103                                                                            
B104                                                                            
B105                                                                            
B106                                                                            
B107                                                                            
B108                                                                            
B109                                                                            
B110                                                                            
B111                                                                            
B112                                                                            
B113                                                                            
B114                                                                            
B115                                                                            
B116                                                                            
B117                                                                            
B118                                                                            
B119                                                                            
B120  

Where not exists (subquery)

The WHERE NOT EXISTS subquery is used to display cases where a selected column does not appear in another table.

select
   author_key
from
   author
where 
   not exists (select author_key from book_author)
;

As a general rule, the use of the NOT EXISTS subqueries are discouraged because the query can often be re-written as a standard join with much faster performance.

Where column in (subquery)

select
   book_title
from
   book
where 
   pub_key in (select pub_key from publisher
                     where
                     publisher.pub_key = book.pub_key)
;


BOOK_TITLE                                                                      
-
windows sucks                                                                   
piano greats                                                                    
DOS for dummies                                                                 
The zen of auto repair                                                          
zero loss finance                                                               
operations research theory                                                      
non violins in the workplace                                                    
UNIX for experts                                                                
pay no taxes and go to jail                                                     
the fall of microsoft                                                           
writers market                                                                  
managing stress                                                                 
bears are people too                                                            
reduce spending the republican way                                              
the willow weeps no more                                                        
oracle9i sql tuning                                                             
was george washington gay?                                                      
cooking light                                                                   
never eat boogers                                                               
how to housebreak your horse                                                    

20 rows selected.

Where column not in (subquery)

In this example, we select all books that do not have sales.  Note that this is a non-correlated subquery.

select
   book_key
from
   book
where 
   book_key not in (select book_key from sales)
;


BOOK_K                                                                          
----                                                                       
B117                                                                            
B118                                                                            
B119                                                                            
B120  

There are serious problems with subqueries that may return NULL values. It is a good idea to discourage the use of the NOT IN clause (which invokes a subquery) and to prefer NOT EXISTS (which invokes a correlated subquery), since the query returns no rows if any rows returned by the subquery contain null values.

Note: Prior to Oracle10g there was a bug that caused a huge execution difference between EXISTS and IN.  Starting in 10g release 2 and beyond, Oracle will automatically rewrite an IN subquery to use the EXISTS clause.

 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

��  
 
 

 
 
 
 
oracle dba poster
 

 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright 1996 -  2014

All rights reserved by Burleson

Oracle is the registered trademark of Oracle Corporation.