အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ 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

AWS - Application Load Balancer
Elastic Load Balancing (ELB) ELB ဆိုတာကတော့ request တွေကို တစ်နေရာတည်းမှ လက်ခံကာ Amazon EC2 instances၊ containers, etc.....

Terraform Day 3: Benefits of Terraform State
Terraform ကိုလေ့လာ တဲ့အခါ ကျွန််တော်တို့ရဲ့ Project Folder ထဲမှာ terraform.tfstate ဆိုတဲ့ ဖိုင်လေးကို တွေ့ဖူးကြပါလိမ့်မယ...

Terraform Day 2: Essential IaC Principles You Must Know
မနေ့ကတော့ Terraform အကြောင်း အကြမ်းဖျင်း Concept ကို ပြောပြခဲ့ပြီးပြီဆိုတော့ ဒီနေ့မှာတော့ Terraform ကို Professional ကျက...

TCP/IP Protocol
အားလုံးပဲမင်္ဂလာပါ။ ဒီနေ့ ကျွန်တော်တို့ TCP/IP Protocol အကြောင်း ဆွေးနွေးသွားပါမယ်။ ပထမဆုံးအနေနဲ့ TCP/IP ရဲ့ History လေး...

Terraform Day 1: Introduction to IAC and Terraform
ကျွန်တော်တို့ cloud အကြောင်း စပြောကြပြီဆိုရင် အရင်ဆုံး ခေါင်းထဲရောက်လာတာ Console ထဲဝင်၊ UI ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...

