1JOINING 2 Tables in sql
2
3SELECT X.Column_Name , Y.Column_Name2
4FROM TABLES1_NAME X
5INNER JOIN TABLES2_NAME Y ON X.Primary_key = Y.Foreign_key;
6
7
8--FOR EXAMPLE
9--GET THE FIRST_NAME AND JOB_TITLE
10--USE EMPLOYEES AND JOBS TABLE
11--THE RELATIONSHIP IS JOB_ID
12
13SELECT E.FIRST_NAME , J.JOB_TITLE
14FROM EMPLOYEES E
15INNER JOIN JOBS J ON J.JOB_ID = E.JOB_ID;
16
17
1INNER JOIN:
2is used when retrieving data from multiple
3tables and will return only matching data.
4
5LEFT OUTER JOIN:
6is used when retrieving data from
7multiple tables and will return
8left table and any matching right table records.
9
10RIGHT OUTER JOIN:
11is used when retrieving data from
12multiple tables and will return right
13table and any matching left table records
14
15FULL OUTER JOIN:
16is used when retrieving data from
17multiple tables and will return both
18table records, matching and non-matching.
19
20
21
22INNER JOIN :
23SELECT select_list From TableA A
24Inner Join TableB B
25On A.Key = B.Key
26
27
28LEFT OUTER JOIN :
29SELECT select_list From TableA A
30Left Join TableB B
31On A.Key = B.Key
32
33(where b.key is null)//For delete matching data
34
35
36
37RIGTH OUTER JOIN :
38SELECT select_list From TableA A
39Right Join TableB B
40On A.Key = B.Key
41
42
43FULL JOIN :
44SELECT select_list From TableA A
45FULL OUTER Join TableB B
46On A.Key = B.Key
47
48
1#Joining 2 tables: table_1 and table_1
2
3#Select the all the columns you want to show in the final joined table.
4SELECT
5 table_1.column_a,
6 table_2.column_b
7#Select the first table
8FROM
9 database_name.table_1
10#Select the type of join statement and second table you want to join.
11JOIN
12 database_name.table_2
13#Select the related columns on which you want to join the tables.
14ON
15 table_1.column_a = table_2.column_b
16