Thuta Learning
SQL
BasicData & Databasesအခြေခံ

DISTINCT

SQLသင်ခန်းစာ 1020 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • SELECT DISTINCT က SELECT list ထဲက column အားလုံး ပေါင်းထားတဲ့ combination ကို စစ်တယ်ဆိုတာ ရှင်းပြနိုင်ဖို့
  • DISTINCT က NULL တွေကို တစ်ခုတည်းအဖြစ် စုပေးပုံ ကြိုတွက်နိုင်ဖို့
  • DISTINCT ရလဒ်ကို ORDER BY နဲ့ စီပြီး "ORDER BY expressions must appear in select list" error ကို နားလည်နိုင်ဖို့
  • COUNT(DISTINCT col) နဲ့ DISTINCT ON (...) ကို ဘယ်အချိန် သုံးရမလဲ ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Customer ရာချီ ရှိတဲ့ shop တစ်ခုမှာ "ဘယ်နိုင်ငံတွေကို ပို့ပေးဖူးလဲ" လို့ မေးရင် customer တိုင်းရဲ့ country ကို ထုတ်လိုက်တာနဲ့ Germany က ဆယ်ခါ၊ Mexico က ရှစ်ခါ ထပ်ပေါ်နေပါမယ်။ SELECT DISTINCT က ထပ်နေတဲ့ row တွေကို တစ်ကြောင်းတည်းအဖြစ် ချုံ့ပေးပြီး မတူတဲ့ တန်ဖိုးတွေကိုပဲ ပြန်ပေးပါတယ်။

Filter dropdown ထဲ ထည့်မယ့် country list၊ blog ရဲ့ tag list၊ product category list လို "ရှိသမျှ အမျိုးအစား" မေးခွန်းတွေမှာ DISTINCT ကို သုံးပါတယ်။ Table ထဲက data ကိုတော့ ဘာမှ မပြောင်းပါဘူး၊ ပြန်ပေးတဲ့ ရလဒ်ကိုပဲ ချုံ့တာပါ။

Problem → Why → Solution

ပြဿနာ: dropdown ထဲမှာ နိုင်ငံတွေ ထပ်နေတယ်

Developer တစ်ယောက်က country filter dropdown အတွက် country list ကို ထုတ်ပါတယ်။ ပထမ query က customer တစ်ယောက်ကို row တစ်ကြောင်း ပြန်လို့ dropdown ထဲ Germany နှစ်ခါ၊ Mexico နှစ်ခါ ပေါ်ပါတယ်။ DISTINCT ထည့်လိုက်တော့ ထပ်တာ ပျောက်သွားပေမဲ့ ဒုတိယ query မှာ city ပါ ထည့်လိုက်တော့ Germany ပြန်ထပ်လာပါတယ် — DISTINCT ပျက်သွားတာ မဟုတ်ပါဘူး။

dropdown.sql
SELECT country FROM customers;            -- Germany, Mexico, Mexico, ... repeated
SELECT DISTINCT country FROM customers;   -- each country once
SELECT DISTINCT country, city             -- Germany twice again:
FROM customers;                           -- (Germany, Berlin), (Germany, Mannheim)

ဘာကြောင့် Germany ပြန်ထပ်လာတာလဲ

DISTINCT က column တစ်ခုချင်းစီကို မစစ်ပါဘူး၊ SELECT list ထဲက column အားလုံး ပေါင်းထားတဲ့ row တစ်ကြောင်းလုံးကို စစ်ပါတယ်။ (Germany, Berlin) နဲ့ (Germany, Mannheim) က row နှစ်ကြောင်း မတူလို့ နှစ်ခုလုံး ကျန်ပါတယ်။ Dropdown အတွက် country တစ်ခုတည်းပဲ လိုရင် SELECT list ထဲမှာ country တစ်ခုတည်းပဲ ထားရပါမယ်။

Core concept: DISTINCT က row အပြည့်ကို နှိုင်းယှဉ်တယ်

PostgreSQL က WHERE နဲ့ filter လုပ်ပြီး SELECT list ကို တွက်ပြီးမှ DISTINCT ကို သုံးပါတယ်။ Row နှစ်ကြောင်းမှာ column တိုင်းရဲ့ တန်ဖိုးတွေ တူမှ duplicate လို့ သတ်မှတ်ပြီး တစ်ကြောင်းပဲ ချန်ပါတယ်။ ဒါကြောင့် column တစ်ခု ထပ်ထည့်တိုင်း "ထပ်တယ်" ဆိုတဲ့ အဓိပ္ပာယ် ပြောင်းသွားပါတယ်: country တစ်ခုတည်းဆိုရင် နိုင်ငံ list၊ country နဲ့ city ဆိုရင် (နိုင်ငံ, မြို့) အတွဲ list ပါ။

