You are on page 1of 4

School of Computing Sciences

B. Tech (IT) V SEM RDBMS LAB FOR A, B BATCHES

JOINS
SQL> create table t1(p number,q char,r number);
Table created.
SQL> create table t2(a number,b char,c number);
Table created.
SQL> insert into t1 values(10,'a',5);
1 row created.
SQL> insert into t1 values(25,'a',6);
1 row created.
SQL> insert into t1 values(15,'b',8);
1 row created.
SQL> insert into t2 values(10,'b',5);
1 row created.
SQL> insert into t2 values(25,'c',3);
1 row created.
SQL> insert into t2 values(30,'d',8);
1 row created.
SQL> select * from t1;
PQ
R
---------- - ---------10 a
5
25 a
6
Hari Seetha,SCS,VIT University

15 b

SQL> select * from t2;


AB
C
---------- - ---------10 b
5
25 c
3
30 d
8
CARTESIAN PRODUCT (CROSS JOIN)
SQL> select * from t1, t2;
PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
25 a
6
10 b
5
15 b
8
10 b
5
10 a
5
30 d
8
25 a
6
30 d
8
15 b
8
30 d
8
10 a
5
25 c
3
25 a
6
25 c
3
15 b
8
25 c
3
9 rows selected.

SQL> select * from t1 cross join t2;


PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
25 a
6
10 b
5
15 b
8
10 b
5
10 a
5
30 d
8
25 a
6
30 d
8
15 b
8
30 d
8
10 a
5
25 c
3
25 a
6
25 c
3
15 b
8
25 c
3
9 rows selected.

EQUI JOIN

Hari Seetha,SCS,VIT University

SQL> select * from t1, t2 where p=a;


PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
25 a
6
25 c
3

NATURAL JOIN
SQL> select * from t1 natural join t2 ;
PQ
RB
C
---------- - ---------- - ---------10 a
5b
5
25 a
6c
3
SQL> alter table t2 rename column P to A;
Table altered.

LEFT OUTER JOIN

SQL> select * from t1,t2 where t1.p=t2.a (+);


PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
15 b
8
25 a
6
25 c
3
RIGHT OUTER JOIN
SQL> select * from t1,t2 where t1.p(+)=t2.a;
PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
25 a
6
25 c
3
30 d
8

FULL OUTER JOIN


SQL> select * from t1 full outer join t2 on p=a;

Hari Seetha,SCS,VIT University

PQ
R
AB
C
---------- - ---------- ---------- - ---------10 a
5
10 b
5
25 a
6
25 c
3
15 b
8
30 d
8

Hari Seetha,SCS,VIT University

You might also like