bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL Self Join

Concept visual

SQL Self Join

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Overview

A self join is a regular join, but the table is joined with itself.

Self Join Syntax

Select

column_name(s)

From

table1 T1, table1 T2 WHERE condition

;

T1 and

T2 are different table aliases for the same table.

Demo Database

Below is a selection from the "Customers" table:

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin

12209

Germany

Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222 México D.F. 05021

Mexico

Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F. 05023

Mexico

SQL Self Join Example

The following SQL statement matches customers that are from the same city:

Previous

SQL FULL JOIN

Next

SQL UNION Operator