JOINING 2TablesinsqlSELECT X.Column_Name , Y.Column_Name2
FROM TABLES1_NAME X
INNERJOIN TABLES2_NAME Y ON X.Primary_key = Y.Foreign_key;--FOR EXAMPLE--GET THE FIRST_NAME AND JOB_TITLE--USE EMPLOYEES AND JOBS TABLE--THE RELATIONSHIP IS JOB_IDSELECT E.FIRST_NAME , J.JOB_TITLE
FROM EMPLOYEES E
INNERJOIN JOBS J ON J.JOB_ID = E.JOB_ID;
-- Rows with ID existing in both a, b and c-- JOIN is equivalent to INNER JOINSELECT a.ID, a.NAME, b.VALUE1, c.VALUE1 FROM table1 a
JOIN table2 b ON a.ID = b.ID
JOIN table3 c ON a.ID = c.ID
WHERE a.ID >=1000;-- ⇓ Test it ⇓ (Fiddle source link)
INNERJOIN:
is used when retrieving datafrom multiple
tablesand will return only matching data.LEFTOUTERJOIN:
is used when retrieving datafrom
multiple tablesand will returnlefttableandany matching righttable records.RIGHTOUTERJOIN:
is used when retrieving datafrom
multiple tablesand will returnrighttableandany matching lefttable records
FULLOUTERJOIN:
is used when retrieving datafrom
multiple tablesand will return both
table records, matching and non-matching.INNERJOIN :
SELECT select_list From TableA A
InnerJoin TableB B
On A.Key= B.KeyLEFTOUTERJOIN :
SELECT select_list From TableA A
LeftJoin TableB B
On A.Key= B.Key(where b.keyisnull)//For delete matching data
RIGTH OUTERJOIN :
SELECT select_list From TableA A
RightJoin TableB B
On A.Key= B.KeyFULLJOIN :
SELECT select_list From TableA A
FULLOUTERJoin TableB B
On A.Key= B.Key
SELECT table1.column1,table1.column2,table2.column1,....FROM table1
INNERJOIN table2
ON table1.matching_column = table2.matching_column;
table1: Firsttable.
table2: Secondtable
matching_column: Column common to both the tables.
#INNER JOIN: Intersection between 2 tablesSELECT*FROM A
INNERJOIN B ON A.key= B.key#LEFT JOIN: left table with the intersection joined to the right tableSELECT*FROM A
LEFTJOIN B ON A.key= B.key#LEFT JOIN(without intesection): left table without the intersection joined# to the right tableSELECT*FROM A
LEFTJOIN B ON A.key= B.keyWHERE B.keyISNULL#RIGHT JOIN: right table with the intersection joined to the left tableSELECT*FROM A
RIGHTJOIN B ON A.key= B.key#RIGHT JOIN(without intesection): right table without the intersection joined# to the left tableSELECT*FROM A
RIGHTJOIN B ON A.key= B.keyWHERE A.keyISNULL# there seems to be a mistake on the site (B changed to A)#FULL JOIN: union of 2 tableSELECT*FROM A
FULLJOIN B ON A.key= B.key#FULL JOIN(without intesection): union of two table without the intersectionSELECT*FROM A
FULLJOIN B ON A.key= B.keyWHERE A.keyISNULLOR B.keyISNULL
LEFTJOINAllrowsfrom the lefttable will be returned, even if there's no matching row in the right table.
RIGHT JOIN
All rows from the right table will be returned, even if there's no matching rowin the lefttable.INNERJOIN
Only returnsrowswhere there's a matching rowin both tables.