ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Online shop တစ်ခုရဲ့ filter panel ကို စဉ်းစားကြည့်ပါ။ "Phone ဖြစ်ရမယ်" နဲ့ "Stock ရှိရမယ်" ဆိုတဲ့ checkbox နှစ်ခုလုံး ရွေးထားရင် နှစ်ခုလုံးနဲ့ ကိုက်တဲ့ ပစ္စည်းပဲ ပြပါတယ် — ဒါက AND ပါ။ Brand list ထဲမှာ "Samsung" ရော "Apple" ပါ ရွေးထားရင် တစ်ခုခုနဲ့ ကိုက်တာ ပြပါတယ် — ဒါက OR ပါ။ "Refurbished မပါ" ဆိုတာကတော့ NOT ပါ။
SQL မှာ WHERE clause တစ်ခုထဲ condition အများကြီး ထည့်ချင်ရင် ဒီ keyword သုံးလုံးနဲ့ ချိတ်ပါတယ်။ AND က row ကို ပိုတင်းကျပ်စေပြီး (ရလဒ် နည်းလာ)၊ OR က ပိုကျယ်စေပါတယ် (ရလဒ် များလာ)။ NOT ကတော့ condition တစ်ခုရဲ့ အဖြေကို ပြောင်းပြန်လှန်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: parentheses တစ်စုံ ပျောက်တာနဲ့ ရလဒ် မှားသွားတယ်
Sales team က "Mexico ဒါမှမဟုတ် Germany က customer တွေ ထုတ်ပေးပါ၊ ဒါပေမဲ့ Mexico City မှာ ရှိတဲ့သူတွေ မပါစေနဲ့" လို့ တောင်းပါတယ်။ စကားပြောအတိုင်း ဘယ်ကနေ ညာကို ချရေးလိုက်ရင် အောက်က query ရပါတယ်။ Error မတက်ပါဘူး၊ row လေးကြောင်းလည်း ပြန်လာပါတယ်။ ဒါပေမဲ့ Mexico City က customer နှစ်ယောက်က ရလဒ်ထဲ ပါနေဆဲပါ — ဖယ်ခိုင်းထားတာ အလုပ်မလုပ်ပါဘူး။
SELECT customer_name, city, country
FROM customers
WHERE country = 'Mexico'
OR country = 'Germany' AND NOT city = 'Mexico City';
-- Returns Ana Trujillo and Antonio Moreno (both in Mexico City!)ဘာကြောင့် Mexico City row တွေ ပါလာတာလဲ
SQL က AND ကို OR ထက် အရင် ပေါင်းပါတယ်။ ဒါကြောင့် PostgreSQL က ဒီ WHERE ကို country = 'Mexico' OR (country = 'Germany' AND NOT city = 'Mexico City') လို့ ဖတ်ပါတယ်။ "Mexico City မဟုတ်ရ" ဆိုတဲ့ စည်းကမ်းက Germany ဘက်ကိုပဲ ကပ်သွားပြီး Mexico row တွေက ပထမ condition တစ်ခုတည်းနဲ့ ဖြတ်ကျော်သွားပါတယ်။ (country = 'Mexico' OR country = 'Germany') လို့ ကွင်းခတ်ပေးမှ ရည်ရွယ်ချက်အတိုင်း ဖြစ်ပါတယ်။
Core concept: condition တစ်ခုစီက TRUE, FALSE, NULL ထဲက တစ်ခု ပြန်ပေးတယ်
WHERE clause က row တစ်ကြောင်းချင်းစီအတွက် boolean expression တစ်ခုကို တွက်ပြီး TRUE ထွက်တဲ့ row ကိုပဲ ထားပါတယ်။ AND က ဘက်နှစ်ဖက်လုံး TRUE ဖြစ်မှ TRUE ပြန်ပြီး OR က တစ်ဖက်ဖက် TRUE ဖြစ်ရင် TRUE ပြန်ပါတယ်။ NOT က TRUE ကို FALSE, FALSE ကို TRUE ပြောင်းပါတယ်။ ဒီအထိက programming language တိုင်းမှာ တွေ့ဖူးတဲ့ logic အတိုင်းပါပဲ။
ကွာခြားချက် ပထမတစ်ခုက precedence ပါ။ Parentheses မပါရင် PostgreSQL က NOT ကို အရင်ဆုံး၊ နောက် AND၊ နောက်ဆုံးမှ OR ကို တွက်ပါတယ် — သင်္ချာမှာ အမြှောက်က အပေါင်းထက် အရင်လာသလိုပါပဲ။ ဒါကြောင့် a OR b AND c ဆိုတာ a OR (b AND c) ပါ၊ (a OR b) AND c မဟုတ်ပါဘူး။ AND နဲ့ OR ရောရေးတဲ့ query တိုင်းမှာ ကိုယ်ဆိုလိုတဲ့ အုပ်စုကို ကွင်းခတ်ပေးတာက ကိုယ့်အတွက်ရော နောက် ဖတ်မယ့်သူအတွက်ပါ အလုံခြုံဆုံးပါ။
ကွာခြားချက် ဒုတိယက NULL ပါ။ SQL မှာ condition တစ်ခုက TRUE, FALSE အပြင် NULL (unknown) လည်း ဖြစ်နိုင်ပါတယ်။ country က NULL ဖြစ်နေရင် country = 'Mexico' က NULL ပြန်ပြီး NOT (NULL) ကလည်း NULL ပဲ ဖြစ်ပါတယ်။ WHERE က TRUE ကိုပဲ လက်ခံလို့ အဲဒီ row က condition ဖြစ်လည်း ပျောက်၊ ပြောင်းပြန် condition ဖြစ်လည်း ပျောက်ပါတယ်။ TRUE OR NULL က TRUE ဖြစ်ပြီး FALSE AND NULL က FALSE ဖြစ်တာကိုလည်း မှတ်ထားပါ — အဖြေကို သေချာသိနိုင်ရင် NULL က အဖြေကို မပြောင်းပါဘူး။
| a နဲ့ b | a AND b | a OR b |
|---|---|---|
| TRUE, TRUE | TRUE | TRUE |
| TRUE, FALSE | FALSE | TRUE |
| TRUE, NULL | NULL | TRUE |
| FALSE, NULL | FALSE | NULL |
| NULL, NULL | NULL | NULL |
နောက်ဆုံး သတိထားရမှာက SQL က programming language တွေလို ဘယ်ကနေ ညာ short-circuit လုပ်မယ်လို့ ကတိမပေးတာပါ။ PostgreSQL ရဲ့ planner က condition တွေကို သူ့စိတ်ကြိုက် အစဉ်နဲ့ တွက်နိုင်လို့ "ဘယ်ဘက် condition က အရင် စစ်ပေးမှာပဲ" ဆိုပြီး ညာဘက်မှာ error တက်နိုင်တဲ့ expression ကို မထားပါနဲ့။
- Row တစ်ကြောင်း ယူ
- NOT ကို အရင် တွက်
- AND နောက် OR ကို တွက်
- TRUE ထွက်မှ row ကို ထား
FALSE နဲ့ NULL နှစ်ခုလုံး row ကို ဖယ်ပါတယ်။ ကွင်းခတ်ထားရင် အထဲက အရင် တွက်ပါတယ်။
အထွေထွေ ပုံစံ
SELECT column_list
FROM table_name
WHERE (condition_a OR condition_b)
AND condition_c
AND NOT condition_d;
-- shorthand for several ORs on one column
WHERE column_name IN (value_1, value_2, value_3)OR နဲ့ ဆက်ထားတဲ့ အုပ်စုကို ကွင်းခတ်ပြီး AND နဲ့ ကျန်တဲ့ စည်းကမ်းတွေ ထပ်ချိတ်ပါတယ်။ Condition တစ်ခုချင်းစီကို line တစ်ကြောင်းစီ ခွဲရေးရင် ဖတ်ရလွယ်ပြီး comment out လုပ်ကြည့်ရလည်း လွယ်ပါတယ်။ Column တစ်ခုတည်းမှာ OR တွေ ဆက်တိုက် ဖြစ်နေရင် IN (...) က ပိုတိုပါတယ်။
Example: country နှစ်ခုထဲက customer တွေ၊ ဒါပေမဲ့ Mexico City မပါ
Example အားလုံးက အောက်က customers table ခြောက်ကြောင်းပေါ်မှာ run ပါတယ်။ Cactus Comidas ရဲ့ country က NULL (မမှတ်ရသေး) ဖြစ်နေတာကို သတိထားကြည့်ပါ — deeper section မှာ ဒီ row ပြဿနာ ပေးပါလိမ့်မယ်။ ပထမ query က OR တစ်ခုတည်း သုံးပြီး ဒုတိယ query က ကွင်းခတ်ထားတဲ့ OR အုပ်စုကို AND NOT နဲ့ ချိတ်ပါတယ်။
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);-- Customers in either country
SELECT customer_name, city, country
FROM customers
WHERE country = 'Mexico' OR country = 'Germany'
ORDER BY customer_id;
-- Either country, but not in Mexico City
SELECT customer_name, city, country
FROM customers
WHERE (country = 'Mexico' OR country = 'Germany')
AND NOT city = 'Mexico City'
ORDER BY customer_id;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- OR
WHERE country = 'Mexico' OR country = 'Germany'Condition နှစ်ခုထဲက တစ်ခုခု TRUE ဖြစ်ရင် row ကို ထားပါတယ်။ UK customer နဲ့ country NULL ဖြစ်နေတဲ့ Cactus Comidas က နှစ်ခုလုံးနဲ့ မကိုက်လို့ ပျောက်ပါတယ်။
- ORDER BY
ORDER BY customer_id;ORDER BY မပါရင် row အစဉ်ကို PostgreSQL က ကတိမပေးပါဘူး။ ရလဒ်ကို ယှဉ်ကြည့်ရလွယ်အောင် customer_id နဲ့ စီထားပါတယ် (နောက် lesson မှာ အသေးစိတ် လေ့လာပါမယ်)။
- Parentheses
WHERE (country = 'Mexico' OR country = 'Germany')ကွင်းက OR အုပ်စုကို တစ်ယူနစ်တည်း ဖြစ်စေပါတယ်။ ကွင်းမပါရင် AND က Germany condition နဲ့ပဲ အရင်ပေါင်းသွားပြီး problem section က bug ပြန်ပေါ်ပါမယ်။
- AND NOT
AND NOT city = 'Mexico City'NOT က city = 'Mexico City' ရဲ့ အဖြေကို ပြောင်းပြန်လှန်ပြီး AND က အဲဒီ စည်းကမ်းကို OR အုပ်စု တစ်ခုလုံးပေါ် သက်ရောက်စေပါတယ်။ city <> 'Mexico City' လို့ ရေးလည်း အဓိပ္ပာယ် အတူတူပါ။
customer_name | city | country
-------------------------+-------------+---------
Alfreds Futterkiste | Berlin | Germany
Ana Trujillo | Mexico City | Mexico
Antonio Moreno | Mexico City | Mexico
Blauer See Delikatessen | Mannheim | Germany
(4 rows)
customer_name | city | country
-------------------------+----------+---------
Alfreds Futterkiste | Berlin | Germany
Blauer See Delikatessen | Mannheim | Germany
(2 rows)- ကွင်းထဲက OR အုပ်စုကို တွက်
- NOT city = 'Mexico City' ကို တွက်
- ရလဒ်နှစ်ခုကို AND နဲ့ ပေါင်း
TRUE
Row is returned
FALSE သို့မဟုတ် NULL
Row is filtered out
NOT နဲ့ NULL: ပျောက်သွားတဲ့ row
"Mexico မဟုတ်တဲ့ customer အားလုံး" လို့ ရေးလိုက်ရင် Cactus Comidas ပါလာမယ်လို့ ထင်စရာပါ — သူက Mexico မဟုတ်ဘူးလေ။ တကယ်တော့ country က NULL ဖြစ်လို့ NOT country = 'Mexico' က NULL ပဲ ထွက်ပြီး row ပျောက်ပါတယ်။ ဒီလို row တွေ ပါချင်ရင် IS DISTINCT FROM ကို သုံးပါ၊ သူက NULL ကို တန်ဖိုးတစ်ခုလို နှိုင်းယှဉ်ပြီး TRUE သို့မဟုတ် FALSE ပဲ ပြန်ပါတယ်။
-- 3 rows: Cactus Comidas (country NULL) is missing
SELECT customer_name, country
FROM customers
WHERE NOT country = 'Mexico';
-- 4 rows: NULL counts as "different from Mexico"
SELECT customer_name, country
FROM customers
WHERE country IS DISTINCT FROM 'Mexico';NOT IN နဲ့ NULL
IN က OR တွေ ဆက်ထားတာနဲ့ တူပြီး NOT IN က AND တွေ ဆက်ထားတာနဲ့ တူပါတယ်။ ဒါကြောင့် list ထဲမှာ NULL တစ်ခု ပါသွားရင် x NOT IN (1, NULL) က TRUE ဘယ်တော့မှ မဖြစ်တော့ဘဲ row တစ်ကြောင်းမှ မပြန်လာပါဘူး။ Subquery ကနေ list ယူတဲ့အခါ အဖြစ်အများဆုံးပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
E-commerce search page တစ်ခုမှာ user ရွေးတဲ့ filter တိုင်းက WHERE ထဲ condition တစ်ခုစီ ဖြစ်လာပါတယ်: category တွေကြား OR (IN နဲ့)၊ price range၊ in_stock နဲ့ "discontinued မဟုတ်" တို့ကို AND နဲ့ ချိတ်ပါတယ်။ Backend code က filter တွေကို string ဆက်ပြီး ဖန်တီးတဲ့အခါ OR အုပ်စုကို ကွင်းမခတ်မိတာက အဖြစ်အများဆုံး bug ပါ — user က filter တစ်ခု ထပ်ရွေးလိုက်တာနဲ့ ရလဒ် ပိုများလာတာမျိုး ဖြစ်တတ်ပါတယ်။
Multi-tenant app တွေမှာ ဒီ bug က security ပြဿနာ ဖြစ်လာနိုင်ပါတယ်။ WHERE tenant_id = $1 AND status = 'open' OR status = 'pending' ဆိုရင် pending ဖြစ်နေတဲ့ တခြား company တွေရဲ့ record တွေပါ ပေါက်ကြားပါတယ်။ ဒါကြောင့် team တော်တော်များများက tenant/owner စစ်တဲ့ condition ကို အမြဲ သီးခြား AND တစ်ခုအဖြစ် ထားပြီး user filter တွေကို ကွင်းထဲ ထည့်ရေးကြပါတယ်။ Admin report တွေမှာလည်း "deleted မဟုတ်" ဆိုတဲ့ NOT condition က nullable column ပေါ်မှာ ဆိုရင် IS NOT TRUE နဲ့ ရေးတာက ပိုလုံခြုံပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. AND နဲ့ OR ရောရေးပြီး ကွင်းမခတ်တာ
WHERE category = 'phone' OR category = 'laptop' AND in_stock ဆိုရင် stock ရှိမရှိ စစ်တာ laptop ကိုပဲ သက်ရောက်ပြီး stock ကုန်နေတဲ့ phone တွေ အကုန် ပါလာပါတယ်။ WHERE (category = 'phone' OR category = 'laptop') AND in_stock လို့ ရေးပါ၊ ဒါမှမဟုတ် category IN ('phone', 'laptop') AND in_stock လို့ ရေးပါ။
2. NOT က NULL row တွေကို ပါစေမယ်လို့ ထင်တာ
WHERE NOT discontinued ဆိုရင် discontinued က NULL ဖြစ်နေတဲ့ (မစစ်ရသေးတဲ့) product တွေ ပျောက်ပါတယ်၊ NOT NULL က NULL ဖြစ်လို့ပါ။ NULL ကို FALSE လို သဘောထားချင်ရင် discontinued IS NOT TRUE လို့ ရေးပါ၊ text column ဆိုရင် IS DISTINCT FROM ကို သုံးပါ။
3. Column တစ်ခုတည်းကို AND နဲ့ value နှစ်ခု စစ်တာ
WHERE country = 'Mexico' AND country = 'Germany' က row တစ်ကြောင်းရဲ့ country တစ်ခုတည်းက တစ်ပြိုင်နက် တန်ဖိုးနှစ်ခု ဖြစ်လို့ မရလို့ row သုည ပြန်ပါတယ်။ စကားပြောမှာ "Mexico နဲ့ Germany က customer တွေ" လို့ ပြောပေမဲ့ SQL မှာ OR ဒါမှမဟုတ် IN လိုပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- AND နဲ့ OR ရောရေးတိုင်း ကွင်းခတ်ပါ၊ precedence ကို မှတ်မိနေရင်တောင် ဖတ်သူအတွက် ရှင်းစေပါတယ်။
- Column တစ်ခုတည်းပေါ်က OR တွေကို IN (...) နဲ့ အစားထိုးပါ။
- Nullable column ပေါ်မှာ NOT သုံးခင် NULL row တွေ ပါသင့်လား စဉ်းစားပြီး IS DISTINCT FROM / IS NOT TRUE ကို ရွေးပါ။
- Condition တစ်ခုကို line တစ်ကြောင်းစီ ရေးပြီး ရလဒ်ကို row လက်နဲ့ရေတွက်ပြီး ကြိုမှန်းထားတာနဲ့ တိုက်စစ်ပါ။
Mini challenge
Mini challenge: shop filter တစ်ခု
Product table ပေါ်မှာ user ရွေးထားတဲ့ filter ကို SQL query တစ်ခုအဖြစ် ရေးပါ: laptop သို့မဟုတ် phone ဖြစ်ရမယ်၊ stock ရှိရမယ်၊ ဈေး 1000 အောက် ဖြစ်ရမယ်၊ discontinued မဖြစ်ရ (NULL ဆိုရင် ရောင်းနေဆဲလို့ သဘောထား)။
- name, category, price column သုံးခု ပြရမယ်
- Category OR အုပ်စုကို ကွင်းခတ်ရမယ် (သို့မဟုတ် IN သုံး)
- discontinued က NULL ဖြစ်တဲ့ Phone Max ရလဒ်ထဲ ပါရမယ်
- price နဲ့ ငယ်ရာမှ ကြီးရာ စီရမယ်
CREATE TABLE products (
product_id integer PRIMARY KEY,
name text NOT NULL,
category text NOT NULL,
price numeric(8, 2) NOT NULL,
in_stock boolean NOT NULL,
discontinued boolean -- NULL = not reviewed yet
);
INSERT INTO products VALUES
(1, 'Laptop Air 13', 'laptop', 899.00, true, false),
(2, 'Laptop Pro 16', 'laptop', 1899.00, true, false),
(3, 'Phone Mini', 'phone', 499.00, false, false),
(4, 'Phone Max', 'phone', 999.00, true, NULL),
(5, 'Tablet 10', 'tablet', 349.00, true, false),
(6, 'Phone Classic', 'phone', 199.00, true, true);
-- TODO: laptops OR phones (mind the parentheses)
-- TODO: in stock and cheaper than 1000
-- TODO: not discontinued, but keep rows where discontinued IS NULL
-- TODO: cheapest first
SELECT name, category, price
FROM products;ရလဒ်: Row နှစ်ကြောင်း: Laptop Air 13 (899.00) နဲ့ Phone Max (999.00)။ Laptop Pro 16 က ဈေးကြီးလို့၊ Phone Mini က stock မရှိလို့၊ Phone Classic က discontinued ဖြစ်လို့၊ Tablet 10 က category မကိုက်လို့ ပါမလာပါ။
အရိပ်အမြွက် ကြည့်ရန်
NOT discontinued လို့ ရေးရင် Phone Max ပျောက်ပါမယ်။ boolean column တစ်ခုကို NULL ပါ FALSE လို သဘောထားချင်ရင် IS NOT TRUE ကို သုံးပါ။
အဖြေ ကြည့်ရန်
SELECT name, category, price
FROM products
WHERE (category = 'laptop' OR category = 'phone')
AND in_stock
AND price < 1000
AND discontinued IS NOT TRUE
ORDER BY price;Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- AND က ရလဒ်ကို ကျဉ်းစေပြီး OR က ကျယ်စေတယ်၊ NOT က condition တစ်ခုရဲ့ အဖြေကို လှန်တယ်။
- Precedence က NOT, AND, OR အစဉ်ပါ — AND နဲ့ OR ရောရေးတိုင်း ကွင်းခတ်ပါ။
- WHERE က TRUE row တွေပဲ ထားလို့ NULL ထွက်တဲ့ row က condition ရော ပြောင်းပြန်ရောမှာ ပျောက်တယ်။
- Column တစ်ခုတည်းပေါ်က OR တွေကို IN နဲ့ တိုစေပါ၊ NOT IN list ထဲ NULL မပါစေနဲ့။
PostgreSQL SQL Tutorial — PostgreSQL Global Development Group