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

  • Very useful for the beginners like me..

    Reply
  • u r explanation excellent

    Reply
  • thanks a milion, this article will not be forgotten, NEVER, very nice……..

    Reply
  • Hi,

    I have a small problem of the following Tables, I have MyTable1 (for example) and I want to attach content of following MyTable2 to MyTable1, but MyTable2 have the Data just 1 week ago, and it should be in appropriate columns against that columns in MyTable1, pl tel me what can I do?

    MyTable 1 :

    Id_Date Id_Branch NewBalance
    07/08/2012 1270 160000
    07/08/2012 1654 170000
    07/08/2012 3046 160000
    11/08/2012 2046 140000
    11/08/2012 4395 170000
    11/08/2012 1720 160000
    18/08/2012 3046 110000
    18/08/2012 3046 160000

    MyTable 2 :

    Id_Date OldBalance
    11/08/2012 170000
    11/08/2012 150000
    11/08/2012 180000
    18/08/2012 130000
    18/08/2012 100000

    It should be some thing like this :

    Id_Date Id_Branch NewBalance OldBalance
    07/08/2012 1270 160000 –
    07/08/2012 1654 170000 –
    07/08/2012 3046 160000 –
    11/08/2012 2046 140000 170000
    11/08/2012 4395 170000 150000
    11/08/2012 1720 160000 180000
    18/08/2012 3046 110000 130000
    18/08/2012 3046 160000 100000

    Thanks a lot…..

    Reply
  • Thanku Sir….what you are given data is very needfull for learners

    Reply
  • I am fortunate enough to have found your blog. This is by far the absolute best tutorial/ explanation of sql joins I have ever found in 14 years in the IT industry. You visual examples clarify joins like no other. For a visual learner this presentation completes my understanding of joins after all of these years. If I woud have seen examples like this years ago…

    Thank you sir!

    Reply
  • nimisha savalia
    December 12, 2012 9:18 pm

    superb N awesome site n explanation..!!
    Thanks…!!

    Reply
  • Very nice and good illustarte. Keep it up…

    Reply
  • G.NAGA HARISH CHOUDARY
    December 19, 2012 5:28 pm

    really superbbbb !!!!!!!! SITEEEEEE

    Reply
  • Very useful artical :)

    Reply
  • Please explain Inner join of three or more table same way….

    Reply
  • can you please put some lights on some three table join.

    Reply
  • soundarya
    nice creation
    thank u

    Reply
  • itsmekasturi@gmail.com
    January 3, 2013 11:54 am

    i want to join two queries like select * from table1 where custid in(with DirectReports(……)).But its not giving me the permission to use DirectReports.I will be very much thankful if you show me the way to do it

    Reply
  • Shreyas Dharmadhikari
    January 10, 2013 5:37 pm

    Please explain Inner join of three or more table same way

    Reply
  • is it possible to switch between two table in SQL Server
    Means in one condition we can use one table and in another condition we can use another table
    like
    if @Q=1
    select * from table1 t1 inner join table2 t2 on t1.id=t2.id

    else
    select * from table1
    end

    without using if else

    Reply
  • sir, can you please explain only the keyword “JOIN” and how can we use it in a syntax.

    Reply
  • thanx

    Reply
  • Hi,
    thanks, this is the best description of joins concept in the web.

    Reply
  • Thanks a lot, when i have any doubt about SQL joins, i open this site.. Really
    helpful.

    Reply

Leave a Reply