NULL ကို ကိုင်တွယ်ပုံက WHERE နဲ့ မတူပါဘူး။ WHERE မှာ NULL = NULL က TRUE မဖြစ်ပေမဲ့ DISTINCT ကတော့ NULL တွေကို အချင်းချင်း "မကွဲပြား" (not distinct) လို့ သဘောထားလို့ NULL row တွေ ဘယ်လောက်ပဲ ရှိရှိ ရလဒ်မှာ NULL row တစ်ကြောင်းပဲ ကျန်ပါတယ်။ psql မှာ NULL ကို အလွတ်အဖြစ် ပြလို့ ရလဒ်ထဲ line အလွတ်တစ်ကြောင်း တွေ့ရင် အဲဒါ NULL ပါ။

DISTINCT က အစဉ်ကို ကတိမပေးပါဘူး။ PostgreSQL က duplicate ဖယ်ဖို့ sort ဒါမှမဟုတ် hash table ကို သုံးနိုင်ပြီး hash သုံးရင် ရလဒ်က ဘယ်လို အစဉ်နဲ့မဆို ထွက်လာနိုင်ပါတယ်။ ဒါကြောင့် ORDER BY ကို ကိုယ်တိုင် ထည့်ရပါမယ်။ SELECT DISTINCT နဲ့ တွဲရင် ORDER BY ထဲက expression တိုင်း SELECT list ထဲမှာ ပါရပါမယ်၊ မပါရင် "for SELECT DISTINCT, ORDER BY expressions must appear in select list" error တက်ပါတယ် — ပြင်ဆင်ထားတဲ့ row တစ်ကြောင်းမှာ city တန်ဖိုး အများကြီး ရှိနိုင်လို့ ဘယ် city နဲ့ စီရမလဲ မသိလို့ပါ။

DISTINCT က အလကား မဟုတ်ပါဘူး: row အားလုံးကို sort ဒါမှမဟုတ် hash လုပ်ရလို့ table ကြီးရင် ကုန်ကျစရိတ် ရှိပါတယ်။ JOIN မှားလို့ row တွေ ပွားလာတာကို DISTINCT နဲ့ ဖုံးတာက ပြဿနာရဲ့ အရင်းအမြစ်ကို ဖျောက်ထားတာပါ။

SELECT DISTINCT country က SELECT country ... GROUP BY country နဲ့ row တူတူ ပြန်ပေးပြီး PostgreSQL က နှစ်ခုလုံးကို plan တူတူနဲ့ run လေ့ ရှိပါတယ်။ အုပ်စုတစ်ခုစီအတွက် count ဒါမှမဟုတ် sum ပါ လိုလာရင် GROUP BY ကို သုံးပါ၊ နောက် lesson တစ်ခုမှာ လေ့လာပါမယ်။

မှတ်ထားရမယ့် ပုံစံ: row ကို key တစ်ခုလို ကြည့်
  1. WHERE နဲ့ filter
  2. Select column တွေကို key တစ်ခု ပေါင်း
  3. Key တူတဲ့ row ကို တစ်ခု ချန်
  4. ORDER BY နဲ့ စီ

Key ထဲမှာ NULL ပါရင်လည်း NULL အချင်းချင်း တူတယ်လို့ သဘောထားပါတယ်။

အထွေထွေ ပုံစံ

sql
SELECT DISTINCT column_a, column_b
FROM table_name
WHERE condition
ORDER BY column_a, column_b;

SELECT COUNT(DISTINCT column_a) FROM table_name;

-- PostgreSQL only: one row per column_a
SELECT DISTINCT ON (column_a) column_a, column_b
FROM table_name
ORDER BY column_a, sort_key DESC;

DISTINCT က SELECT keyword နောက်မှာ ချက်ချင်း လာပြီး column list တစ်ခုလုံးကို သက်ရောက်ပါတယ်။ COUNT(DISTINCT ...) က မတူတဲ့ တန်ဖိုး ဘယ်နှခု ရှိလဲ ရေတွက်ပါတယ်။ DISTINCT ON က PostgreSQL ရဲ့ extension ဖြစ်ပြီး အုပ်စုတစ်ခုစီက ORDER BY အရ ပထမ row ကို ယူပါတယ်။

