---
title: SQL Select Distinct
description: "## The SQL SELECT DISTINCT Statement – SQL SELECT DISTINCT хэллэг The `SELECT DISTINCT` statement is used to return only distinct (different) values. `SELECT DISTINCT` хэллэг нь  зөвхөн ялгаатай (өөр) утгуудыг буцаахад ашиглагддаг. ### Example – Жишээ Select all the different countries…"
url: "https://apprentice.mn/docs/sql-select-distinct/"
updated: "2024-07-16"
category: SQL
---

# SQL Select Distinct

## The SQL SELECT DISTINCT Statement – SQL SELECT DISTINCT хэллэг

The `SELECT DISTINCT` statement is used to return only distinct (different) values.

`SELECT DISTINCT` хэллэг нь  зөвхөн ялгаатай (өөр) утгуудыг буцаахад ашиглагддаг.

### Example – Жишээ

Select all the different countries from the “Customers” table:

“Хэрэглэгчид” хүснэгтээс бүх өөр улсуудыг харуулах:

> SELECT DISTINCT Country FROM Customers;

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

Хүснэгтийн доторхи багана ихэвчлэн олон давхардсан утгуудыг агуулдаг; зарим дохиолдолд зөвхөн өөр өөр (ялгаатай) утгуудын жагсаалт харахыг хүсэх үед ашиглана.

---

## Syntax – Дүрэм

`SELECT DISTINCT column1, column2, ...FROM table_name;`

---

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

Below is a selection from the **Customers** table used in the examples:

Доорхи **Хэрэглэгчид** хүснэгтийг жишээ болгон ашигласан:

| 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 |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |

---

## SELECT Example Without DISTINCT – DISTINCT хэрэглээгүй SELECT жишээ

If you omit the `DISTINCT` keyword, the SQL statement returns the “Country” value from all the records of the “Customers” table:

Хэрэв та DISTINCT түлхүүр үгийг хасвал, SQL хэллэг нь “Хэрэглэгчид(Customers)” хүснэгтийн бүх “Улс(Country)” гэсэн мэдээллүүдийг утгыг харуулна:

### Example – Жишээ

> SELECT Country FROM Customers;

## Count Distinct – Ялгаатай тоолох

By using the `DISTINCT` keyword in a function called `COUNT`, we can return the number of different countries.

`COUNT` функцэд `DISTINCT` түлхүүр үгийг ашигласнаар, бид өөр өөр хэдэн улс байгааг тоогоор харах боломжтой.

### Example – Жишээ

> SELECT COUNT(DISTINCT Country) FROM Customers;

**Note:**The COUNT(DISTINCT *column_name*) is not supported in Microsoft Access databases.

**Тэмдэглэл:**Microsoft Access мэдээллийн сангуудад COUNT(DISTINCT *column_name*) дэмждэггүй.

Here is a workaround for MS Access:

MS Access-д зориулсан шийдэл нь:

### Example – Жишээ

> SELECT Count(*) AS DistinctCountries
> FROM (SELECT DISTINCT Country FROM Customers);

You will learn about the COUNT function later in this tutorial.

Илүү их мэдээлэл COUNT функц зааварчилгаа хичээл дээрээс үзнэ үү.

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