In SQL, what’s the difference between an inner and outer join?


Joins are used to combine the data from two tables, with the result being a new, temporary table. The temporary table is created based on column(s) that the two tables share, which represent meaningful column(s) of comparison. The goal is to extract meaningful data from the resulting temporary table. Joins are performed based on something called a predicate, which specifies the condition to use in order to perform a join. A join can be either an inner join or an outer join, depending on how one wants the resulting table to look.

It is best to illustrate the differences between inner and outer joins by use of an example. Here we have 2 tables that we will use for our example:

Employee Location
EmpID EmpName
13 Jason
8 Alex
3 Ram
17 Babu
25 Johnson
EmpID EmpLoc
13 San Jose
8 Los Angeles
3 Pune, India
17 Chennai, India
39 Bangalore, India

It’s important to note that the very last row in the Employee table does not exist in the Employee Location table. Also, the very last row in the Employee Location table does not exist in the Employee table. These facts will prove to be significant in the discussion that follows.

Outer Joins

Let’s start the explanation with outer joins. Outer joins can be be further divided into left outer joins, right outer joins, and full outer joins. Here is what the SQL for a left outer join would look like, using the tables above:

select * from employee left outer join location 
on employee.empID = location.empID;

Subscribe to our newsletter for more free interview questions.

In this SQL we are joining on the condition that the employee ID’s match in the rows tables. So, we will be essentially combining 2 tables into 1, based on the condition that the employee ID’s match. Note that we can get rid of the "outer" in left outer join, which will give us the SQL below. This is equivalent to what we have above.

select * from employee left join location 
on employee.empID = location.empID;

A left outer join retains all of the rows of the left table, regardless of whether there is a row that matches on the right table. The SQL above will give us the result set shown below.

Employee.EmpID Employee.EmpName Location.EmpID Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India
25 Johnson NULL NULL

The Join Predicate – a geeky term you should know

Earlier we had mentioned something called a join predicate. In the SQL above, the join predicate is "on employee.empID = location.empID". This is the heart of any type of join, because it determines what common column between the 2 tables will be used to "join" the 2 tables. As you can see from the result set, all of the rows from the left table are returned when we do a left outer join. The last row of the Employee table (which contains the "Johson" entry) is displayed in the results even though there is no matching row in the Location table. As you can see, the non-matching columns in the last row are filled with a "NULL". So, we have "NULL" as the entry wherever there is no match.

A right outer join is pretty much the same thing as a left outer join, except that the rows that are retained are from the right table. This is what the SQL looks like:

select * from employee right outer join location 
on employee.empID = location.empID;

// taking out the "outer", this also works:

select * from employee right join location 
on employee.empID = location.empID;

Using the tables presented above, we can show what the result set of a right outer join would look like:

Employee.EmpID Employee.EmpName Location.EmpID Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India
NULL NULL 39 Bangalore, India

We can see that the last row returned in the result set contains the row that was in the Location table, but not in the Employee table (the "Bangalore, India" entry). Because there is no matching row in the Employee table that has an employee ID of "39", we have NULL’s in the result set for the Employee columns.

Inner Joins

Now that we’ve gone over outer joins, we can contrast those with the inner join. The difference between an inner join and an outer join is that an inner join will return only the rows that actually match based on the join predicate. Once again, this is best illustrated via an example. Here’s what the SQL for an inner join will look like:

select * from employee inner join location on 
employee.empID = location.empID

This can also be written as:

select * from employee, location
where employee.empID = location.empID

Now, here is what the result of running that SQL would look like:

Employee.EmpID Employee.EmpName Location.EmpID Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India

Inner vs Outer Joins

We can see that an inner join will only return rows in which there is a match based on the join predicate. In this case, what that means is anytime the Employee and Location table share an Employee ID, a row will be generated in the results to show the match. Looking at the original tables, one can see that those Employee ID’s that are shared by those tables are displayed in the results. But, with a left or right outer join, the result set will retain all of the rows from either the left or right table.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon

