By this article i am trying to show you the joins in different way i.e. by in Maths Terms by the sets as well as the query which will give you the return in one single cilck of Execute in Management Studio.
Joins :- Joins is basically returns the resultset by making a merge between two table (which is at row level).
Types Of Joins :-
1] Inner Join
2] Outer Join
3] Self Join
Lets consider the 2 sets that we have in maths
1] Natural Number {Column name = iNATURAL_NUMBER int}
2] Even-Odd Numbers (shuffle i.e. some from Even and some from ODD) {Column name = iEVEN_ODD_NUMBER int}
#1 is our set/Table A={0,3,4,7,8,9,10,11,12,13}
#2 is our next set/Table B={0,2,3,6,7,10,11,14}
Lets consider we have above data in 2 tables that is
in table A we have data 0,3,4,7,8,9,10,11,13 and in table B we have data 0,2,3,6,7,10,11,14
1] Inner Join :- it is an simple join between two tables which returns the resultsets for matching of rows based on the on clause of join.
Math Term Inner Join will be for A & B is
A Inner JOIN B ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER = {0,3,7,10,11}
2] Outer Join :- it has 3 sub types
2.1] Left Outer Join
2.2] Right Outer Join
2.3] Full Outer Join
2.1] Left Outer Join :- it is an join applied on Left table which returns the resultsets for rows based on the on clause of join and for non matching give NULL from right table. It give all the records from the Left table and Null from the nonmatching in Right table.
A Left Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER = {0,3,4,7,8,9,10,11,12,13}
if we used Left outer Join on query gives NULL for the Right Table in our example for (4,8,9,12,13) gives (NULL,NULL,NULL,NULL,NULL) from query which is not in the table B.
2.2] Right Outer Join :- it is an join applied on Right table which returns the resultsets for rows based on the on clause of join and for non matching give NULL from Left table. It give all the records from the Right table and Null from the nonmatching in Left table.
A Right Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER = {0,2,3,6,7,10,11,14}
if we used Right outer Join on query gives NULL for the Left Table in our example for (2,6,14) gives (NULL,NULL,NULL) from query which is not in the table A.
2.3] Full Outer Join :- it is an join applied on both table which returns the resultsets for rows based on the on clause of join and for non matching give NULL from Right as well as Left table. It give all the records from the Right as well as Left table and Null from the nonmatching in right as well as left table.
A Full Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER = {0,2,3,4,6,7,8,9,10,11,12,13,14}
if we used Full outer Join on query gives NULL for the right as well as left Table in our example for Right (4,8,9,12,13) gives (NULL,NULL,NULL,NULL,NULL) and for Left (2,6,14) gives (NULL,NULL,NULL) from query which is not in the table A and B.
2.4] Left Outer Join With NULL :- it will use the out put from the #2.1 and returns for only non matching from Right table
A Left Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER WHERE B.iEVEN_ODD_NUMBER is NULL = {4,8,9,12,13}
if we used Left outer Join with NULL on query gives all the records from the left table having null from right table in our example for (4,8,9,12,13) gives (NULL,NULL,NULL,NULL,NULL) from query which is not in the table B.
2.5] Right Outer Join With NULL :- it will use the out put from the #2.2 and returns for only non matching from Left table
A Right Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER WHERE A.iNATURAL_NUMBER is NULL = {2,6,14}
if we used Right outer Join with NULL on query gives all the records from the left table having null from Left table in our example for (2,6,14) gives (NULL,NULL,NULL) from query which is not in the table A.
2.6] Full Outer Join With NULL :- it will use the out put from the #2.3 and returns for only non matching from Left table
A Right Outer Join ON A.iNATURAL_NUMBER =B.iEVEN_ODD_NUMBER WHERE A.iNATURAL_NUMBER is NULL OR B.iEVEN_ODD_NUMBER IS NULL = {4,8,9,12,13,2,6,14}
if we used Full outer Join with NULL on query gives all the records from the left & Right table having null from Left table in our example for (2,6,14) gives (NULL,NULL,NULL) and Right table in our example for (4,8,9,12,13) gives (NULL,NULL,NULL,NULL,NULL) from query which is not in the table A and B.
——————-
The query which you can directly run for Joins
IF EXISTS (SELECT * FROM sys.databases WHERE name =’JOINS’)
BEGIN
USE master
DROP DATABASE JOINS
CREATE DATABASE JOINS
END
ELSE CREATE DATABASE JOINS
USE JOINS
CREATE SCHEMA JOIN_DEMO
–DROP TABLE JOINS.JOIN_DEMO.NATURAL_NUMBERS
CREATE TABLE JOINS.JOIN_DEMO.NATURAL_NUMBERS
(
iNATURAL_ID INT IDENTITY(1,1) PRIMARY KEY,
iNATURAL_NUMBER INT
)
INSERT INTO JOINS.JOIN_DEMO.NATURAL_NUMBERS VALUES(0),(3),(4),(7),(8),(9),(10),(11),(12),(13)
–DROP TABLE JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS
CREATE TABLE JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS
(
iEVEN_ODD_ID INT IDENTITY(1,1) PRIMARY KEY,
iEVEN_ODD_NUMBER INT
)
INSERT INTO JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS VALUES(2),(11),(6),(7),(10),(14),(3),(0)
SELECT iNATURAL_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS
SELECT iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS
—Types of joins
—- 1] Inner Join
—- 2] Outer Join
—- 3] Self Join
—- 4] Cross Join
—-#1] Inner Join
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
—-#2] Outer Join has sub joins…
—- #2.1] Left Outer Join
—- #2.2] Right Outer Join
—- #2.3] Full Outer Join
—- #2.4] Left Outer Join WITH NULL
—- #2.5] Right Outer Join WITH NULL
—- #2.6] Full Outer Join WITH NULL
—- #2.1] Left Outer Join
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
LEFT OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
—- #2.2] Right Outer Join
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
RIGHT OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
—- #2.3] Full Outer Join
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
FULL OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
—- #2.4] Left Outer Join WITH NULL
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
LEFT OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
WHERE EN.iEVEN_ODD_NUMBER IS NULL
—- #2.5] Right Outer Join WITH NULL
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
RIGHT OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
WHERE NN.iNATURAL_NUMBER IS NULL
—- #2.6] Full Outer Join WITH NULL
SELECT NN.iNATURAL_NUMBER,EN.iEVEN_ODD_NUMBER FROM JOINS.JOIN_DEMO.NATURAL_NUMBERS NN WITH(NOLOCK)
FULL OUTER JOIN JOINS.JOIN_DEMO.EVEN_ODD_NUMBERS EN WITH(NOLOCK) ON NN.iNATURAL_NUMBER = EN.iEVEN_ODD_NUMBER
WHERE EN.iEVEN_ODD_NUMBER IS NULL OR NN.iNATURAL_NUMBER IS NULL