{"id":2251,"date":"2014-02-01T01:15:11","date_gmt":"2014-01-31T19:45:11","guid":{"rendered":"http:\/\/sqlhints.com\/?p=2251"},"modified":"2015-06-29T02:40:44","modified_gmt":"2015-06-28T21:10:44","slug":"joins-in-sql-server","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2014\/02\/01\/joins-in-sql-server\/","title":{"rendered":"Joins In Sql Server"},"content":{"rendered":"<h2><a title=\"Sql Server Tutorial\" href=\"https:\/\/sqlhints.com\/tutorialsqlserver\/\" target=\"_blank\">Sql Server Tutorial<\/a> Lesson 6: JOINS in Sql Server with Examples<\/h2>\n<p style=\"text-align: justify;\">Join facilitates the retrieval of information from multiple tables.\u00a0In Sql server we have following 6 types of Joins:<\/p>\n<ol>\n<li><strong>INNER JOIN<\/strong><\/li>\n<li><strong>LEFT OUTER JOIN<\/strong><\/li>\n<li><strong>RIGHT OUTER JOIN<\/strong><\/li>\n<li><strong>FULL OUTER JOIN<\/strong><\/li>\n<li><strong>CROSS JOIN<\/strong><\/li>\n<li><strong>SELF JOIN<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">To demo these features let us first create the Customers and Orders table as depicted in the below image by using the following script:<\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/Joins1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/Joins1.jpg\" alt=\"Joins1\" width=\"629\" height=\"159\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/Joins1.jpg 629w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/Joins1-300x75.jpg 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">CREATE DATABASE SqlHintsJoinDemo\r\nGO\r\nUSE SqlHintsJoinDemo\r\nGO\r\n--Create Customers Table and Insert records\r\nCREATE TABLE Customers \r\n( CustomerId INT, Name VARCHAR(50) )\r\nGO\r\nINSERT INTO Customers(CustomerId, Name) VALUES(1,&#039;Shree&#039;)\r\nINSERT INTO Customers(CustomerId, Name) VALUES(2,&#039;Kalpana&#039;)\r\nINSERT INTO Customers(CustomerId, Name) VALUES(3,&#039;Basavaraj&#039;)\r\nGO\r\n\r\n--Create Orders Table and Insert records into it\r\nCREATE TABLE Orders\r\n(OrderId INT, CustomerId INT, OrderDate DateTime)\r\nGO\r\nINSERT INTO Orders(OrderId, CustomerId, OrderDate) \r\nVALUES(100,1,Getdate()-1)\r\n\r\nINSERT INTO Orders VALUES(200,4,Getdate())\r\nINSERT INTO Orders VALUES(300,3,Getdate()+1)\r\nGO<\/pre>\n<p><strong>[ALSO READ] <a title=\"Joining Two Tables without any Common Column between them \u2013 Sql Server\" href=\"https:\/\/sqlhints.com\/2013\/09\/07\/joining-two-tables-without-any-common-column-between-them-sql-server\/\" target=\"_blank\">Joining Two Tables without any Common Column between them<\/a><\/strong><\/p>\n<h2>1. INNER JOIN in Sql Server<\/h2>\n<p style=\"text-align: justify;\">Inner Join returns only the matching rows in both the tables (i.e. returns only those rows for which the join condition satisfies).<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1:<\/strong> As per the data in our demo tables, Customers with CustomerId 1 and 3 in Customers table have the orders in the Orders table. Where as the customer with CustomerId 2 doesn&#8217;t have any order in the Orders table. So the Inner Join on the CustomerId column between Customers and Orders table will return the Customer and Order details of the Customers with CustomerId 1 and 3 only.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * \r\nFROM  Customers C \r\n\t\tINNER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/INNER-JOIN.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2258\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/INNER-JOIN.jpg\" alt=\"INNER JOIN\" width=\"744\" height=\"436\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/INNER-JOIN.jpg 744w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/INNER-JOIN-300x175.jpg 300w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2:<\/strong> Below Inner Join query demonstrates how to get name of all the \u00a0Customer who have at-least one order in the Orders table.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT C.Name \r\nFROM  Customers C \r\n\t\tINNER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nName<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nBasavaraj<br \/>\nShree<\/p>\n<h2>2. Left OUTER JOIN in Sql Server<\/h2>\n<p>Left Outer Join\/Left Join returns all the rows from the LEFT table and the corresponding matching rows from the right table. If right table doesn&#8217;t have the matching record then for such records right table column will have NULL value in the result.<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1:<\/strong> As per the data in our demo tables, Customers with CustomerId 1 and 3 in Customers table have the orders in the Orders table. Where as the customer with CustomerId 2 doesn&#8217;t have any order in the Orders table. So the Left join on the CustomerId column between Customers and Orders table will return the Customer and Order details of the Customers with CustomerId 1 and 3 and for CustomerId 2 the Order Table columns will have NULL value in the result.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * \r\nFROM  Customers C \r\n\t\tLEFT OUTER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/LEFT-OUTER-JOIN1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2271\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/LEFT-OUTER-JOIN1.jpg\" alt=\"LEFT OUTER JOIN1\" width=\"754\" height=\"455\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/LEFT-OUTER-JOIN1.jpg 754w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/LEFT-OUTER-JOIN1-300x181.jpg 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2:<\/strong> Below query demonstrates how to get the name of the Customer who don&#8217;t have Orders using LEFT OUTER JOIN.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT C.CustomerId, C.Name \r\nFROM  Customers C \r\n\t\tLEFT OUTER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId\r\nWHERE O.OrderId IS NULL<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nCustomerId Name<br \/>\n&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2 Kalpana<\/p>\n<h2>3. RIGHT OUTER JOIN in Sql Server<\/h2>\n<p style=\"text-align: justify;\">Right Outer Join\/Right Join returns all the rows from the RIGHT table and the corresponding matching rows from the left table. If left table doesn&#8217;t have the matching record then for such records left table column will have NULL value in the result.<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1:<\/strong> As per the data in our demo tables, only for the order with OrderId 200 we don&#8217;t have it&#8217;s corresponding customer info with CustomerId 4 in the Customers table. And for the other two orders, the corresponding customer info is present in the Customers Table. So for the orders with CustomerId 1 and 3 will have customer details and for the order with CustomerId 4, the Customers table columns will have NULL value in the result.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT *\r\nFROM  Customers C \r\n\t\tRIGHT OUTER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/RIGHT-OUTER-JOIN.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2268\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/RIGHT-OUTER-JOIN.jpg\" alt=\"RIGHT OUTER JOIN\" width=\"747\" height=\"445\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/RIGHT-OUTER-JOIN.jpg 747w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/RIGHT-OUTER-JOIN-300x178.jpg 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2:<\/strong> Below query demonstrates how to get the Orders with a CustomerId, for which we don&#8217;t have a mapping any record in the Customers Table:<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT O.*\r\nFROM  Customers C \r\n\t\tRIGHT OUTER JOIN Orders O\r\n\t\t\tON O.CustomerId = C.CustomerId\r\nWHERE C.CustomerId IS NULL<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nOrderId CustomerId OrderDate<br \/>\n&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n200 4 2014-01-31 23:48:32.853<\/p>\n<h2>4. FULL OUTER JOIN in Sql Server<\/h2>\n<p style=\"text-align: justify;\">It returns all the rows from both the tables, if there is no matching row in either of the sides\u00a0then it displays NULL values in the result for that table columns in such rows.<\/p>\n<p><strong>Full Outer Join = Left Outer Join + Right Outer Join<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1:<\/strong> As per the data in our Demo tables the Customer with CustomerId 2 doesn&#8217;t have order in the Orders table. So in the result of FULL Outer join between Customers and Orders table on the CustomerId column will have NULL values for the Orders table columns for the Customer with CustomerId 2.<\/p>\n<p>And for the Order with OrderId 200 having CustomerId 4 doesn&#8217;t have a matching record in the customer table with CustomerId 4.\u00a0So in the result of FULL Outer join between Customers and Orders table on the CustomerId column will have NULL values for the Customers table columns for the Order with OrderId 200.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT *\r\n\tFROM Customers C\r\n\t\t\tFULL OUTER JOIN Orders O\r\n\t\t\t\tON O.CustomerId = C.CustomerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOIN.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2277\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOIN.jpg\" alt=\"FULL OUTER JOIN\" width=\"740\" height=\"462\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOIN.jpg 740w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOIN-300x187.jpg 300w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2:<\/strong> Below query demonstrates how to get the list of all the Customers without Orders and also the Orders which doesn&#8217;t have corresponding customer in the Customers Table.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT *\r\n\tFROM Customers C\r\n\t\t\tFULL OUTER JOIN Orders O\r\n\t\t\t\tON O.CustomerId = C.CustomerId\r\nWHERE C.CustomerId IS NULL OR O.OrderId IS NULL<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOINDemo2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2279\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOINDemo2.jpg\" alt=\"FULL OUTER JOINDemo2\" width=\"543\" height=\"69\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOINDemo2.jpg 543w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/FULL-OUTER-JOINDemo2-300x38.jpg 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/a><\/p>\n<h2>5. CROSS JOIN in Sql Server<\/h2>\n<p style=\"text-align: justify;\">Cross join is also referred to as Cartesian Product. For every row in the LEFT Table of the CROSS JOIN all the rows from the RIGHT table are returned and Vice-Versa (i.e.result will have the Cartesian product of the rows from join tables).<\/p>\n<p><strong>No.of Rows in the Result of CRoss Join<\/strong> = (<strong>No. of Rows in LEFT Table<\/strong>) <strong>*<\/strong> (<strong>No. of Rows in RIGHT Table<\/strong>)<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT *\r\nFROM Customers C\r\n\t\tCROSS JOIN Orders O<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/CROSS-JOIN.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2281\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/CROSS-JOIN.jpg\" alt=\"CROSS JOIN\" width=\"715\" height=\"489\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/CROSS-JOIN.jpg 715w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/CROSS-JOIN-300x205.jpg 300w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/a><\/p>\n<h2>6. SELF JOIN in Sql Server<\/h2>\n<p style=\"text-align: justify;\">If a Table is joined to itself using one of the join types explained above, then such a type of join is called SELF JOIN.<\/p>\n<p>To demo this join let us create an Employee table with data as depicted in the below image by the following script:<\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2283\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN.jpg\" alt=\"SELF JOIN\" width=\"283\" height=\"92\" \/><\/a><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">CREATE TABLE Employee\r\n(EmployeeId INT, Name NVARCHAR(50), ManagerId INT)\r\nGO\r\nINSERT INTO Employee VALUES(1,&#039;Shree&#039;,1)\r\nINSERT INTO Employee VALUES(2,&#039;Kalpana&#039;,1)\r\nINSERT INTO Employee VALUES(3,&#039;Basavaraj&#039;,2) \r\nINSERT INTO Employee VALUES(4,&#039;Monty&#039;,2) \r\nGO<\/pre>\n<p style=\"text-align: justify;\"><strong>Demo 1:<\/strong> Now if we need to get the name of the Employee and his Manager name for each employee in the Employee Table. Then we have to Join Employee Table to itself as Employee and his Manager data is present in this table only as shown in the below query:<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT E.EmployeeId, \r\n       E.Name &#039;Employee Name&#039;, M.Name &#039;Manager Name&#039; \r\nFROM dbo.Employee E \r\n\t\tINNER JOIN Employee M\r\n\t\t\tON M.EmployeeId = E.ManagerId<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN-RESULT.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2285\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN-RESULT.jpg\" alt=\"SELF JOIN RESULT\" width=\"334\" height=\"103\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN-RESULT.jpg 334w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/01\/SELF-JOIN-RESULT-300x92.jpg 300w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/a><\/p>\n<p><strong>[ALSO READ] <a title=\"Joining Two Tables without any Common Column between them \u2013 Sql Server\" href=\"https:\/\/sqlhints.com\/2013\/09\/07\/joining-two-tables-without-any-common-column-between-them-sql-server\/\" target=\"_blank\">Joining Two Tables without any Common Column between them<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sql Server Tutorial Lesson 6: JOINS in Sql Server with Examples Join facilitates the retrieval of information from multiple tables.\u00a0In Sql server we have following 6 types of Joins: INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN CROSS JOIN SELF JOIN To demo these features let us first create the Customers and &hellip; <a href=\"https:\/\/sqlhints.com\/2014\/02\/01\/joins-in-sql-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Joins In Sql Server<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,242],"tags":[271,593,270,595,267,590,305,286,264,266,285,591,268,594,592,269,272,597,587,585,582,265,589,583,586,584,596,986,588,996],"class_list":["post-2251","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-sql-server-tutorial","tag-cross-join","tag-cross-join-sql","tag-full-outer-join","tag-full-outer-join-sql","tag-inner-join","tag-inner-join-sql","tag-join-between-two-tables","tag-joining-two-tables-with-examples","tag-joins","tag-joins-in-sql-server","tag-joins-with-examples","tag-left-join-sql","tag-left-outer-join","tag-outer-join-sql","tag-right-join-sql","tag-right-outer-join","tag-self-join","tag-self-join-sql","tag-sql-cross-join","tag-sql-full-outer-join","tag-sql-inner-join","tag-sql-joins","tag-sql-left-join","tag-sql-left-outer-join","tag-sql-outer-join","tag-sql-right-outer-join","tag-sql-self-join","tag-sql-server","tag-sql-server-joins","tag-sql-server-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-Aj","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=2251"}],"version-history":[{"count":29,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2251\/revisions"}],"predecessor-version":[{"id":3452,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2251\/revisions\/3452"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}