86 Responses to “Inner vs. Outer joins”

  1. Peter Moh says:

    It explains out jion and inner join very clearly

  2. Love To Learn says:

    Can anyone tell me which is the recommended way to write the SQL statements?

    select * from employee, location
    where employee.empID = location.empID


    select * from employee inner join location on
    employee.empID = location.empID

    I am now learning SQL and find that using the WHERE clause makes more sense to me but I am not sure if that is the recommended method.

  3. says:

    Nice stuff !!

  4. Bipin says:

    Very helpful

  5. G. says:

    Very helpful. Thanks alot

  6. Ketan Dharmik says:

    How join two table have different field in mysql????

  7. Suresh says:

    very helpful thaks

  8. mahesh says:

    very nice buddy.

  9. hh says:

    DUDU? xD

  10. Gavin Shao says:

    The explanation is really clear

  11. Bibhuti Anand says:

    Excellent Explanation of almost all the topic i have searched in this site…
    Great work. thanks.

  12. Kevin says:

    Best explanation ever….thanks so much

  13. Sam says:

    Worked in IT for over 12 years and I really never understood joins that well UNTIL Today. Thanks!

  14. Md Farooq says:

    wow awesome explanation

  15. Stella says:

    best explanation ever !!! new grad here preparing for interview here.

  16. kim says:

    how about joining table with one id only?

  17. speakhearseenoevil says:

    You might find this diagram useful. I have it saved on my desktop for quick reference.

  18. sumitperi says:

    amazing explanation.. my concepts are becoming a lot clear

  19. Guest says:

    Superb…. Thank you

  20. sunil says:

    Again a very good explanation

  21. Chetu says:

    Really good explanation….:)

  22. ramesh says:

    Super Buddy!!. Fantastic explanation. Kudos to the author

  23. Pawan Pareek says:

    Thanks for the post. I got the point very well and cleared my confusion between these.

  24. javed says:

    thankzzzz…Its Really Helpful…

  25. PI says:

    performance point of view, inner is better

  26. Priya Chetwani says:

    i’m new to the mysql joins. thank you so much for explanation

  27. sum15 says:

    Very well explained!! Thanks

  28. ram says:

    simple & best explanation

  29. Kannan Ramasamy says:

    very nice for ur explanation

  30. lakshmi says:

    i would like to know the exact differnce between inner join ,outerjoin and full outer join,can some guide me?

  31. Adi Narayana says:

    Excellent explanation.

  32. ankit says:

    very helpful

  33. NITHIY ANANTH says:


  34. sruthibenjamin says:

    very good explanation, thank you.

  35. HK says:

    what happens if I interchange tables during left outer and right outer joins? In that case we can just have one type of outer join.

  36. Md Tauqueer Alam says:

    Saved my lots of time… explained extremely well in an organized and understanding way. Thanx

  37. andy says:

    Thank you so very much. Yes i agree the best explanation i have read thus far

  38. cromateu says:

    thanku for this explanation :)

  39. Simon Mohoalali says:

    Awesome! simple, sweet and straight to the point!

  40. Nachi says:

    Really Good Explanation! Thanks a lot!

  41. Mayank Kumar says:

    Hi I want to know what is the difference between Inner join and Natural join?Please explain with examples.

  42. Abhaya says:

    Does it means like the UNION?
    which means it’ll return all values from both the table.

  43. Abhaya says:

    Yes, we don’t have this concept. we have left outer and right outer join.
    Inner join is only the simplest one and it returns the value based on the common field’s rows only.

  44. Rahul Sharma says:


  45. ihateyou says:


  46. shamim says:

    Nice Examples

  47. Chandra Prakash says:

    Best Ever explanation for a LAYMAN

  48. So far so good. I hope you will also add the explanation of a full outer join.

  49. gcoxusa says:

    The explanation is excellent, but if I get this question (what
    is the difference between an inner and outer join?) in a phone interview they would not appreciate me launching into this long-winded explanation.

  50. sanjay says:

    it means we don’t have the concept of inner left and right join ???

  51. Labeeba says:

    Simple explanation.Thank you

  52. Sanjay says:

    very nice !!!!

  53. Ranjith Babu says:

    Thnk you so much…

  54. Meera says:


  55. Sgt13Echo says:

    Whats better to use the “inner join” or commas and a where statement

  56. Boo says:

    ya… nice tutorial …… i like it

  57. Muhammad Usman says:

    Good explanation and awesome work….

  58. Keshav says:

    Great !!

  59. Reddy says:

    explanation made very simple manner. Liked this.

  60. Edwin Bajwahuka says:

    yes, it is amazing

  61. Zelinskey says:

    Really amazing explanation. Better than all other sites (almost > 20) which I referred. Read all the comments below and found none that doesn't appreciate you.

  62. Nishant says:

    Cleared concepts in lucid way

  63. Prashant says:

    Really very good and useful explanation….Thanks

  64. Bharat Reddy says:

    really am struggling with joins Concept..
    now, i will roar cause i clearly understood .

  65. Ravivarman says:

    Very nice explanation.!! Thanks very much..!!

  66. neethu says:

    awsome explanation

  67. anas says:

    full outer join gives both common and uncommon result from the tables…

  68. Anwesh says:

    I have gone through many refernce material to understand Outer join, but i never understood…This is by far the best and the simplest explanation possible….Thanks a ton DUDU

  69. Vaibhav Tikoo says:

    The way the explanation is given is just awesome…Thanks for sharing

  70. GLKC says:

    Finally, clear and simple conceptual explanations that are easy to understand! Thank You!

  71. Richa says:

    Thorough Explanation :) Thank u so much for other concepts as well!

  72. Fida Husain says:

    really very good Explanation about join

  73. Aamir Sohail says:

    you are awesome bddy.. post more about hints, execution plan table's data(most confusing) also write some topics for performance tuning…. plzz

  74. Gixx says:

    On the DOT… Dude

  75. Hegel says:

    This is good. Thank you.

  76. vicky says:

    what about full outer join ???

  77. Anirudh says:


  78. Vuyani Billy Nyathikazi says:


  79. apurva says:

    osum work :)

  80. anonymus says:

    really ur site s so awesome…as u give a simple and neat explanation..i wish u post more in networking and operating systems..

  81. rd says:

    simple and precise explanation

  82. Akash says:

    I love this explanation,,It seems the author got the crux of sql..

  83. Shivam Bansal says:

    This site is awesome…

  84. dixitsingla says:

    Best explanation

Leave a Reply