INNER JOIN statement is used to retrieve matched data by specified column(s) between two tables. This statement returns data when at least one match is available between joined columns.
This simple example will depict us logic of inner join
CREATE TABLE TABLE_2
(
ID2 INT,
NUMBER2 VARCHAR(20)
)
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(6,'Six')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(7,'Seven')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(8,'Eight')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(9,'Nine')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(10,'Ten')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(11,'Eleven')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(12,'Twelve')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(13,'Thirteen')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(14,'Fourteen')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(15,'Fifteen')
select * from TABLE_2
This simple example will depict us logic of inner join
CREATE TABLE TABLE_1
(
ID1 INT,
NUMBER1 VARCHAR(20)
)
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(1,'One')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(2,'Two')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(3,'Three')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(4,'Four')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(5,'Five')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(6,'Six')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(7,'Seven')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(8,'Eight')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(9,'Nine')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(10,'Ten')
select * from TABLE_1
(
ID1 INT,
NUMBER1 VARCHAR(20)
)
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(1,'One')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(2,'Two')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(3,'Three')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(4,'Four')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(5,'Five')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(6,'Six')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(7,'Seven')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(8,'Eight')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(9,'Nine')
INSERT INTO TABLE_1(ID1, NUMBER1) VALUES(10,'Ten')
select * from TABLE_1
ID1 | NUMBER1 |
---|---|
1 | One |
2 | Two |
3 | Three |
4 | Four |
5 | Five |
6 | Six |
7 | Seven |
8 | Eight |
9 | Nine |
10 | Ten |
CREATE TABLE TABLE_2
(
ID2 INT,
NUMBER2 VARCHAR(20)
)
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(6,'Six')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(7,'Seven')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(8,'Eight')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(9,'Nine')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(10,'Ten')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(11,'Eleven')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(12,'Twelve')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(13,'Thirteen')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(14,'Fourteen')
INSERT INTO TABLE_2(ID2, NUMBER2) VALUES(15,'Fifteen')
select * from TABLE_2
ID2 | NUMBER2 |
---|---|
6 | Six |
7 | Seven |
8 | Eight |
9 | Nine |
10 | Ten |
11 | Eleven |
12 | Twelve |
13 | Thirteen |
14 | Fourteen |
15 | Fifteen |
select * from TABLE_1 inner join TABLE_2 on TABLE_1 .ID = TABLE_2 .ID
ID1 | NUMBER1 | ID2 | NUMBER2 |
---|---|---|---|
6 | Six | 6 | Six |
7 | Seven | 7 | Seven |
8 | Eight | 8 | Eight |
9 | Nine | 9 | Nine |
10 | Ten | 10 | Ten |
Note
NULL Values will not match in JOINS
No comments:
Post a Comment