အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 7 အတွက်ကတော့ Leetcode ရဲ့ difficulty Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ (570. Managers with at Least 5 Direct Reports) ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။

ဒီပုစ္ဆာ အတွက်ကတော့ Employee table တစ်ခုထဲပဲပေးထားပါတယ်။ ကျွန်တော်တို့က အနိမ့်ဆုံး ငါးခုနဲ့ အထက် Employee တွေက report လုပ်တဲ့ manager ရဲ့ name ကိုရှာပေးရမှာပဲဖြစ်ပါတယ်။
1*Example*
2
3Input:
4Employee table:
5+-----+-------+------------+-----------+
6| id | name | department | managerId |
7+-----+-------+------------+-----------+
8| 101 | John | A | null |
9| 102 | Dan | A | 101 |
10| 103 | James | A | 101 |
11| 104 | Amy | A | 101 |
12| 105 | Anne | A | 101 |
13| 106 | Ron | B | 101 |
14+-----+-------+------------+-----------+
15Output:
16+------+
17| name |
18+------+
19| John |
20+------+
21 manager id null ဖြစ်နေရင်တော့ manager မရှိဘူးပေါ့။ အဲ့တော့ ကျွန်တော်တို့ query လေးရေးလိုက်ရအောင်ဗျ။
1
2SELECT m.name FROM Employee m
3JOIN Employee e ON e.managerId = m.id
4GROUP BY m.name, m.id
5HAVING COUNT(e.id) >= 5;ဒီမှာဆို ကျွန်တော်ပထမဆုံး Employee ကို m (manager )လို့ alias ပေးထားတယ်ဗျ။ self join နဲ့ Employee e ရဲ့managerId က m (manager) ရဲ့ id နဲ့တူတာကို join ထားတယ်ပေါ့။ ဒီလို join လိုက်ခြင်းဖြင့် ကျွန်တော်တို့ manager တွေတော့ရလာခဲ့ပြီ အဲ့တော့ manager တွေတစ်ယောက်ခြင်းစီမှာ report ငါးခုအထက်ရှိမရှိစစ်ဖို့ တစ်ယောက်ခြင်းစီရဲ့ name,id ကို Group By လုပ်ထားလိုက်ပါတယ်။
Group By မှာ condition စစ်ချင်ရင် Having လေးကို အသုံးပြုရတာဖြစ်လို့ Having သုံးပြီး employee ရဲ့ id count ကို 5နဲ့ အထက်စစ်လိုက်ပါတယ်။ ဒါဆို report 5ခုနဲ့ခက်ရှိတဲ့ manager တွေကို ရရှိပြီပဲဖြစ်ပါတယ်ခင်ဗျာ။ Day 7 အတွက်ပုစ္ဆာလေးလည်းပြီးဆုံးခဲ့ပြီဗျာ။ Video လေးကိုတော့ထုံးစံအတိုင်း အောက်က YouTube link လေးကနေဝင်ရောက်ကြည့်ရှု့နိုင်ပါတယ်ဗျ။ Stay safe and see you all ပါဗျ။
30 Days Of LeetCode Database Challenge Day 7 - (570. Managers with at Least 5 Direct Reports)
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


