အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 3 အတွက်ကတော့ Leetcode ရဲ့ difficulties medium problem တစ်ပုဒ်ဖြစ်တဲ့ 184. Department Highest Salary ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ Employee နဲ့ Department ဆိုတဲ့ table 2 ခုပေစထားပြီး departmentId ကို foreign key အနေနဲ့ Employee table ထဲမှာ reference လုပ်ထားတာပဲဖြစ်ပါတယ်။ Department တစ်ခုချင်းစီရဲ့ လစာအမြင့်ဆုံး Employee တွေကို ရှာပေးရမှာပဲဖြစ်ပါတယ်။ ပထမဆုံးအနေနဲ့ ဒီ problem ကို solve လုပ်ဖို့ sub query နဲ့ function လေးကို အသုံးပြုသွားမှာပဲဖြစ်ပါတယ်။
![]()
Sub Query
Sub query ဆိုတာကတော့ main query ရဲ့အတွင်းမှာ နောက်ထပ် query တစ်ခုရေးသားပြီး main query ရဲ့ result တစ်ခု အနေနဲ့ အသုံးပြုတာပဲဖြစ်ပါတယ်။ Sub query မှာ Single row sub query နဲ့ Multiple row sub query ဆိုတာရှိပါတယ်။ ဒီနေရာမှာကတော့ single row sub query လေးကို အသုံးပြုသွားမှာပဲဖြစ်ပါတယ်။
Function
SQL မှာ function ဆိုတာက pre-defined or user-defined ဆိုတာမျိုးရှိပြီး ကျွန်တော်တို့ params တွေကနေမှ result တစ်ခုကို ပြန်ရယူချင်ရင်အသုံးပြုတာပဲဖြစ်ပါတယ်။ ကျွန်တော်ကတော့ built-in function လေးတစ်ခုအသုံးပြုသွားမှာပဲဖြစ်ပါတယ်။
1SELECT d.name AS Department, e.name AS Employee, e.salary AS Salary FROM Employee e
2JOIN Department d ON d.id = e.departmentId WHERE e.salary = (
3 SELECT MAX(e.salary) from Employee e where e.departmentId = d.id
4);ဒီ query မှာဆိုရင် ကျွန်တော်တို့ output table မှာပြထားသလို ပထမဆုံး column က Department ပေါ့ အဲ့တော့ Department table ထဲက name ကိုယူပြီး column name ပေးချင်တာဖြစ်တော့ AS နဲ့နောက်က လိုချင်တဲ့ name အတိုင်းသတ်မှတ်ပေးလိုက်ပါတယ်။ Employee တိုင်းမှာ Department ရှိတာဖြစ်လို့ table နှစ်ခုကို default inner join နဲ့ပဲ join ထားလိုက်ပါတယ်။ where နောက်က condition မှာတော့ department တစ်ခုခြင်းဆီရဲ့ အမြင့်ဆုံးလစာရှိတဲ့သူတွေကို လိုချင်တာဖြစ်လို့ sub query လေးကနေ department တစ်ခုခြင်းစီအတွက် အမြင့်ဆုံးလစာ ကို MAX function နဲ့ယူထားတာပဲဖြစ်ပါတယ်။ ဒီ query နဲ့ ကျွန်တော်တို့ လိုချင်တဲ့ results ကရရှိသွားမှာပဲဖြစ်ပါတယ်။ Day 3 အတွက်ကိုလည်း အောက် က YouTube link လေးကနေတစ်ဆင့် ဝင်ရောက်ကြည့်ရှု့နိုင်ပါတယ်ခင်ဗျာ။ see you all.
30 Days Of LeetCode Database Challenge - Day 3 (184. Department Highest Salary)
Watch on YouTubeDiscussion
Join the conversation
How do you feel about this article?
Comments
Sign in to join the conversation
Sign in to be the first to comment!
Share Your Article
Share with your professional network
Recent Articles

Day 2 - GitHub Actions ရဲ့ အဓိက Concepts များနှင့် Runner အကြောင်း
ပြီးခဲ့တဲ့အပတ် Day 1 တုန်းက ကျွန်တော်တို့ CI/CD ရဲ့ Concept တွေကို အကြမ်းဖျင်း ပြောခဲ့ကြပြီးပြီနော်။ ဒီနေရာမှာ လက်တွေ့မစ...
Day 1 - Software Development ကို ပိုပြီးမြန်စေမယ့် CI/CD
Software တစ်ခုရေးပြီးပြီဆိုရင် "ငါ့စက်ထဲမှာတော့ အလုပ်လုပ်တယ်" ဆိုရုံနဲ့ မပြီးသေးပါဘူး။ User တွေသုံးမယ့် Server ပေါ်ရောက်...

Secure AWS ECR Github Action Using OIDC
Modern DevOps နှင့် Cloud Security တို့မှာ အဓိကဖြစ်လာသည့် Authentication System ယနေ့ခေတ် DevOps, Cloud Engineering, CI/C...

Manual vs Automated Infrastructure: Why "useradd" Still Matters in the Terraform Era
DevOps careerကို လျှောက်နေတဲ့သူတိုင်း Terraform၊ Ansible စတဲ့ Infrastructure as Code (IaC) tools တွေရဲ့ အလုပ်လုပ်နိုင်စွ...
From Surviving to Thriving
“AI ကြောင့် developer အလုပ်ပျောက်သွားမလား?” ဆိုတဲ့ မေးခွန်းကို Junior developer တိုင်း စဉ်းစားဖူးကြမှာပါ။ ဈေးကွက်အခြေအနေ...

