employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。
+----+-------+--------+-----------+
| id | name | salary | managerid |
+----+-------+--------+-----------+
| 1 | joe | 70000 | 3 |
| 2 | henry | 80000 | 4 |
| 3 | sam | 60000 | null |
| 4 | max | 90000 | null |
+----+-------+--------+-----------+
給定 employee 表,編寫乙個 sql 查詢,該查詢可以獲取收入超過他們經理的員工的姓名。在上面的**中,joe 是唯一乙個收入超過他的經理的員工。
+----------+
| employee |
+----------+
| joe |
+----------+
第一想法,通過自連線,再做減法。
應該可以繼續優化的。。---- oracle ----
/* write your pl/sql query statement below */
select c.name as employee
from
( select a.name,
(a.salary - b.salary) as salary
from employee a
left join employee b
on a.managerid = b.id
) cwhere c.salary > 0 ---- 868ms
通過---- oracle ----
/* write your pl/sql query statement below */
select a.name as employee
from employee a
left join employee b
on a.managerid = b.id
where a.salary > b.salary ---- 528ms
where
實現
內連線會自動過濾---- mysql ----
select a.name as employee
from employee a,
employee b
where a.managerid = b.id
and a.salary > b.salary; ---- 260ms
null
,所以關聯的時候無須再設定managerid is not null
過濾條件。
子查詢效能 & 關聯查詢,到底哪個快?驗證一番。
直接通過from table a, table b
會產生笛卡爾積,影響效率。
另外,還可通過子查詢和exists
進行解答,不過效率都不如連線來得快,就不進行測試了。
LeetCode 181 超過經理收入的員工 解題
超過經理收入的員工 employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 9000...
LeetCode 181 超過經理輸入的員工
題目 employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 null ...
LeetCode181超過經理收入的員工
題目 employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 null ...