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

  • Sajin K Sivadas
    June 2, 2013 11:22 am

    Excellent work….Thanks for sharing your knowledge….it helps a lot…..

    Reply
  • Sajin K Sivadas
    June 2, 2013 11:24 am

    Excellent work….Thanks a lot for a simple explanation …

    Reply
  • Celso Palmeira
    June 5, 2013 11:58 pm

    Can I translate this post to Portuguese and post on blog (surely giving all credits to you blog)?
    My blog is blog.celsoneto.com.br

    Reply
  • VenkataRamana
    June 13, 2013 4:52 am

    Hi what is the difference b/n creating table
    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’
    and
    CREATE TABLE table1
    (ID INT, Value VARCHAR(10))
    INSERT INTO Table1 values (1,’first’),(2,’second’),(3,’third’),(4,’Fourth’),(5,’fifth’).
    Is both are same? Which one you used mostly in real time. I am a learner pls let me know

    Reply
  • VenkataRamana
    June 13, 2013 4:55 am

    Hi Pls post any other examples from your real time exp on joins.
    Thanks
    Chinna.

    Reply
  • Pinal Sir,

    Excellent article. Very very easy to understand the concepts of joins.
    Appreciated.

    Reply
  • suresh babu garine
    June 22, 2013 11:53 am

    Very nice article….. It helps us in using performance tuning

    Reply
  • its vary useful for realtime senarios

    thaks&very much article

    Reply
  • It’s a very good article to clear out the concepts of Joins in SQL.

    Reply
  • Hi Pinal,

    I have query regarding join.

    Generally we are writing the query with INNER JOIN when we want common records from both the tables.

    if we can write query like this ,

    SELECT *
    FROM t1 , t2
    WHERE t1.ID = t2.ID

    Above query is faster then INNER JOIN or Both will be the same …

    Waiting for the Expert Openion

    Reply
  • Suraj Deshpande
    August 7, 2013 5:26 pm

    Awesome and Excellent article! Keep up the good work.

    Reply
  • very simple and understandable aritcle…..thank’s

    Reply
  • This is a great article. It could only be made better if it described what type of join is being used behind the scenes when I do this: Select * from tablea, tableb or this: select * from tablea join table b. I think both are left inner, but I’m not sure

    Reply
  • Mr. Pinal Dave,

    i have a query regarding left join and right join query writing

    select * from
    table1
    left join table2 on table1.col1 = table2.col1

    select * from
    table1
    left join table2 on table2.col1 = table1.col1

    Which is the best way to write above query and why?

    Reply
  • Hi Pinal ,
    Can you please explain Outer Join Queries without Where clause and conditions in On Clause.Like
    select * from
    table1
    left join table2 on table1.col1 = table2.col1
    and table1.col3 like ‘a%’

    Reply
  • gooog one

    Reply
  • Hi,

    I have a query.. i’m using inner join between 2 tables and getting a result set. now from this result set, I need to select rows based on another condition.. how do I do that?

    Reply
  • Good one….

    Reply
  • Great forum
    In ref to Left outer Join – where Null example, how to add more conditions?
    This works –
    /* LEFT JOIN – WHERE NULL */
    SELECT t1.*,t2.*
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.ID = t2.ID
    WHERE t2.ID IS NULL

    But this doesn’t work –
    /* LEFT JOIN – WHERE NULL */
    SELECT t1.*,t2.*
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.ID = t2.ID
    WHERE t2.ID IS NULL
    AND t2.col2 = ‘XYZ’
    AND t1.col4 = ‘ABC’

    Reply
  • I try it is not /* LEFT JOIN – WHERE NULL */
    SELECT t1.*,w1.*
    FROM Table1 w1
    LEFT JOIN Table2 w2 ON w1.ID = w2.ID
    WHERE w2.ID IS NULL
    AND t2.col2 = ‘XYZ’
    AND w1.col4 = ‘ABC’

    Reply

Leave a Reply