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
sir, how to write these query?
“employee work under either pradeep or srinivas?
Do you have parent id as part of your table?
select cols from table
where parent_id in (select id from table where name in ‘pradeep’,”srinivas)
This is a good one for help
how to count id in join query for perticluer where ???????///
Hi is there any way to delete rows from down lets say i have to delete 10 ros from bottom and here i have not used any indexes. not any constraint like unique or Primary key.
I like the example’s simplicity.
This is really a very good article which I was looking for on joins
Thanks
thank’s for it. It is simplicity to understand
very good article for join. hat off you boss.
Thank you for the wonderful Explaination aout joins. All my boubts are cleared thank you so much. Keep posting.
Nice articles….
This is great, I refer to many articles on SQl Authority, really helpful. Thank a ton!
this is really nice article
What is the mean of “s.PID *= p.PID” ?
Hi Pinal, I follow your blog frequently, but some seem tough to understand. Can you please forward the Sql Server material if you have any, which is best in understanding. I’m very passionate in learning SqlServer. I’m unable to write complex queries using sub queries, group by, joins. But I really want to learn and write such kind of queries which seem very tough for me. [email removed]
Thanks
I cannot make queries of such kind if an interviewer asks me. Kindly suggest me in this regard. Thanks again
Its very use full article… thanks… can you give some more tough examples for left and right joins also
An excellent article on Joins, specially graphical presentation of joins.
Great work keep it up.
Amir Sajjad
Great article on JOIN’s PInalDave. I do have one question about Left Joins and Right Joins are they affected by doing sum’s or count’s values my return null values? For example, if I sum(sold price) and count(1) for the number of records that went into the sum(soldprice) and doing something as follows:
Select ct.CustomerNbr, ct.CustomerName, sum(soldprice), count(1)
from CustomerTable ct
Left Outer Join Orders o on o.CustomerNbr = ct.CustomerNbr
Where o.orderdate between ‘2013-01-01’ and ‘2013-01-31’
Order by ct.CustomerName
Should I not get a list of all customers and have a soldprice and count of null if the customer has no orders in the Orders table?
I thought that is the result set I would get but the customers with no orders are not in the result set.
Very well explained, now I understand joins. I don’t know why they seemed so hard to understand for me in the past. I like your graphical presentations the most.
cool explanation dude..