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
Great Dave…!
Very Very good article i have ever gone through.
now i have a very good idea about joins
Debdutta Nath…..Want to know the answer for the question you always pose for the students.
Very Good Article
Keep it up
Good Luck
This is an awosome post about joins. Very nicely presented. You simply ROCK….
This article gives me enough basic knowledge about joins….Thank u
yes it is great and so helpful.
Thanks a lot. It’s a easiest way to learn. I understood clearly. Please give this way of presentation for all the concepts of sql queries statement. it’s very useful.
This is the best article i ever saw. clean representation.. just diagrams enough to understand.
Hello
Pinal,
This one is very good article. But I need your help in one query.
There is two table PO and Inventory. PO and Inventory has orderno and Itemno common.But PO table has qty and inventory has two records for that same orederno.
like example:
PO table Inventory table
1| 1|600 1|1|400|8001
1|1|200 1|1|200|8002
Now I need data like 1|1|600|400|8001
1|1|200|200|8002
Bur when i join two table it will give me
1|1|600|400|8001
1|1|600|200|8001
1|1|200|200|8002
1|1|200|400|8002
Can you help me? How can i solve this problem?
Hi Pinky,
Generally the in same PO Inventory table primary key must be combination of PO and Item No.. because for the same PO, Item Id must not be repeated..In your case if u still want to continue u must have to increment item no… or else add a extra column
Great work.. keep going man.
Hi Pinky,
Generally the in same PO Inventory table primary key must be combination of PO and Item No.. because for the same PO, Item Id must not be repeated..In your case if u still want to continue u must have to increment item no… or else add a extra column
Hi Pinal,
The examples given were wonderful. Since you used the images it gives very clear understanding… Great job….
Great article
Need help in making a join between two tables of different tablespaces.
Lets say, table1 t1 from tablespace tb1 and table2 t2 from tablespace t2 have one corresponding column c1, c2 with same values which can be used to make a join so we can write:
select * from tb1.t1, tb2.t2 where
tb1.t1.c1 = tb2.t2.c2
this syntax is not getting me the result, so could anyone please help me with the correct syntax.
select t1.* from tb1..t1 as t1 inner join tb2..t2 as t2 on t1.c1 = t2.c2
Wow, you are very smart. I got the ideas right away. Thank you so much!
Very good approach to understand easily.
hi ,its very nice..easily understandable..
thanks a lot…
Pinal,
First of all i must say article is really nice and easy to understand.
secondly, i m used to old method of joining the table, but i understood this way of joining but my question of interest is how to perform join where we have more than two table from where we want to fetch records?
can u let me know the answer…
Hi pinal ,
I need some help on this..
I have 2 tables sales and salesdetails
sales table have (sid ,itemid ,date)
and salesdetails have (id, sid, itemid, qty)
now i want to compare sales of two years like
qty(2008-09) qty(2009-10) month
100 2000 aug
200 250 dec
plz tell me hw 2 get the same…
Search about Cross-tab reports in this site
Hello Pinal,
A very good blog, very helpful, thanks a million.
SMA
thanq very much this one i need. very good explanation hopping to have few more article related to this topic from you very soon
superb article on joins… best ever explanation…