Hi Guys,
if you don't want to use the key words for joining the tables like.
INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN FULL OUTER JOIN you can use plus (+) sign as well for this.
Lets assume you have two tables like TABLE_1 and TABLE_2
CREATE TABLE TABLE_1
(
id NUMBER,
name VARCHAR2 (200)
);
CREATE TABLE TABLE_2
(
id NUMBER,
name VARCHAR2 (200)
);
1. INSERT INTO TABLE_1
VALUES (1, 'ASHISH');
2. INSERT INTO TABLE_1
VALUES (2, 'SAHAY');
3. INSERT INTO TABLE_2
VALUES (1, 'DEMO');
4. INSERT INTO TABLE_2
VALUES (3, 'TEST');
Now see the queries for joins :
1. INNER JOIN ( Simple join )
SELECT t1.id id_from_table_1,
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1, TABLE_2 T2
WHERE t1.id = t2.id;
OUTPUT :
OUTPUT:
3. RIGHT OUTER JOIN
if you don't want to use the key words for joining the tables like.
INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN FULL OUTER JOIN you can use plus (+) sign as well for this.
Lets assume you have two tables like TABLE_1 and TABLE_2
CREATE TABLE TABLE_1
(
id NUMBER,
name VARCHAR2 (200)
);
CREATE TABLE TABLE_2
(
id NUMBER,
name VARCHAR2 (200)
);
1. INSERT INTO TABLE_1
VALUES (1, 'ASHISH');
2. INSERT INTO TABLE_1
VALUES (2, 'SAHAY');
3. INSERT INTO TABLE_2
VALUES (1, 'DEMO');
4. INSERT INTO TABLE_2
VALUES (3, 'TEST');
Now see the queries for joins :
1. INNER JOIN ( Simple join )
SELECT t1.id id_from_table_1,
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1, TABLE_2 T2
WHERE t1.id = t2.id;
OUTPUT :
ID_FROM_TABLE_1
|
NAME_FROM_TABLE_1
|
ID_FROM_TABLE_2
|
NAME_FROM_TABLE_2
|
1
|
ASHISH
|
1
|
DEMO
|
2. LEFT OUTER JOIN
SELECT t1.id id_from_table_1,
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1, TABLE_2 T2
WHERE t1.id = t2.id (+);
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1, TABLE_2 T2
WHERE t1.id = t2.id (+);
OUTPUT:
ID_FROM_TABLE_1
|
NAME_FROM_TABLE_1
|
ID_FROM_TABLE_2
|
NAME_FROM_TABLE_2
|
1
|
ASHISH
|
1
|
DEMO
|
2
|
SAHAY
|
-
|
-
|
3. RIGHT OUTER JOIN
SELECT t1.id id_from_table_1,
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1, TABLE_2 T2
WHERE t1.id (+) = t2.id;
OUTPUT:
ID_FROM_TABLE_1
|
NAME_FROM_TABLE_1
|
ID_FROM_TABLE_2
|
NAME_FROM_TABLE_2
|
1
|
ASHISH
|
1
|
DEMO
|
-
|
-
|
3
|
TEST
|
4. FULL OUTER JOIN :
SELECT t1.id id_from_table_1,
t1.name name_from_table_1,
t2.id id_from_table_2,
t2.name name_from_table_2
FROM TABLE_1 t1
FULL OUTER JOIN
TABLE_2 T2
ON t1.id = t2.id; -- CANNOT USE (+) SIGN
OUTPUT:
ID_FROM_TABLE_1
|
NAME_FROM_TABLE_1
|
ID_FROM_TABLE_2
|
NAME_FROM_TABLE_2
|
1
|
ASHISH
|
1
|
DEMO
|
-
|
-
|
3
|
TEST
|
2
|
SAHAY
|
-
|
-
|
No comments:
Post a Comment
Please do not add any spam links or abusive comments.