Get Started: SQL Self Join

In This Article, You Will Know About SQL Self JOIN. 

SQL Self JOIN – Before moving ahead, let’s know a bit about SQL FULL JOIN.

Table of Contents

The SQL Self Join is used to join a table with itself as if it were given as two tables.

Syntax

				
					SELECT a. Column_name, b.Column_name…
FROM table1 A1, table1 A2
WHERE a.same_column_name = b.same_column_name;
				
			

Note: A1 and A2 are the aliases for the table name.  

Example Database

Table name: Data

S.No

Name

Class

Section

Address

1

Alex

5

A

NY-10

2

Rolex

6

B

LA-12

3

Salex

7

C

GR-15

4

Jalex

8

D

SP-18

5

Kalex

9

E

WS-20

SQL Self Join

Example: Use the SQL Self Join to join a table with itself.

				
					SELECT a.Name, b.Class, a.Address
FROM Data a, Data b
WHERE a.Address <> b.Address;
				
			

Example: Use the SQL Self Join to join a table with itself, ordered by Address.

				
					SELECT a.Name, b.Class, a.Address
FROM Data a, Data b
WHERE a.Address <> b.Address
ORDER BY a.Address;
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

1 thought on “Get Started: SQL Self Join”

Leave a Comment

Stay in the loop

codingstreets