1SELECT column_name(s)
2FROM table1
3INNER JOIN table2
4ON table1.column_name = table2.column_name;
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
1-- Rows with ID existing in both a and b
2-- JOIN is equivalent to INNER JOIN
3SELECT a.ID, a.NAME, b.VALUE1 FROM table1 a
4 JOIN table2 b ON a.ID = b.ID
5WHERE a.ID >= 1000;
6-- ⇓ Test it ⇓ (Fiddle source link)
1A relational database consists of multiple related tables linking together using common columns which are known as foreign key columns. Because of this, data in each table is incomplete from the business perspective.
2MySQL supports the following types of joins:
3
4Inner join
5Left join
6Right join
7Cross join
8
9The following shows the basic syntax of the inner join clause that joins two tables table_1 and table_2:
10
11SELECT column_list
12FROM table_1
13INNER JOIN table_2 ON join_condition;
14
15
16SELECT column_list
17FROM table_1
18INNER JOIN table_2 USING (column_name);
19
20SELECT column_list
21FROM table_1
22LEFT JOIN table_2 USING (column_name);
23Here is the syntax of the right join:
24
25SELECT column_list
26FROM table_1
27RIGHT JOIN table_2 ON join_condition;
28
29The following shows the basic syntax of the cross join clause:
30
31SELECT select_list
32FROM table_1
33CROSS JOIN table_2;