Example: နိုင်ငံ list နဲ့ (နိုင်ငံ, မြို့) list

Customer ခုနစ်ယောက်မှာ Germany နှစ်ယောက်၊ Mexico နှစ်ယောက် (မြို့တူ)၊ UK နှစ်ယောက် (London တူ) ရှိပြီး Cactus Comidas ရဲ့ country က NULL ပါ။ Query နှစ်ခုကို ယှဉ်ကြည့်ရင် DISTINCT ရဲ့ "duplicate" အဓိပ္ပာယ်က column list နဲ့ ပြောင်းသွားတာ မြင်ရပါမယ်။

CREATE TABLE customers (
  customer_id   integer PRIMARY KEY,
  customer_name text NOT NULL,
  city          text,
  country       text
);
INSERT INTO customers VALUES
  (1, 'Alfreds Futterkiste',     'Berlin',       'Germany'),
  (2, 'Ana Trujillo',            'Mexico City',  'Mexico'),
  (3, 'Antonio Moreno',          'Mexico City',  'Mexico'),
  (4, 'Around the Horn',         'London',       'UK'),
  (5, 'Blauer See Delikatessen', 'Mannheim',     'Germany'),
  (6, 'Cactus Comidas',          'Buenos Aires', NULL),
  (7, 'Consolidated Holdings',   'London',       'UK');
distinct.sql
SELECT DISTINCT country
FROM customers
ORDER BY country;

SELECT DISTINCT country, city
FROM customers
ORDER BY country, city;

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. One columnSELECT DISTINCT country

    Row ခုနစ်ကြောင်းကနေ country မတူတာ လေးခု ကျန်ပါတယ်: Germany, Mexico, UK နဲ့ NULL တစ်ခု။ အလွတ် line က Cactus Comidas ရဲ့ NULL country ပါ။

  2. ORDER BYORDER BY country;

    DISTINCT တစ်ခုတည်းက အစဉ် ကတိမပေးလို့ ORDER BY ထည့်ထားပါတယ်။ PostgreSQL မှာ NULL က ASC မှာ နောက်ဆုံး ရောက်လို့ အလွတ် line က အောက်ဆုံးမှာ ပါ။

  3. Two columnsSELECT DISTINCT country, city

    အခု (country, city) အတွဲကို စစ်လို့ Germany က Berlin နဲ့ Mannheim အတွဲနှစ်ခု ဖြစ်ပြီး Mexico City နဲ့ London ကတော့ တစ်ခါစီပဲ ပေါ်ပါတယ်။

  4. Sort keysORDER BY country, city;

    ORDER BY ထဲက column နှစ်ခုလုံး SELECT list ထဲ ပါလို့ အလုပ်လုပ်ပါတယ်။ customer_name နဲ့ စီဖို့ ကြိုးစားရင် error တက်ပါမယ်။

You should see
 country
---------
 Germany
 Mexico
 UK

(4 rows)

 country |     city
---------+--------------
 Germany | Berlin
 Germany | Mannheim
 Mexico  | Mexico City
 UK      | London
         | Buenos Aires
(5 rows)

COUNT(DISTINCT) နဲ့ DISTINCT ON

"နိုင်ငံ ဘယ်နှခု ရှိလဲ" ဆိုတာ list မလိုဘဲ အရေအတွက်ပဲ လိုရင် COUNT(DISTINCT country) ကို သုံးပါ။ Aggregate function တွေက NULL ကို ကျော်လို့ ဒီမှာ ရလဒ်က 4 မဟုတ်ဘဲ 3 ပါ — SELECT DISTINCT က NULL row ကို ပြပေမဲ့ COUNT(DISTINCT) က မရေတွက်ပါဘူး။

count-distinct.sql
SELECT COUNT(*)                AS all_rows,      -- 7
       COUNT(country)          AS with_country,  -- 6
       COUNT(DISTINCT country) AS countries      -- 3
FROM customers;

