---
title: SQL Inner Join
description: "## INNER JOIN The `INNER JOIN` keyword selects records that have matching values in both tables. `INNER JOIN` түлхүүр үг нь хоёр хүснэгтэд таарч буй утгатай мэдээллүүдийг сонгодог. Let’s look at a selection of the **Products** table: **Бүтээгдэхүүнүүд** хүснэгтийн хэсгийг үзье: | ProductID |…"
url: "https://apprentice.mn/docs/sql-inner-join/"
updated: "2024-07-18"
category: SQL
---

# SQL Inner Join

## INNER JOIN

The `INNER JOIN` keyword selects records that have matching values in both tables.

`INNER JOIN` түлхүүр үг нь хоёр хүснэгтэд таарч буй утгатай мэдээллүүдийг сонгодог.

Let’s look at a selection of the **Products** table:

**Бүтээгдэхүүнүүд** хүснэгтийн хэсгийг үзье:

| ProductID | ProductName | CategoryID | Price |
| --- | --- | --- | --- |
| 1 | Chais | 1 | 18 |
| 2 | Chang | 1 | 19 |
| 3 | Aniseed Syrup | 2 | 10 |

And a selection of the **Categories** table:

Бас**Ангиллууд** хүснэгтээс хэсгийг үзье:

| CategoryID | CategoryName | Description |
| --- | --- | --- |
| 1 | Beverages | Soft drinks, coffees, teas, beers, and ales |
| 2 | Condiments | Sweet and savory sauces, relishes, spreads, and seasonings |
| 3 | Confections | Desserts, candies, and sweet breads |

We will join the Products table with the Categories table, by using the `CategoryID` field from both tables:

Бүтээгдэхүүнүүд хүснэгт, Ангиллууд хүснэгтийн `АнгилалID` талбараар нь холбож үзье:

### Example – Жишээ

Join Products and Categories with the INNER JOIN keyword:

INNER JOIN түлхүүр үгээр Products болон Categories хүснэгтүүдийг холбох:

> SELECT ProductID, ProductName, CategoryName
> FROM Products
> INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;

![SQL INNER JOIN](https://www.w3schools.com/sql/img_inner_join.png)

**Note:** The `INNER JOIN` keyword returns only rows with a match in both tables. Which means that if you have a product with no CategoryID, or with a CategoryID that is not present in the Categories table, that record would not be returned in the result.

**Тэмдэглэл:** INNER JOIN түлхүүр үг нь зөвхөн хоёр хүснэгтэд таарсан мөрүүдийг буцаана. Энэ нь хэрэв таны бүтээгдэхүүнд CategoryID байхгүй эсвэл Categories хүснэгтэд байхгүй CategoryID-тэй бол тэр мэдээллийн үр дүнд орохгүй гэсэн үг юм.

---

## Syntax – Дүрэм

`SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;`

`SELECT багана_нэр(нууд)
FROM хүснэгт1
INNER JOIN хүснэгт2
ON хүснэгт1.багана_нэр = хүснэгт2.багана_нэр;`

## Naming the Columns – Багануудаа нэрлэх

It is a good practice to include the table name when specifying columns in the SQL statement.

SQL өгүүлбэрт багануудыг зааж өгөхдөө хүснэгтийн нэрийг оруулах нь сайн арга юм.

### Example – Жишээ

Specify the table names:

Хүснэгтийн нэрийг тодорхойлох:

> SELECT Products.ProductID, Products.ProductName, Categories.CategoryName
> FROM Products
> INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;

The example above works without specifying table names, because none of the specified column names are present in both tables. If you try to include `CategoryID` in the `SELECT` statement, you will get an error if you do not specify the table name (because `CategoryID` is present in both tables).

> ~~Дээрх жишээ нь хүснэгтийн нэрийг тодорхойлохгүйгээр ажилладаг, учир нь тодорхойлсон баганын нэрс хоёр хүснэгтэд хоёуланд нь байхгүй. Хэрэв та SELECT өгүүлбэрт CategoryID-г оруулахыг оролдвол хүснэгтийн нэрийг тодорхойлохгүй бол алдаа гарна (учир нь CategoryID нь хоёр хүснэгтэд хоёуланд нь байдаг).~~

---

## JOIN or INNER JOIN

`JOIN` and `INNER JOIN` will return the same result.

`JOIN` болон `INNER JOIN` нь ижил үр дүнг буцаана.

`INNER` is the default join type for `JOIN`, so when you write `JOIN` the parser actually writes `INNER JOIN`.

`INNER` нь `JOIN`-ийн анхны өгөгдмөл төрөл тул та `JOIN` гэж бичихэд, үнэндээ `INNER JOIN` гэж бичсэн гэсэн үг юм.

### Example – Жишээ

JOIN is the same as INNER JOIN:

JOIN нь INNER JOIN-тэй ижил:

> SELECT Products.ProductID, Products.ProductName, Categories.CategoryName
> FROM Products
> JOIN Categories ON Products.CategoryID = Categories.CategoryID;

---

## JOIN Three Tables – Гурван хүснэгтийг холбох

The following SQL statement selects all orders with customer and shipper information:

Доорх SQL өгүүлбэр нь бүх захиалгыг харилцагч болон ачаа илгээгчийн мэдээлэлтэй сонгоно:

Here is the **Shippers** table:

Энд **Ачаа илгээгчид**хүснэгт байна:

| ShipperID | ShipperName | Phone |
| --- | --- | --- |
| 1 | Speedy Express | (503) 555-9831 |
| 2 | United Package | (503) 555-3199 |
| 3 | Federal Shipping | (503) 555-9931 |

### Example – Жишээ

> SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
> FROM ((Orders
> INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
> INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

         [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-inner-join%2F&linkname=SQL%20Inner%20Join)[https://www.addtoany.com/share](https://www.addtoany.com/share)
