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.
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.
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.
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.
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.
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
The above example can also be created using Right Outer Join.
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.
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)
402 Comments. Leave new
Good article. Crisp and to the point. Venn diagrams explain better than any amount of verbal explanation. Good job and thank you.
Simply Superb…Thanks a lot.
very very very goood explaination…. Thank you …
I have a question.
Let us assume, I have Table t1(c1, c2, c3) & t2(c2, c4, c5).
SELECT t1.*, t2.*
FROM t1 LEFT JOIN t2 ON t1.C2=t2.C2;
SELECT t1.*, t2.*
FROM t2 RIGHT JOIN t1 ON t2.C2=t1.C2;
Both yields same results. Then Why LEFT and RIGHT JOINS Required?
Here you have the column names in the above example. Give some values to understand why is returning the same results. For example, if in t1 the values are (a,1,aa), (b,2,bb) and in t2 you have (1,d,dd) and (2,e,ee) you will have the same results. Because the common set of data is 1,2 for column c2 and there are no values that are only in t1 or only in t2.
very very very goood explaination…. Thank you
thanks man .
thats a great job.
it has help me grab the concept without stress at all.
Excellent work brother. It is easy understand. For me who fear with joins, its a splendid explanation by presentaing graphically.
Thanks a lot……
Keep adding interesting stuff.
Excellent explanation Dave. It would be really useful if you can present similar explanation for joining 3 tables or more. It gets really complicated when we need a left outer join of 1 table with more than 1 table.
Excellent but some more explanation needed
Excellent Explanation
than k u sir very useful sir……
Thanks sir very much.This article help me a lot
GOOD
Simply Superb !!
Awesome description of join
The description is very impressive and easy to understand.
Thank You very much.
Pinal, good explanation, Can you also explain about External Join
hai pinaldave,
Please can u help on joins
i’ve three tables A,B,C
A table having 1 row and B Table having 2 rows and C Table having three rows .
Now i want to select all the rows from 3 tables.
can u help on this task
Thanks,
Narasimha.M
hai pinaldave,
Please can u help on joins
i’ve three tables A,B,C
A table having 1 row and B Table having 2 rows and C Table having three rows .A table pk is fk for remaining tables[B,C]
Now i want to select all the rows from 3 tables.
can u help on this task
select instr(‘sumitishappay’,’p’,-1)from dual;
it should count from left …….but i m getting o/p,,11