DISTINCT ON (column) က PostgreSQL မှာပဲ ရှိပါတယ်။ Column တန်ဖိုး တစ်ခုစီအတွက် row တစ်ကြောင်း ချန်ပြီး ORDER BY အရ ပထမ ရောက်တဲ့ row ကို ယူပါတယ် — "customer တစ်ယောက်ချင်းစီရဲ့ နောက်ဆုံး order" လို မေးခွန်းအတွက် အတိုဆုံး နည်းပါ။ ORDER BY ရဲ့ ပထမ expression တွေက DISTINCT ON ထဲက expression တွေနဲ့ တူရပါမယ်။ MySQL နဲ့ SQL Server မှာ မရှိလို့ အဲဒီမှာ window function (ROW_NUMBER) နဲ့ ရေးရပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Admin dashboard တွေမှာ filter option တွေကို data ထဲကနေ တိုက်ရိုက် ထုတ်ကြပါတယ်: SELECT DISTINCT status FROM orders ORDER BY status က order status dropdown ကို ဖြည့်ပေးပြီး COUNT(DISTINCT customer_id) က "ဒီလ ဝယ်ယူတဲ့ customer ဘယ်နှယောက်" ဆိုတဲ့ KPI ကို တွက်ပေးပါတယ်။ Order row တွေကို COUNT(*) နဲ့ ရေတွက်ရင် order နှစ်ခု မှာတဲ့ customer က နှစ်ယောက်လို ဖြစ်သွားလို့ DISTINCT က ဒီနေရာမှာ မရှိမဖြစ်ပါ။

Table ကြီးလာတဲ့အခါ dropdown တစ်ခု ဖွင့်တိုင်း order သန်းချီကို DISTINCT လုပ်တာ နှေးလာပါတယ်။ အဲဒီအချိန်မှာ team တွေက status တွေကို သီးခြား lookup table (order_statuses) ထဲ ထားပြီး အဲဒီ table သေးသေးလေးကနေ ဖတ်ကြပါတယ်။ "Customer တစ်ယောက်ချင်းစီရဲ့ နောက်ဆုံး login" လို report တွေမှာတော့ PostgreSQL project တွေက DISTINCT ON ကို (customer_id, created_at DESC) index နဲ့ တွဲသုံးကြပြီး customer တစ်ယောက်ချင်းစီရဲ့ အသစ်ဆုံး row ကို index ကနေ တိုက်ရိုက် ဖတ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. DISTINCT က ပထမ column တစ်ခုတည်းကိုပဲ သက်ရောက်တယ်လို့ ထင်တာ

SELECT DISTINCT country, customer_name ဆိုရင် customer နာမည်တိုင်း မတူလို့ row အားလုံး ပြန်ထွက်ပါတယ်။ DISTINCT က function မဟုတ်လို့ DISTINCT(country), customer_name လို့ ကွင်းခတ်ရေးလည်း column list တစ်ခုလုံးကိုပဲ သက်ရောက်ပါတယ်။ Country တစ်ခုချင်းစီအတွက် customer တစ်ယောက်ပဲ လိုရင် DISTINCT ON (country) ကို သုံးပါ။

2. JOIN မှားလို့ ပွားလာတဲ့ row တွေကို DISTINCT နဲ့ ဖုံးတာ

Query က row ထပ်ပြန်လာလို့ DISTINCT ထည့်လိုက်ရင် ရလဒ်က မှန်သလို ဖြစ်သွားပေမဲ့ join condition ပျောက်နေတာ ဒါမှမဟုတ် one-to-many relationship ကို နားလည်မှုလွဲတာ ကျန်နေဆဲပါ။ SUM ဒါမှမဟုတ် COUNT ထပ်ထည့်တဲ့အခါ ပွားနေတဲ့ row တွေကြောင့် ကိန်းဂဏန်း မှားပါလိမ့်မယ်။ Row ဘာကြောင့် ထပ်လဲ အရင် ရှာပါ။

3. DISTINCT ရလဒ်ကို စီပြီးသားလို့ ယူဆတာ

Sort နဲ့ duplicate ဖယ်ခဲ့တဲ့ အချိန်တွေမှာ ရလဒ်က စီပြီးသားလို ပေါ်တတ်ပေမဲ့ planner က hash ကို ပြောင်းသုံးတာနဲ့ အစဉ်ပျက်ပါတယ်။ Row အနည်းငယ် update လုပ်ပြီး ORDER BY မပါတဲ့ DISTINCT ကို ပြန် run ရင် UK, Germany, Mexico လို ရှုပ်နေတဲ့ အစဉ်မျိုး မြင်ရနိုင်ပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • DISTINCT သုံးတိုင်း ORDER BY ကို တွဲရေးပါ၊ အထူးသဖြင့် dropdown ဒါမှမဟုတ် report ထဲမှာ user မြင်ရမယ့် list တွေမှာပါ။
  • SELECT list ထဲမှာ "ထပ်လား" ဆုံးဖြတ်ချင်တဲ့ column တွေကိုပဲ ထားပါ။
  • Query က row ထပ်ပြန်လာရင် DISTINCT မထည့်ခင် JOIN နဲ့ WHERE ကို အရင် စစ်ပါ။
  • အရေအတွက်ပဲ လိုရင် list ကို app ထဲ ရေတွက်မယ့်အစား COUNT(DISTINCT ...) ကို database မှာ တွက်ပါ။

