Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

JOINS IN SQL

A SQL JOIN combines records from two tables.


A JOIN locates related column values in the two tables.
A query can contain zero, one, or multiple JOIN operations.

List of different types of joins are the following:


1. Equijoin
2. Inner Join
3. Outer Join
 Left outer join
 Right outer join
 Full outer Join
4. Natural Join
5. Cross Join.
6. Self-join

CREATE TABLE EMP(


EMP_NO INT ,
E_NAME VARCHAR(20),
DEPT_NO INT
);
INSERT INTO EMP VALUES (102,'JAY ARORA','9384');
INSERT INTO EMP VALUES (99,'HARSH ARORA','9385');
INSERT INTO EMP VALUES (201,'ROHIT PATEL','9386');
INSERT INTO EMP VALUES (194,'BUNNY ARORA','9388');
INSERT INTO EMP VALUES (142,'SHREYAS','9389');
SELECT * FROM EMP;
CREATE TABLE DEPT(
E_NAME VARCHAR(30),
DEPT_NO INT
);
INSERT INTO DEPT VALUES ('AJAY RAHANDALE','9372');
INSERT INTO DEPT VALUES ('JAY ARORA','9384');
INSERT INTO DEPT VALUES ('BUNNY ARORA','9388');
INSERT INTO DEPT VALUES ('LUCKY PARMAR','9379');
INSERT INTO DEPT VALUES ('VIVEK PATEL','9369');
SELECT * FROM DEPT;

1. EQUI JOIN :
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables. EQUI
JOIN also create JOIN by using JOIN with ON and then providing the names of the columns with their
relative tables to check equality using equal sign (=).
SELECT * FROM EMP,DEPT WHERE EMP.DEPT_NO=DEPT.DEPT_NO;

EMP_NO E_NAME DEPT _NO E_NAME DEPT _NO


102 JAY ARORA 9384 JAY ARORA 9384

194 BUNNY ARORA 9388 BUNNY ARORA 9388

2.INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies. This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be same.
SELECT * FROM EMP INNER JOIN DEPT ON EMP.DEPT_NO=DEPT.DEPT_NO

EMP_NO E_NAME DEPT _NO E_NAME DEPT _NO


102 JAY ARORA 9384 JAY ARORA 9384
194 BUNNY ARORA 9388 BUNNY ARORA 9388

3.OUTER JOIN

1. LEFT OUTER JOIN: This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no matching
row on right side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER
JOIN.

SELECT * FROM EMP LEFT OUTER JOIN DEPT ON EMP.DEPT_NO=DEPT.DEPT_NO;

EMP_NO E_NAME DEPT _NO E_NAME DEPT _NO

102 JAY ARORA 9384 JAY ARORA 9384

194 BUNNY ARORA 9388 BUNNY ARORA 9388

142 SHREYAS 9389 - -

99 HARSH ARORA 9385 - -

201 ROHIT PATEL 9386 - -

2. RIGHT OUTER JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows
of the table on the right side of the join and matching rows for the table on the left side of join.
The rows for which there is no matching row on left side, the result-set will contain null. RIGHT
JOIN is also known as RIGHT OUTER JOIN.

SELECT * FROM EMP RIGHT OUTER JOIN DEPT ON EMP.DEPT_NO=DEPT.DEPT_NO;

EMP_NO E_NAME DEPT _NO E_NAME DEPT _NO

102 JAY ARORA 9384 JAY ARORA 9384

194 BUNNY ARORA 9388 BUNNY ARORA 9388

- - - VIVEK PATEL 9369

- - - AJAY RAHANDALE 9372

- - - LUCKY PARMAR 9379

3. FULL OUTER JOIN: FULL JOIN creates the result-set by combining result of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.

SELECT * FROM EMP FULL OUTER JOIN DEPT ON EMP.DEPT_NO=DEPT.DEPT_NO;

EMP_NO E_NAME DEPT _NO E_NAME DEPT _NO


102 JAY ARORA 9384 JAY ARORA 9384

194 BUNNY ARORA 9388 BUNNY ARORA 9388

142 SHREYAS 9389 - -


99 HARSH ARORA 9385 - -

201 ROHIT PATEL 9386 - -

- - - VIVEK PATEL 9369

- - - AJAY RAHANDALE 9372

- - - LUCKY PARMAR 9379

4. Natural Join :
Natural Join joins two tables based on same attribute name and datatypes. The resulting
table will contain all the attributes of both the table but keep only one copy of each
common column.
SELECT * FROM EMP NATURAL JOIN DEPT

E_NAME DEPT _NO EMP_NO

JAY ARORA 9384 102

BUNNY ARORA 9388 194

5. Cross Join :
Cross join allows us to join each and every row of both the tables. It is similar to the cartesian product
that joins all the rows.
SELECT * FROM EMP CROSS JOIN DEPT
6. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the
table is joined with itself and all other rows depending on some conditions. A SELF JOIN is a
regular join, but the table is joined with itself – this is extremely useful for
comparisons within a table.

SQL> CREATE TABLE CUSTOMERS


(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (6, 'Komal', 22, 'MP', 4500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES(7, 'Ashish',26, 'Mumbai', 6500.00)

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES(8, 'Mahesh', 28, 'Kota',5500.00)

Problem: Match customers that are from the same address


Answer: SELECT A.ID, A.NAME, B.ADDRESS, B.SALARY
FROM CUSTOMERS A
JOIN
CUSTOMERS B
ON
A.ID<>B.ID AND A.ADDRESS=B.ADDRESS
ORDER BY A.ADDRESS

Note: A and B are Aliases for the same Customer table.

ID NAME ADDRESS SALARY

3 kaushik Kota 5500

8 Mahesh Kota 2000

4 Chaitali Mumbai 6500

7 Ashish Mumbai 6500

You might also like