# SQL Self JOIN

### SQL Self JOIN

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

#### Self JOIN Syntax

```sql
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
```

### Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:<br>

![](https://826093633-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LS8lPNzCGPR5-DLbGXv%2F-LSY-waNYjqnNNKdvlkO%2F-LSY0N6cnx0JjfnNVpZC%2Fimage.png?alt=media\&token=d5041b95-bad6-48e4-b8b7-cac0c67c6562)

\
SQL Self JOIN Example

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

```sql
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City 
ORDER BY A.City;

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-sql.gitbook.io/sql-programming/sql-select/sql-joins/sql-self-join.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
