SQL SERVER – Introduction to JOINs – Basic of JOINs

The launch of Gandhinagar SQL Server User Group was a tremendous, astonishing success! It was overwhelming to see a large gathering of enthusiasts looking up to me (I was the Key Speaker) eager to enhance their knowledge and participate in some brainstorming discussions. Some members of User Group had requested me to write a simple article on JOINS elucidating its different types.

INNER JOIN

This join returns rows when there is at least one match in both the tables.
SQL SERVER - Introduction to JOINs - Basic of JOINs inner join

OUTER JOIN

There are three different Outer Join methods.

LEFT OUTER JOIN
This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
SQL SERVER - Introduction to JOINs - Basic of JOINs left join

RIGHT OUTER JOIN
This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
SQL SERVER - Introduction to JOINs - Basic of JOINs right join

FULL OUTER JOIN
This join combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match.
SQL SERVER - Introduction to JOINs - Basic of JOINs outer join

CROSS JOIN

This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are multiplication of record number from both the tables.

SQL SERVER - Introduction to JOINs - Basic of JOINs cross join - half

Additional Notes related to JOIN:

The following are three classic examples to display where Outer Join is useful. You will notice several instances where developers write query as given below.

SELECT t1.*
FROM Table1 t1
WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2)
GO

The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause in join.

/* LEFT JOIN - WHERE NULL */
SELECT t1.*,t2.*
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL

SQL SERVER - Introduction to JOINs - Basic of JOINs left join null

The above example can also be created using Right Outer Join.

SQL SERVER - Introduction to JOINs - Basic of JOINs right join null

NOT INNER JOIN
Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.

SQL SERVER - Introduction to JOINs - Basic of JOINs outer join null

You can download the complete SQL Script here, but for the sake of complicity I am including the same script here.

USE AdventureWorks
GO
CREATE TABLE table1
(ID INT, Value VARCHAR(10))
INSERT INTO Table1 (ID, Value)
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
UNION ALL
SELECT 3,'Third'
UNION ALL
SELECT 4,'Fourth'
UNION ALL
SELECT 5,'Fifth'
GO
CREATE TABLE table2
(ID INT, Value VARCHAR(10))
INSERT INTO Table2 (ID, Value)
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
UNION ALL
SELECT 3,'Third'
UNION ALL
SELECT 6,'Sixth'
UNION ALL
SELECT 7,'Seventh'
UNION ALL
SELECT 8,'Eighth'
GO
SELECT *
FROM Table1
SELECT *
FROM Table2
GO
USE AdventureWorks
GO
/* INNER JOIN */
SELECT t1.*,t2.*
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* LEFT JOIN */
SELECT t1.*,t2.*
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* RIGHT JOIN */
SELECT t1.*,t2.*
FROM Table1 t1
RIGHT JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* OUTER JOIN */
SELECT t1.*,t2.*
FROM Table1 t1
FULL OUTER JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* LEFT JOIN - WHERE NULL */
SELECT t1.*,t2.*
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
GO
/* RIGHT JOIN - WHERE NULL */
SELECT t1.*,t2.*
FROM Table1 t1
RIGHT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t1.ID IS NULL
GO
/* OUTER JOIN - WHERE NULL */
SELECT t1.*,t2.*
FROM Table1 t1
FULL OUTER JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t1.ID IS NULL OR t2.ID IS NULL
GO
/* CROSS JOIN */
SELECT t1.*,t2.*
FROM Table1 t1
CROSS JOIN Table2 t2
GO
DROP TABLE table1
DROP TABLE table2
GO

I hope this article fulfills its purpose. I would like to have feedback from my blog readers. Please suggest me where do you all want me to take this article next.