Mini challenge

Mini challenge: order history ထဲက မေးခွန်းနှစ်ခု

orders table ပေါ်မှာ query နှစ်ခု ရေးပါ။ ပထမက တစ်ခါမှ အမှာခံဖူးတဲ့ product list (တစ်ခါစီ၊ အက္ခရာစဉ်)၊ ဒုတိယက customer တစ်ယောက်ချင်းစီရဲ့ နောက်ဆုံး order (product နဲ့ အချိန်) ပါ။

  • ပထမ query က product သုံးခုပဲ ပြန်ရမယ်
  • ဒုတိယ query က customer တစ်ယောက်ကို row တစ်ကြောင်း ပြန်ရမယ်
  • ဒုတိယ query မှာ DISTINCT ON နဲ့ ORDER BY ကို ကိုက်အောင် ရေးရမယ်
order-history.sql
CREATE TABLE orders (
  order_id   integer PRIMARY KEY,
  customer   text NOT NULL,
  product    text NOT NULL,
  ordered_at timestamp NOT NULL
);
INSERT INTO orders VALUES
  (1, 'mya',  'Keyboard', '2026-04-01 09:00'),
  (2, 'kyaw', 'Mouse',    '2026-04-01 11:30'),
  (3, 'mya',  'Monitor',  '2026-04-03 14:00'),
  (4, 'hla',  'Mouse',    '2026-04-02 08:15'),
  (5, 'kyaw', 'Keyboard', '2026-04-05 16:45'),
  (6, 'mya',  'Mouse',    '2026-04-02 10:00');

-- TODO 1: each product once, A to Z
SELECT product
FROM orders;

-- TODO 2: one row per customer: their newest order
-- (customer, product, ordered_at)

ရလဒ်: Query 1: Keyboard, Monitor, Mouse။ Query 2: hla — Mouse (2026-04-02 08:15)၊ kyaw — Keyboard (2026-04-05 16:45)၊ mya — Monitor (2026-04-03 14:00)။

အရိပ်အမြွက် ကြည့်ရန်

DISTINCT ON (customer) သုံးရင် ORDER BY က customer နဲ့ စရမယ်၊ ပြီးမှ ordered_at DESC ထည့်ရင် customer တစ်ယောက်ချင်းစီရဲ့ အသစ်ဆုံး row က ပထမ ရောက်ပါတယ်။

အဖြေ ကြည့်ရန်
order-history.sql
SELECT DISTINCT product
FROM orders
ORDER BY product;

SELECT DISTINCT ON (customer) customer, product, ordered_at
FROM orders
ORDER BY customer, ordered_at DESC;

Quick Quiz 1 / 3

Row တွေက (Germany, Berlin), (Germany, Berlin), (Germany, Mannheim) ဖြစ်ရင် SELECT DISTINCT country, city က row ဘယ်နှကြောင်း ပြန်မလဲ။

Quick Quiz 2 / 3

country column မှာ Germany, NULL, NULL, UK ရှိပါတယ်။ COUNT(DISTINCT country) က ဘာ ပြန်မလဲ။

Quick Quiz 3 / 3

SELECT DISTINCT country FROM customers ORDER BY city; ကို run ရင် ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • SELECT DISTINCT က SELECT list ထဲက column အားလုံးရဲ့ combination ကို စစ်တယ်။
  • DISTINCT မှာ NULL တွေ တစ်ခုတည်း ဖြစ်သွားပေမဲ့ COUNT(DISTINCT) က NULL ကို မရေတွက်ဘူး။
  • DISTINCT က အစဉ် မပေးလို့ ORDER BY ထည့်ပါ၊ ORDER BY column တွေ SELECT list ထဲ ပါရမယ်။
  • DISTINCT ON က PostgreSQL ရဲ့ "အုပ်စုတစ်ခုစီက ပထမ row" tool ပါ။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

DISTINCT | Thuta Learning