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

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

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

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







Oracle SQL for self join

Oracle Database Tips by Donald BurlesonSeptember 6, 2015

Question: I need to compare two sets of rows within the same table, and I want to write a self-join in Oracle SQL. What are the ways to join an Oracle table with itself using an alias?

Answer: Oracle has several methods for joining a table to itself, and the SQL for self joins can be done several ways. 

A self join is used to join a table to itself, and it is commonly used when you have a table with dates and you want to compare one date to another within the same table.

Visually, it sometimes helps to look at the table as two identical tables (A and B). I am joining A to B, it just happens to be the same table. Because the table is used twice in the query, you must alias the table, and of course, you cannot use a natural join. In the example below, I want a list of employees and their managers. In the EMP table, each employee is identified by an emp_key. The employee's manager is in the manager column and it contains the manager's emp_key.

SQL> select
2 b.emp_last_name EMPLOYEE,
3 a.emp_last_name MANAGER
4 from
5 emp a right outer join emp b on (a.emp_key = b.manager);

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

10 rows selected

As you can see, King is the manager for each of the employees, and King does not have a manager assigned. I used an outer join so that King would also be listed as an employee, even though he has no manager. Otherwise, King would not have been listed. I join the emp table to itself, joining where the emp_key matched the manager.

It's very common to do a self join when comparing two rows in the same table and when comparing two ranges of rows within the same table.  (For large complex comparisons of ranges of rows, some advocate self-joins using global temporary tables).
Here are the two most common ways to join a table with itself.
Traditional self-join:  (note the old and new aliases for the table names)
   to_char(snap_time,'yyyy-mm-dd HH24')  mydate,
   sum(new.pins-old.pins)                c1,
   sum(new.reloads-old.reloads)          c2,
   sum(new.pins-old.pins)                library_cache_miss_ratio
   stats$librarycache old,
   stats$librarycache new,
   stats$snapshot     sn
   new.snap_id = sn.snap_id
   old.snap_id = new.snap_id-1
   old.namespace = new.namespace
 group by
   to_char(snap_time,'yyyy-mm-dd HH24');
Self join with analytic function:
   sy.snap_id,sy.statistic# statistic#, statname,
sy.value - (LAG(sy.value)
ORDER BY sy.snap_id)) statdelta
FROM stats$sysstat sy
WHERE sy.snap_id IN (12208,12599,13480,13843)
('consistent gets','consistent changes',
'db block gets', 'db block changes')
ORDER BY, sy.snap_id;



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
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 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational