---
title: SQL Self Join
description: "## SQL Self Join – SQL Өөртэйгөө холбогдох A self join is a regular join, but the table is joined with itself. Self Join нь энгийн join боловч хүснэгт өөрөө өөртэйгөө холбогддог. ### Self Join Syntax – Self Join дүрэм…"
url: "https://apprentice.mn/docs/sql-self-join/"
updated: "2024-07-21"
category: SQL
---

# SQL Self Join

## SQL Self Join – SQL Өөртэйгөө холбогдох

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

Self Join нь энгийн join боловч хүснэгт өөрөө өөртэйгөө холбогддог.

### Self Join Syntax – Self Join дүрэм

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

`SELECT багана_нэр(нүүд)
FROM хүснэгт1 T1, хүснэгт1 T2
WHERE нөхцөл;`

*T1* and *T2* are different table aliases for the same table.

Нэг хүснэгтийг T1 болон T2 гэж өөр өөр хочтойгоор нэрлэсэн.

---

## Demo Database – Жишээ мэдээллийн сан

Below is a selection from the “Customers” table:

Доорх “Харилцагчид” хүснэгтээс жишээ болгож авсан хэсэг:

| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |

---

## SQL Self Join Example – SQL Self Join жишээ

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

Доорх SQL өгүүлбэр нь ижил хотын харилцагчдыг тааруулдаг:

### Example – Жишээ

> 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;

 [Жишээ энд дарж харна уу](https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join_self)         [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-self-join%2F&linkname=SQL%20Self%20Join)[https://www.addtoany.com/share](https://www.addtoany.com/share)