Reference : Pinal Dave (https://blog.sqlauthority.com)

Best Practices, Database, SQL Joins, SQL Scripts, SQL User Group
Previous Post
SQL SERVER – FIX : ERROR : The SQL Server System Configuration Checker cannot be executed due to WMI configuration on the machine Error:2147749896 (0×80041008)
Next Post
SQL SERVER – Check if Current Login is Part of Server Role Member

Related Posts

402 Comments. Leave new

  • Hi Pinal,
    There is an spelling mistake in Full Outer Join explanation, instead of “right outer join” you typed “right after join”. Even thought it is not a big mistake, i just want to notify you.

    Reply
  • Hi Pinal,
    In Full Outer Join – Where NULL picture having spelling mistake, instead of “Full Outer Join – Where NULL” there is only “Outer Join – Where NULL”, it is in in the final picture.

    Reply
  • Kumar Sivarajan
    October 5, 2011 11:37 am

    Excellent article. How can I download this article, for feature reference.

    Reply
  • Great explained, thanks

    Reply
  • But Is there any Technical difference between Left Outer Join And right Outer Join

    as i think if table order in the query is interchanged result is same?

    Reply
  • hi pinal,

    the best sql server website is sqlauthority.
    i want complete material of sqlserver plz send to my mail [emailremoved]
    i am waiting for ur reply.

    Reply
  • Nice

    Reply
  • Dinesh Kumar Tripathi
    October 21, 2011 3:52 pm

    nice article to understand joins ..
    thanks

    Reply
  • Excellent article.Really no more words to express..

    Reply
  • Great Article. Simple and best way to understand.

    Reply
  • Mr pinal dave
    this article is a new hope for all Fresher..
    and u r only one hope to provide best article
    with the help of figure,and simple code…..
    only one request if u will give new topic please first
    make simple table “like this code” because through
    “AdventureWorks” table its little tough to learn for fresher
    and then query……..

    please continue excellent Article..I have no word to express…
    But
    Thank’s lot……………………………………

    Reply
  • nice article :-)
    keep it up..

    Reply
  • g2-c9269a0f1640c4882d118820f8fe160d
    November 17, 2011 2:55 pm

    I have fear to use join earlier but now i can use it without any feal ;)

    Reply
  • This is good article

    Reply
  • Please I need help retrieving data from four tables.
    I am trying to retrieve data from multiple table but I could not get the right data.

    Below are the tables:

    STUDENT
    studentid
    firstname
    lastname
    DOB
    gender
    race
    startdate

    INDIVIDUALVALUE
    studentid
    schoolid
    ddvalueid

    DDVALUES
    ddvalueid
    ddvalue

    SCHOOLS
    schoolid
    schoolnumber

    I want to retrieve firstname, lastname, dob, gender, race, startdate, ddvalue, schoolnumber

    where ddvalue = 6

    Reply
  • Thank you for these article pinal.

    Reply
  • Mr. Pinal Dave,

    I have been benefiting from your blog from years and I just thought that how callous I am that never appreciated your efforts. Thus, I am writing these line to thank you for sharing all this knowledge with us.

    Thanks,
    Rao

    Reply
  • Hi Pinal,

    Your articles are always inspiring, thank you for helping those of us trying to learn.

    I have three tables, fieldnames & records simplified for brevity:

    [Athletes]

    ID Athlete
    1 Athlete1
    2 Athlete2
    3 Athlete3
    4 Athlete4

    [Events]

    ID Event
    1 Event1
    2 Event2
    3 Event3
    4 Event4

    [SummaryData]

    ID Event Athlete
    1 1 1
    2 1 2
    3 2 1
    4 2 3
    5 3 1
    6 3 3
    7 3 4

    I want to select one or more Events from [SummaryData] and only get Athletes that were in both Events,

    eg:

    Events 1 & 2 had only Athlete1
    Events 2 & 3 had Athlete1 + Athlete3

    Please help me in the right direction. What sort of join is simplest and most efficient in achieving this?

    Thank you in advance, Peter

    Reply
  • Thank U SQL Authority…!

    Reply
  • Very Good….

    Reply

Leave a Reply