ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Part 1 မှာ မှတ်တမ်းစာအုပ် လေးအုပ်ကို စီစီရီရီ ဆောက်ပြီးပါပြီ။ ဒါပေမယ့် order စာအုပ်ထဲမှာ "customer 1၊ book 102" ဆိုတဲ့ နံပါတ်တွေပဲ ရှိလို့ လူတစ်ယောက်ဖတ်ရင် နားမလည်ပါဘူး။ Join ဆိုတာ စာအုပ်နှစ်အုပ်ကို ဘေးချင်းချပြီး နံပါတ်တူတဲ့ စာကြောင်းတွေကို တွဲဖတ်တာပါ။
ဒီ Part 2 မှာ ဆိုင်ရဲ့ website လိုအပ်တဲ့ မေးခွန်းလေးခုကို ဖြေပါမယ်: "Harry ဆိုတဲ့ စာအုပ်ကို ဘယ်သူရေးလဲ"၊ "ဘာတွေ ကုန်နေလဲ"၊ "ဘယ်သူ ဘာဝယ်ခဲ့လဲ"၊ "ဘယ်စာအုပ်ကို ဘယ်သူမှ မဝယ်ရသေးလဲ" ပါ။
Problem → Why → Solution
ပြဿနာ: ပျောက်သွားတဲ့ စာအုပ်
ဆိုင်ပိုင်ရှင်က "ဘယ်သူမှ မဝယ်ရသေးတဲ့ စာအုပ်တွေကို discount ပေးချင်တယ်" လို့ ပြောပါတယ်။ Developer က join ဆိုရင် INNER JOIN လို့ အလေ့အကျင့်ဖြစ်နေလို့ အောက်က query ကို ရေးလိုက်ပါတယ်။ Result က row 0 ပါ။ Error မတက်လို့ "ဒီတစ်ပတ် စာအုပ်တိုင်း ရောင်းရတယ်" လို့ report တင်လိုက်ပါတယ်။
တကယ်တော့ Fantastic Beasts ကို တစ်ခါမှ မရောင်းရသေးပါဘူး။ INNER JOIN က order မရှိတဲ့ စာအုပ်ကို join အဆင့်မှာတင် ဖယ်ထုတ်ပစ်လို့ WHERE o.OrderID IS NULL က စစ်စရာ row တစ်ကြောင်းမှ မကျန်တော့ပါဘူး။
SELECT b.Title
FROM Books AS b
INNER JOIN Orders AS o ON b.BookID = o.BookID -- keeps only books WITH orders
WHERE o.OrderID IS NULL; -- so nothing can be NULL here
-- (0 rows), and no error to warn youဘာကြောင့် error မတက်တာလဲ
Query က syntax အရ မှန်ပြီး PostgreSQL က ခိုင်းတာကို အတိအကျ လုပ်ပေးတာပါ။ INNER JOIN ရဲ့ အဓိပ္ပာယ်က "ဘက်နှစ်ဖက်လုံးမှာ ကိုက်တဲ့ row ပဲ ထားမယ်" ဖြစ်ပါတယ်။ "မရှိတာ" ကို ရှာချင်ရင် ကိုက်တာမရှိတဲ့ row တွေကိုပါ NULL နဲ့ ထိန်းထားပေးတဲ့ LEFT JOIN လိုပါတယ်။ Join type ရွေးမှားတာက logic bug ဖြစ်လို့ result ကို ကိုယ်တိုင် စစ်မှ သိပါတယ်။
Core concept: join type က ဘယ် row တွေ ကျန်မလဲ ဆုံးဖြတ်တယ်
Join query တစ်ခု ရေးတိုင်း မေးခွန်းနှစ်ခု မေးပါ။ ပထမ: result ထဲက row တစ်ကြောင်းက ဘာကို ကိုယ်စားပြုလဲ (grain)။ Order history မှာ row တစ်ကြောင်း = order တစ်ခုပါ၊ ဒါကြောင့် FROM Orders ကနေ စပြီး customer နဲ့ book အချက်အလက်ကို ဘေးမှာ ကပ်ပါတယ်။ ဒုတိယ: ကိုက်တာ မရှိတဲ့ row တွေကို ထားမလား ဖယ်မလား။ ဒီမေးခွန်းက INNER နဲ့ LEFT ကြား ရွေးချယ်မှုပါ။
၁။ INNER JOIN က ကိုက်တာကိုပဲ ထားတယ်
Books INNER JOIN Authors မှာ author မရှိတဲ့ စာအုပ် ရှိရင် ပျောက်သွားပါမယ်။ Part 1 မှာ AuthorID ကို NOT NULL နဲ့ foreign key ထားခဲ့လို့ စာအုပ်တိုင်းမှာ author ရှိတာ သေချာပြီး INNER JOIN က ဘာမှ မဖျောက်ပါဘူး။ Constraint တွေက join ရဲ့ အပြုအမူကိုပါ ခန့်မှန်းလို့ရအောင် လုပ်ပေးတာပါ။
၂။ LEFT JOIN က ဘယ်ဘက် row အားလုံးကို ထားတယ်
Books LEFT JOIN Orders မှာ order မရှိတဲ့ စာအုပ်တွေလည်း ကျန်ပြီး o.OrderID၊ o.Quantity စတဲ့ Orders column တွေ NULL ဖြစ်ပါတယ်။ WHERE o.OrderID IS NULL က အဲဒီ row တွေကိုပဲ ရွေးလို့ "anti-join" လို့ ခေါ်ပါတယ်။ OrderID ကို စစ်ရတာက primary key ဖြစ်လို့ တကယ့် order မှာ NULL ဘယ်တော့မှ မဖြစ်နိုင်လို့ပါ။
၃။ Join တစ်ခါတိုးတိုင်း table တစ်ခုစီ ချိတ်တယ်
Order history မှာ Orders ကို Customers နဲ့ တစ်ခါ၊ Books နဲ့ တစ်ခါ join ပါတယ်။ ON condition တစ်ခုစီက foreign key တစ်ခုကို လိုက်ပါတယ်။ Foreign key column အသုံးပြုထားလို့ order တစ်ခုက customer တစ်ယောက်၊ book တစ်အုပ်နဲ့ပဲ ကိုက်ပြီး row အရေအတွက် ၄ ကြောင်းအတိုင်း ကျန်ပါတယ်။
၄။ ILIKE က search box အတွက်
PostgreSQL ရဲ့ LIKE က စာလုံးအကြီးအသေး ခွဲပါတယ်။ LIKE '%harry%' က 'Harry Potter' ကို မတွေ့ပါဘူး။ User တွေက စာလုံးအသေးနဲ့ ရိုက်တတ်လို့ ILIKE ကို သုံးပါတယ်။ ILIKE က PostgreSQL သီးသန့်ပါ။ တခြား database တွေမှာ LOWER(Title) LIKE '%harry%' လို့ ရေးပါ။ MySQL ရဲ့ default collation တွေက LIKE ကိုပဲ case မခွဲဘဲ နှိုင်းယှဉ်လေ့ ရှိပါတယ်။
| မေးခွန်း | Join | ဘာကြောင့် |
|---|---|---|
| စာအုပ်နဲ့ author နာမည် | INNER | စာအုပ်တိုင်းမှာ author ရှိတယ် |
| Order history | INNER (x2) | Order တိုင်းမှာ customer နဲ့ book ရှိတယ် |
| မဝယ်ရသေးတဲ့ စာအုပ် | LEFT + IS NULL | Order မရှိတဲ့ row ကို ထိန်းထားရမယ် |
- Row တစ်ကြောင်း = ဘာလဲ ဆုံးဖြတ်
- အဲဒီ table ကနေ FROM စ
- FK လမ်းကြောင်းအတိုင်း join
- မကိုက်တဲ့ row လိုရင် LEFT
- Row count ကို စစ်
Join ပြီးနောက် row အရေအတွက် မထင်မှတ်ဘဲ တိုးလာရင် ဒါမှမဟုတ် လျော့သွားရင် join type ဒါမှမဟုတ် ON condition ကို ပြန်စစ်ပါ။
အခြေခံ ပုံစံ
SELECT x.col, y.col, z.col
FROM main_table AS x
INNER JOIN table_y AS y ON x.y_id = y.id
INNER JOIN table_z AS z ON x.z_id = z.id
WHERE x.text_col ILIKE '%term%'
ORDER BY x.sort_col DESC;
-- rows in a with no match in b (anti-join)
SELECT a.col FROM a
LEFT JOIN b ON b.a_id = a.id
WHERE b.id IS NULL;Alias (AS x) က query ကို တိုစေပြီး column တိုင်းကို ဘယ် table က လာလဲ ရှင်းစေပါတယ်။ Anti-join မှာ IS NULL စစ်တဲ့ column က ညာဘက် table ရဲ့ primary key ဖြစ်သင့်ပါတယ်။
Bookstore query လေးခု
Query လေးခုလုံးက Part 1 ရဲ့ data အတိအကျပေါ်မှာ run ပါတယ်။ Books.Title လို table နာမည်အပြည့် ရေးမယ့်အစား b၊ a၊ c၊ o alias တွေနဲ့ တိုအောင် ရေးထားပါတယ်။ Search မှာလည်း LIKE '%Harry%' အစား ILIKE '%harry%' ကို သုံးထားလို့ user ရိုက်တဲ့ စာလုံးအကြီးအသေး ဘယ်လိုပဲဖြစ်ဖြစ် ရှာတွေ့ပါတယ်။ PostgreSQL က quote မပါတဲ့ နာမည်တွေကို စာလုံးအသေးပြောင်းလို့ output header တွေမှာ authorname၊ customername လို့ ပေါ်တာ သတိပြုပါ။
-- 1. Search box: title contains "harry" (any case), with the author
SELECT b.Title, a.AuthorName, b.Price
FROM Books AS b
INNER JOIN Authors AS a ON b.AuthorID = a.AuthorID
WHERE b.Title ILIKE '%harry%'
ORDER BY b.Price ASC;
-- 2. Out-of-stock list
SELECT Title, Stock FROM Books WHERE Stock = 0;
-- 3. Order history, newest first
SELECT c.CustomerName, b.Title, o.Quantity, o.OrderDate
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
INNER JOIN Books AS b ON o.BookID = b.BookID
ORDER BY o.OrderDate DESC;
-- 4. Books that were never ordered
SELECT b.Title
FROM Books AS b
LEFT JOIN Orders AS o ON b.BookID = o.BookID
WHERE o.OrderID IS NULL;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Case-insensitive search
WHERE b.Title ILIKE '%harry%'% က စာလုံးဘယ်နှစ်လုံးမဆို (သုည အပါအဝင်) ကို ကိုယ်စားပြုလို့ title ထဲ ဘယ်နေရာမှာ harry ပါပါ တွေ့ပါတယ်။ ILIKE ကြောင့် 'Harry' နဲ့ 'HARRY' လည်း ကိုက်ပါတယ်။
- First join of the history
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerIDOrder တစ်ခုစီဘေးမှာ customer နာမည်ကို ကပ်ပါတယ်။ Grain က order ဖြစ်နေဆဲပါ။ Su Su မှာ order နှစ်ခု ရှိလို့ result ထဲမှာ နှစ်ကြောင်း ပေါ်ပါတယ်။
- Newest first
ORDER BY o.OrderDate DESC;DESC က ရက်စွဲအသစ်ဆုံးကို အပေါ်ဆုံးမှာ ထားပါတယ်။ DESC မပါရင် default ASC ဖြစ်ပြီး အဟောင်းဆုံးက အပေါ်ရောက်ပါတယ်။
- Keep books without orders
LEFT JOIN Orders AS o ON b.BookID = o.BookIDOrder မရှိတဲ့ Fantastic Beasts ကို ဖယ်မထုတ်ဘဲ o.* column တွေကို NULL နဲ့ ဖြည့်ထားပါတယ်။
- Anti-join filter
WHERE o.OrderID IS NULL;Match မရှိတဲ့ row တွေကိုပဲ ကျန်စေပါတယ်။ = NULL လို့ ရေးရင် ဘယ်တော့မှ true မဖြစ်လို့ IS NULL ကို သုံးရပါတယ်။
-- 1. search
title | authorname | price
--------------+-------------+----------
Harry Potter | J.K Rowling | 15000.00
(1 row)
-- 2. out of stock
title | stock
------------------+-------
Fantastic Beasts | 0
(1 row)
-- 3. order history
customername | title | quantity | orderdate
--------------+--------------------+----------+------------
Su Su | Kafka on the Shore | 1 | 2026-02-01
Hla Hla | Norwegian Wood | 3 | 2026-01-12
Ko Ko | Myanmar Sarpay | 1 | 2026-01-10
Su Su | Harry Potter | 2 | 2026-01-05
(4 rows)
-- 4. never ordered
title
------------------
Fantastic Beasts
(1 row)LEFT JOIN မှာ condition ကို ON ထဲ ထားမလား WHERE ထဲ ထားမလား
"စာအုပ်တိုင်းကို ပြပြီး 2026-01-10 နောက်ပိုင်း order ရှိရင် ကပ်ပြပါ" ဆိုတဲ့ report ကို စဉ်းစားကြည့်ပါ။ ရက်စွဲ condition ကို ON ထဲမှာ ထားရင် ညာဘက် table ကို join မလုပ်ခင် filter လုပ်တာဖြစ်လို့ စာအုပ် ၅ အုပ်လုံး ကျန်ပါတယ်။ WHERE ထဲ ထားရင်တော့ join ပြီးမှ filter လုပ်လို့ NULL ရက်စွဲရှိတဲ့ row တွေ ပျောက်ပြီး LEFT JOIN က INNER JOIN လို ဖြစ်သွားပါတယ်။
-- Condition in ON: every book stays (5 rows)
SELECT b.Title, o.OrderID
FROM Books AS b
LEFT JOIN Orders AS o
ON b.BookID = o.BookID AND o.OrderDate >= '2026-01-10'
ORDER BY b.BookID;
-- Harry Potter and Fantastic Beasts show an empty OrderID
-- Condition in WHERE: unmatched books are filtered out (3 rows)
SELECT b.Title, o.OrderID
FROM Books AS b
LEFT JOIN Orders AS o ON b.BookID = o.BookID
WHERE o.OrderDate >= '2026-01-10'
ORDER BY b.BookID;NOT EXISTS ဆိုတဲ့ နောက်နည်းတစ်ခု
SELECT Title FROM Books b WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.BookID = b.BookID); ကလည်း အဖြေတူ ရပါတယ်။ PostgreSQL က နှစ်မျိုးလုံးကို anti-join plan အဖြစ် ပြောင်းလေ့ရှိပါတယ်။ NOT IN (SELECT BookID FROM Orders) ကတော့ subquery ထဲမှာ NULL တစ်ခုပါရင် row တစ်ကြောင်းမှ ပြန်မပေးလို့ ရှောင်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
E-commerce site တွေရဲ့ "My orders" page၊ admin dashboard ရဲ့ "stock ကုန်နေတာ" widget နဲ့ marketing team ရဲ့ "ဝယ်ဖူးပြီး ခြောက်လကြာ ပြန်မဝယ်တဲ့ customer" စာရင်းတွေက ဒီ lesson ထဲက pattern တွေနဲ့ အတူတူပါပဲ။ Anti-join ကို onboarding email မပို့ရသေးတဲ့ user၊ invoice မထုတ်ရသေးတဲ့ order စတာတွေ ရှာဖို့လည်း သုံးပါတယ်။
Application code ထဲမှာ ဒီ query တွေကို string ဆက်ပြီး မရေးပါဘူး။ User ရိုက်တဲ့ search term ကို parameter ($1) အဖြစ် ပို့ပြီး ILIKE '%' || $1 || '%' လို ရေးပါတယ်။ Table ကြီးလာရင် '%term%' လို ရှေ့မှာ % ပါတဲ့ search က ရိုးရိုး B-tree index ကို မသုံးနိုင်လို့ PostgreSQL ရဲ့ pg_trgm extension နဲ့ GIN index ကို သုံးလေ့ ရှိပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
LEFT JOIN လိုတဲ့နေရာမှာ INNER JOIN သုံးတာ
ပြဿနာ section မှာ မြင်ခဲ့သလို order မရှိတဲ့ စာအုပ်တွေ result ကနေ လုံးဝ ပျောက်သွားပြီး error တစ်ခုမှ မပြပါဘူး။ "မရှိတာ"၊ "မလုပ်ရသေးတာ" လို့ မေးတဲ့ မေးခွန်းတိုင်းမှာ LEFT JOIN ကို စဉ်းစားပါ။
Column ကို table နာမည် မတပ်ဘဲ ရေးတာ
Orders နဲ့ Books နှစ်ခုလုံးမှာ BookID ရှိလို့ SELECT BookID ... လို့ ရေးရင် column reference "bookid" is ambiguous error တက်ပါတယ်။ Join query တွေမှာ column တိုင်းကို b.BookID လို alias နဲ့ ရေးပါ။
LEFT JOIN ပြီးမှ ညာဘက် column ကို WHERE နဲ့ filter လုပ်တာ
WHERE o.OrderDate >= ... လို condition က NULL row တွေကို ဖယ်လို့ LEFT JOIN ရဲ့ အကျိုးကျေးဇူး ပျောက်သွားပါတယ်။ ညာဘက် table ကို filter လုပ်ချင်ရင် ON ထဲ ထည့်ပါ။
LIKE က case မခွဲဘူးထင်တာ
MySQL ကနေ ပြောင်းလာသူတွေ အဖြစ်များပါတယ်။ PostgreSQL မှာ LIKE '%harry%' က row 0 ပြန်ပေးပါတယ်။ ILIKE ဒါမှမဟုတ် LOWER() ကို သုံးပါ။
Mini challenge
ရန်ကုန် customer တွေရဲ့ order history
Yangon မှာ နေတဲ့ customer တွေရဲ့ order တွေကိုပဲ ပြတဲ့ query ရေးပါ။ Line တစ်ကြောင်းချင်းစီရဲ့ ငွေပမာဏ (Quantity × Price) ကိုလည်း ပြပါ။
- Orders ကနေ စပြီး Customers နဲ့ Books ကို join ပါ
- CustomerName၊ Title၊ Quantity၊ LineTotal၊ OrderDate column တွေ ပြပါ
- City = 'Yangon' ဖြစ်တဲ့ customer တွေကိုပဲ ထားပါ
- အသစ်ဆုံး order ကို အပေါ်မှာ ထားပါ
SELECT c.CustomerName, b.Title, o.Quantity,
-- TODO: LineTotal = quantity times the book's price
o.OrderDate
FROM Orders AS o
-- TODO: join Customers (alias c) on the customer foreign key
-- TODO: join Books (alias b) on the book foreign key
-- TODO: keep only Yangon customers
-- TODO: newest order first
;ရလဒ်: Row ၃ ကြောင်း: Su Su - Kafka on the Shore - 1 - 13000.00 - 2026-02-01၊ Hla Hla - Norwegian Wood - 3 - 36000.00 - 2026-01-12၊ Su Su - Harry Potter - 2 - 30000.00 - 2026-01-05။ Mandalay က Ko Ko မပါပါ။
အရိပ်အမြွက် ကြည့်ရန်
City က Customers ထဲမှာ ရှိလို့ WHERE c.City = 'Yangon' လို့ ရေးပါ။ String ကို single quote နဲ့ ရေးရပါတယ်၊ double quote ဆိုရင် PostgreSQL က column နာမည်လို့ ယူဆပါတယ်။
အဖြေ ကြည့်ရန်
SELECT c.CustomerName, b.Title, o.Quantity,
o.Quantity * b.Price AS LineTotal,
o.OrderDate
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
INNER JOIN Books AS b ON o.BookID = b.BookID
WHERE c.City = 'Yangon'
ORDER BY o.OrderDate DESC;Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Join မရေးခင် result row တစ်ကြောင်းက ဘာကို ကိုယ်စားပြုလဲ ဆုံးဖြတ်ပါ
- INNER JOIN က ကိုက်တာကိုပဲ ထားပြီး LEFT JOIN က ဘယ်ဘက် row အားလုံးကို ထားပါတယ်
- "မရှိတာ" ရှာဖို့ LEFT JOIN + WHERE right_pk IS NULL (ဒါမှမဟုတ် NOT EXISTS) သုံးပါ
- LEFT JOIN မှာ ညာဘက် table filter ကို ON ထဲ ထည့်ပါ
- PostgreSQL LIKE က case ခွဲလို့ search box အတွက် ILIKE သုံးပါ
PostgreSQL SQL Tutorial — PostgreSQL Global Development Group