ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Part 2 က order တစ်ခုချင်းစီကို အသေးစိတ် ပြတဲ့ စာရင်းတွေပါ။ ဆိုင်ပိုင်ရှင်ကတော့ order ၄၀၀ ကို တစ်ကြောင်းချင်း မဖတ်ချင်ပါဘူး။ "Customer တစ်ယောက်ချင်းစီ စုစုပေါင်း ဘယ်လောက်သုံးလဲ"၊ "ဘယ်စာရေးဆရာမှာ စာအုပ်အများဆုံး ရှိလဲ" ဆိုတဲ့ အနှစ်ချုပ်ကို လိုချင်တာပါ။
GROUP BY က row တွေကို ဗီရိုအံဆွဲတွေထဲ ခွဲထည့်သလိုပါ။ Customer တစ်ယောက်ကို အံဆွဲတစ်ခု၊ ပြီးရင် အံဆွဲတစ်ခုစီထဲက ငွေတွေကို SUM နဲ့ ပေါင်းပါတယ်။ ဒီ Part 3 မှာ report ထုတ်တာအပြင် stock ပြင်တာ၊ data ဟောင်း ရှင်းတာလို ပြုပြင်ထိန်းသိမ်းမှု အလုပ်တွေကိုပါ လုပ်ပါမယ်။
Problem → Why → Solution
ပြဿနာ: WHERE မပါတဲ့ UPDATE တစ်ကြောင်း
Fantastic Beasts ကို ၁၅ အုပ် ပြန်ဝင်လာလို့ stock ပြင်ဖို့ developer က query ကို အမြန်ရိုက်ပြီး WHERE ပါတဲ့ ဒုတိယစာကြောင်းကို မရွေးမိဘဲ run လိုက်ပါတယ်။ PostgreSQL က UPDATE 5 လို့ ပြန်ပြောပါတယ်: စာအုပ် ငါးအုပ်လုံးရဲ့ stock က 15 ဖြစ်သွားပါပြီ။ Myanmar Sarpay ရဲ့ stock 20 ဆိုတဲ့ မှတ်တမ်းမှန် ပျောက်သွားပါပြီ။
DELETE မှာလည်း အတူတူပါ။ DELETE FROM Orders; တစ်ကြောင်းတည်းက sales history အားလုံးကို ဖျက်ပစ်ပြီး Part 3 ရဲ့ report တိုင်း သုည ဖြစ်သွားပါမယ်။ Autocommit mode မှာ statement တစ်ခု ပြီးတာနဲ့ အပြီးတိုင် ဖြစ်သွားလို့ undo ခလုတ် မရှိပါဘူး။
UPDATE Books SET Stock = 15
-- WHERE Title = 'Fantastic Beasts'; <- this line was never run
;
-- UPDATE 5 (every book now has Stock = 15)
DELETE FROM Orders; -- meant: WHERE OrderDate < '2026-01-01'
-- DELETE 4 (all sales history is gone)ဘာကြောင့် ဒီလောက် အန္တရာယ်များလဲ
SQL မှာ WHERE မပါတဲ့ UPDATE နဲ့ DELETE က "row အားလုံး" လို့ အဓိပ္ပာယ်ရပြီး syntax မှန်လို့ PostgreSQL က သတိပေးချက် မပြပါဘူး။ ကာကွယ်နည်းက အလေ့အကျင့်ပါ: SELECT နဲ့ condition ကို အရင်စမ်း၊ primary key နဲ့ ရွေး၊ transaction ထဲမှာ run ပြီး affected row count ကို စစ်ပါ။
Core concept: group လုပ်၊ filter လုပ်၊ လုံခြုံစွာ ပြင်
Report query တစ်ခုကို PostgreSQL က အဆင့်လိုက် လုပ်ပါတယ်။ FROM နဲ့ JOIN က row တွေကို စုပေး၊ WHERE က row တစ်ကြောင်းချင်းကို filter လုပ်၊ GROUP BY က row တွေကို group အဖြစ် ခွဲ၊ COUNT/SUM က group တစ်ခုစီကို တန်ဖိုးတစ်ခု တွက်ပြီး HAVING က group တွေကို filter လုပ်ပါတယ်။ နောက်ဆုံးမှ SELECT က column တွေ ရွေးပြီး ORDER BY က စီပါတယ်။ ဒီအစီအစဉ်ကို သိရင် condition တစ်ခုကို ဘယ်နေရာ ထားရမလဲ ချက်ချင်း သိပါတယ်။
Report query pipeline
---------------------
FROM + JOIN -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY
(all rows) (rows) (drawers) (drawers) (columns) (sort)
Stock > 0 AuthorID COUNT >= 2 SUM(...) DESC၁။ ID နဲ့ group လုပ်ပါ
Customer နှစ်ယောက် နာမည်တူနိုင်ပါတယ်။ GROUP BY c.CustomerName တစ်ခုတည်းဆိုရင် Su Su နှစ်ယောက်ရဲ့ ငွေတွေ ပေါင်းသွားပါမယ်။ GROUP BY c.CustomerID, c.CustomerName လို့ ရေးရင် ID ကို အခြေခံပြီး ခွဲတဲ့အပြင် နာမည်ကိုလည်း SELECT မှာ ပြလို့ ရပါတယ်။
၂။ WHERE က row ကို၊ HAVING က group ကို
"စာအုပ် ၂ အုပ်နဲ့အထက် ရေးထားတဲ့ author" ဆိုတာ group ရဲ့ COUNT ပေါ်မှာ မူတည်လို့ HAVING COUNT(b.BookID) >= 2 ဖြစ်ရပါမယ်။ WHERE COUNT(...) လို့ ရေးရင် aggregate functions are not allowed in WHERE error တက်ပါတယ်။ "Stock ရှိတဲ့ စာအုပ်တွေကိုပဲ ရေတွက်" ဆိုရင်တော့ row condition ဖြစ်လို့ WHERE ထဲ ထည့်ရပါတယ်။
၃။ ပြင်တဲ့ statement ကို SELECT နဲ့ အရင်စမ်းပါ
UPDATE ဒါမှမဟုတ် DELETE မရေးခင် WHERE condition တူတူနဲ့ SELECT ကို run ပြီး ဘယ် row တွေ ထိမလဲ ကြည့်ပါ။ Title က unique မဖြစ်နိုင်လို့ BookID = 105 လို primary key နဲ့ ရွေးတာ ပိုလုံခြုံပါတယ်။ ပြီးရင် PostgreSQL ပြန်ပြောတဲ့ UPDATE 1 လို count ကို preview နဲ့ နှိုင်းယှဉ်ပါ။
၄။ Query ပုံစံနဲ့ ကိုက်တဲ့ index ထည့်ပါ
PostgreSQL က foreign key column တွေကို index အလိုလို မဆောက်ပေးလို့ customer တစ်ယောက်ရဲ့ order တွေရှာတဲ့ query အတွက် Orders(CustomerID) index က အသုံးဝင်ပါတယ်။ Books(Title) B-tree index က Title = '...' နဲ့ ORDER BY Title ကို ကူညီပေမယ့် Part 2 ရဲ့ ILIKE '%harry%' လို ရှေ့မှာ % ပါတဲ့ search ကိုတော့ မကူညီနိုင်ပါဘူး။
- Join နဲ့ row တွေ စု
- WHERE နဲ့ row ကို filter
- ID အလိုက် အံဆွဲထဲ ခွဲ
- အံဆွဲတစ်ခုစီကို တွက်ပြီး HAVING နဲ့ ရွေး
Data ပြင်တဲ့အခါ: SELECT နဲ့ ကြည့်၊ primary key နဲ့ ရွေး၊ ပြီးမှ ပြင်ပါ။
အခြေခံ ပုံစံ
SELECT g.name, COUNT(c.id) AS how_many, SUM(c.amount) AS total
FROM group_table AS g
INNER JOIN child_table AS c ON c.group_id = g.id
WHERE row_condition
GROUP BY g.id, g.name
HAVING group_condition
ORDER BY total DESC;
SELECT * FROM table_name WHERE id = 42; -- preview
UPDATE table_name SET col = new_value WHERE id = 42;SELECT ထဲမှာ aggregate မဟုတ်တဲ့ column တိုင်း GROUP BY ထဲမှာ ပါရပါမယ် (ဒါမှမဟုတ် group လုပ်ထားတဲ့ primary key ပေါ် မူတည်ရပါမယ်)။ ပြင်တဲ့ statement တိုင်းကို preview SELECT နဲ့ condition တူတူ တွဲရေးပါ။
Report နှစ်ခု၊ ပြုပြင်မှု နှစ်ခု၊ index နှစ်ခု
Part 1 data ပေါ်မှာ run ပါတယ်။ HAVING COUNT >= 2 ရလဒ်ကို သေချာဖတ်ပါ။ တစ်ယောက်စီမှာ စာအုပ် ၂ အုပ်တိတိ ရှိပြီး >= က 2 ကိုပါ ထည့်တွက်လို့ Murakami (103၊ 104) နဲ့ Rowling (102၊ 105) နှစ်ယောက်လုံးကို ပြန်ပေးပါတယ်၊ ဘယ်သူမှ ၂ အုပ်ထက် ပိုမရေးပါဘူး။ Order အားလုံးက 2026 ထဲမှာ ဖြစ်လို့ DELETE က row 0 ကို ဖျက်ပါတယ်။ ဒါက condition ကို စမ်းဖို့ ကောင်းတဲ့ အခြေအနေပါ။ Spending report: Su Su = 2×15000 + 1×13000 = 43000။
-- 1. Authors with 2 or more books
SELECT a.AuthorName, COUNT(b.BookID) AS BookCount
FROM Authors AS a
INNER JOIN Books AS b ON a.AuthorID = b.AuthorID
GROUP BY a.AuthorID, a.AuthorName
HAVING COUNT(b.BookID) >= 2
ORDER BY a.AuthorName;
-- 2. Total spending per customer
SELECT c.CustomerName, SUM(o.Quantity * b.Price) AS TotalSpent
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
INNER JOIN Books AS b ON o.BookID = b.BookID
GROUP BY c.CustomerID, c.CustomerName
ORDER BY TotalSpent DESC;
-- 3. Restock: preview the row, then change it by primary key
SELECT BookID, Title, Stock FROM Books WHERE BookID = 105;
UPDATE Books SET Stock = 15 WHERE BookID = 105;
-- 4. Clean up orders placed before 2026 (0 rows match in this sample)
DELETE FROM Orders WHERE OrderDate < '2026-01-01';
-- 5. Indexes for common lookups
CREATE INDEX idx_orders_customer ON Orders (CustomerID);
CREATE INDEX idx_books_title ON Books (Title);
-- 6. Confirm the stock change
SELECT Title, Stock FROM Books ORDER BY BookID;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Group by identity
GROUP BY a.AuthorID, a.AuthorNameAuthorID နဲ့ ခွဲလို့ နာမည်တူ author နှစ်ယောက် ရှိလာရင်တောင် ပေါင်းမသွားပါဘူး။ AuthorName ပါထားလို့ SELECT ထဲမှာ နာမည်ကို ပြနိုင်ပါတယ်။
- Filter the groups
HAVING COUNT(b.BookID) >= 2Group တစ်ခုစီကို ရေတွက်ပြီးမှ စစ်ပါတယ်။ Aung Aung (၁ အုပ်) ကို ဒီနေရာမှာ ဖယ်ထုတ်ပါတယ်။
- Money per order line
SUM(o.Quantity * b.Price) AS TotalSpentOrder line တစ်ကြောင်းချင်းရဲ့ Quantity × Price ကို အရင်တွက်ပြီး customer တစ်ယောက်ချင်းစီအတွက် ပေါင်းပါတယ်။ Price က DECIMAL ဖြစ်လို့ ရလဒ်မှာ .00 ပါပါတယ်။
- Change by primary key
UPDATE Books SET Stock = 15 WHERE BookID = 105;Preview SELECT က row တစ်ကြောင်းပဲ ပြခဲ့ပြီး primary key နဲ့ ရွေးထားလို့ UPDATE 1 ဖြစ်မှာ သေချာပါတယ်။ Title နဲ့ ရွေးရင်တော့ အန္တရာယ်ရှိပါတယ်၊ title တူစာအုပ်နှစ်အုပ် ရှိလာရင် နှစ်အုပ်လုံး ပြောင်းသွားပါမယ်။
- Index a foreign key
CREATE INDEX idx_orders_customer ON Orders (CustomerID);Customer တစ်ယောက်ရဲ့ order တွေ ရှာတာနဲ့ Customers–Orders join ကို table ကြီးလာတဲ့အခါ မြန်စေပါတယ်။ Sample data ၄ ကြောင်းမှာတော့ ကွာခြားချက် မမြင်ရပါဘူး။
-- 1. authors with 2+ books
authorname | bookcount
-----------------+-----------
Haruki Murakami | 2
J.K Rowling | 2
(2 rows)
-- 2. spending per customer
customername | totalspent
--------------+------------
Su Su | 43000.00
Hla Hla | 36000.00
Ko Ko | 5000.00
(3 rows)
-- 3. preview, then UPDATE 1
bookid | title | stock
--------+------------------+-------
105 | Fantastic Beasts | 0
(1 row)
-- 4. DELETE 0
-- 5. CREATE INDEX, CREATE INDEX
-- 6. stock after the update
title | stock
--------------------+-------
Myanmar Sarpay | 20
Harry Potter | 10
Norwegian Wood | 8
Kafka on the Shore | 5
Fantastic Beasts | 15
(5 rows)ပြင်တာကို transaction ထဲမှာ run ပါ
Preview SELECT ရှိပြီးတာတောင် production မှာ ပိုလုံခြုံတဲ့ အလေ့အကျင့်က ပြင်တဲ့ statement ကို BEGIN နဲ့ စတဲ့ transaction ထဲမှာ run တာပါ။ PostgreSQL ပြန်ပြောတဲ့ row count က မျှော်လင့်ထားတာနဲ့ ကိုက်ရင် COMMIT၊ မကိုက်ရင် ROLLBACK လုပ်ပါ။ ROLLBACK လုပ်လိုက်ရင် transaction ထဲက ပြောင်းလဲမှုအားလုံး ပျောက်ပြီး data က BEGIN မတိုင်ခင်အတိုင်း ပြန်ဖြစ်ပါတယ်။ psql မှာဆိုရင် \set ON_ERROR_ROLLBACK လို setting တွေလည်း ရှိပါတယ်။
BEGIN;
UPDATE Books SET Price = Price * 1.10 WHERE AuthorID = 3;
-- PostgreSQL reports UPDATE 2: matches the 2 Murakami books we expected
SELECT Title, Price FROM Books WHERE AuthorID = 3 ORDER BY BookID;
-- Norwegian Wood | 13200.00
-- Kafka on the Shore | 14300.00
ROLLBACK; -- changed our mind: prices are back to 12000.00 and 13000.00
SELECT Title, Price FROM Books WHERE AuthorID = 3 ORDER BY BookID;PostgreSQL မှာ CREATE TABLE၊ ALTER TABLE၊ ရိုးရိုး CREATE INDEX လို DDL statement တွေကိုလည်း transaction ထဲမှာ run ပြီး ROLLBACK လုပ်လို့ ရပါတယ်။ ဒါက database တိုင်းမှာ မရပါဘူး: MySQL နဲ့ Oracle မှာ DDL statement တွေက implicit commit လုပ်ပါတယ်။ ဒါပေမယ့် transaction ကို အကြာကြီး ဖွင့်မထားပါနဲ့။ ပြင်ထားတဲ့ row တွေကို lock ကိုင်ထားလို့ တခြား session တွေ စောင့်နေရပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
"Customer တစ်ယောက်ချင်းစီရဲ့ စုစုပေါင်း သုံးငွေ" ဆိုတာ customer lifetime value (CLV) report ရဲ့ အခြေခံ ဖြစ်ပြီး marketing team က VIP customer ခွဲဖို့ သုံးပါတယ်။ "လအလိုက် ဝင်ငွေ"၊ "author အလိုက် ရောင်းရငွေ" လို report တွေက dashboard တွေမှာ နေ့တိုင်း run ပါတယ်။ Table ကြီးလာတဲ့အခါ ဒီ query တွေကို materialized view ဒါမှမဟုတ် ညတိုင်း run တဲ့ summary table အဖြစ် ကြိုတွက်ထားလေ့ ရှိပါတယ်။
Production data ကို ပြင်တဲ့ team တွေက ad-hoc UPDATE တွေကို code review လုပ်တာ၊ backup ရှိမရှိ အရင်စစ်တာ၊ ဒုတိယလူတစ်ယောက်နဲ့ "pair" လုပ်ပြီး run တာ စတာတွေ လုပ်ကြပါတယ်။ ဒီ lesson ရဲ့ DELETE လို data ဟောင်းရှင်းတဲ့ အလုပ်ကိုတော့ batch သေးသေးလေးတွေနဲ့ (ဥပမာ တစ်ခါ row ၁၀၀၀၀) ခွဲလုပ်ပြီး table ကို ကြာကြာ lock မဖြစ်အောင် ရှောင်ပါတယ်။ Batch တစ်ခုစီက သူ့ကိုယ်ပိုင် transaction တိုတိုလေး ဖြစ်ပြီး နောက်မှ ဂဏန်းတွေ စစ်လို့ရအောင် job က row ဘယ်နှစ်ကြောင်း ဖျက်ခဲ့လဲ log ထဲမှာ မှတ်ထားပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
DELETE/UPDATE မှာ WHERE မေ့တာ
DELETE FROM Orders; က order အားလုံးကို ဖျက်ပြီး autocommit မှာ ပြန်ယူလို့ မရပါဘူး။ WHERE ကို အရင်ရေး၊ SELECT နဲ့ စမ်း၊ transaction ထဲမှာ run ပြီး row count ကို စစ်ပါ။
Aggregate condition ကို WHERE ထဲ ထည့်တာ
WHERE COUNT(b.BookID) >= 2 က aggregate functions are not allowed in WHERE error ပေးပါတယ်။ WHERE က group မဖွဲ့ခင် row တစ်ကြောင်းချင်းကို စစ်လို့ COUNT ကို မသိသေးပါဘူး။ HAVING ကို သုံးပါ။
Join က row တွေ ပွားလာတာကို မသိဘဲ ရေတွက်တာ
Authors–Books–Orders သုံးခုကို join ပြီး COUNT(b.BookID) ရေတွက်ရင် စာအုပ်တစ်အုပ်မှာ order နှစ်ခုရှိတိုင်း အဲဒီစာအုပ်ကို နှစ်ခါ ရေတွက်မိပါတယ်။ ဒီ sample data မှာ စာအုပ်တစ်အုပ်ကို order တစ်ခုထက် မပိုလို့ မမြင်ရပေမယ့် data ကြီးလာရင် ချက်ချင်း ပေါ်ပါမယ်။ COUNT(DISTINCT b.BookID) ကို သုံးပါ ဒါမှမဟုတ် join မလုပ်ခင် subquery ထဲမှာ အရင် aggregate လုပ်ပါ။
Leading % search ကို B-tree index က မြန်စေမယ်ထင်တာ
Books(Title) index က '%harry%' အတွက် အသုံးမဝင်ပါဘူး။ Index ထဲမှာ စာလုံးအစကနေ စီထားလို့ အလယ်က စာလုံးနဲ့ ရှာရင် နေရာ မသိနိုင်လို့ပါ။ pg_trgm GIN index ဒါမှမဟုတ် full-text search ကို သုံးပါ။
Mini challenge
Author price report နဲ့ လအလိုက် ဝင်ငွေ
ဆိုင်ပိုင်ရှင်က report နှစ်ခု ထပ်တောင်းပါတယ်: author တစ်ယောက်ချင်းစီရဲ့ စာအုပ်အရေအတွက်နဲ့ ပျမ်းမျှဈေး၊ နောက်တစ်ခုက လအလိုက် order အရေအတွက်နဲ့ ဝင်ငွေ။
- Author report: AuthorName၊ BookCount၊ AvgPrice (ROUND နဲ့ ဒသမ ၂ နေရာ) ကို AvgPrice ကြီးစဉ်ငယ်လိုက် ပြပါ
- Author ကို AuthorID နဲ့ group လုပ်ပါ
- Monthly report: to_char(OrderDate, 'YYYY-MM') နဲ့ လ၊ order အရေအတွက်၊ SUM(Quantity × Price) ကို ပြပါ
- Monthly report ကို လအစဉ်လိုက် စီပါ
-- Report A: books and average price per author
SELECT a.AuthorName,
-- TODO: number of books AS BookCount
-- TODO: average price rounded to 2 places AS AvgPrice
FROM Authors AS a
INNER JOIN Books AS b ON b.AuthorID = a.AuthorID
-- TODO: group by the author's id and name
-- TODO: highest average first
;
-- Report B: orders and revenue per month
SELECT -- TODO: month as 'YYYY-MM', order count, revenue
FROM Orders AS o
INNER JOIN Books AS b ON b.BookID = o.BookID
-- TODO: group by the month, sort by the month
;ရလဒ်: Report A: J.K Rowling 2 14500.00၊ Haruki Murakami 2 12500.00၊ Aung Aung 1 5000.00။ Report B: 2026-01 မှာ order 3 ခု 71000.00၊ 2026-02 မှာ order 1 ခု 13000.00။
အရိပ်အမြွက် ကြည့်ရန်
ROUND(AVG(b.Price), 2) လို aggregate ကို function နဲ့ ပတ်လို့ ရပါတယ်။ PostgreSQL မှာ GROUP BY ထဲမှာ SELECT list ထဲက alias (GROUP BY month) ဒါမှမဟုတ် အစဉ်နံပါတ် (GROUP BY 1) ကို သုံးလို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
-- Report A: books and average price per author
SELECT a.AuthorName,
COUNT(b.BookID) AS BookCount,
ROUND(AVG(b.Price), 2) AS AvgPrice
FROM Authors AS a
INNER JOIN Books AS b ON b.AuthorID = a.AuthorID
GROUP BY a.AuthorID, a.AuthorName
ORDER BY AvgPrice DESC;
-- Report B: orders and revenue per month
SELECT to_char(o.OrderDate, 'YYYY-MM') AS month,
COUNT(*) AS orders,
SUM(o.Quantity * b.Price) AS revenue
FROM Orders AS o
INNER JOIN Books AS b ON b.BookID = o.BookID
GROUP BY month
ORDER BY month;Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- WHERE က row ကို၊ HAVING က group ကို filter လုပ်ပါတယ်
- နာမည် မဟုတ်ဘဲ ID နဲ့ group လုပ်ပြီး နာမည်ကို ပြဖို့ GROUP BY ထဲ ထပ်ထည့်ပါ
- UPDATE/DELETE တိုင်းကို SELECT နဲ့ အရင်စမ်း၊ primary key နဲ့ ရွေး၊ transaction ထဲ run ပါ
- Join က row တွေ ပွားနိုင်လို့ ရေတွက်တဲ့အခါ COUNT(DISTINCT ...) ကို စဉ်းစားပါ
- Foreign key column တွေကို index လုပ်ပါ၊ '%term%' search အတွက် B-tree မလုံလောက်ပါ
PostgreSQL SQL Tutorial — PostgreSQL Global Development Group