LeetCode:管理者の収入の従業員よりも181以上。

トピックへのリンク:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/

タイトル

Employeeテーブルにはすべての従業員が含まれている、彼らの経営者は、従業員に属します。各従業員は、スタッフの対応ID管理に加えて、IDを持っています。

---- + ------- + -------- + + ----------- +
| ID |名前|給与|マネージャーID |
+ ---- + ---------- -------- + + + -------
| 1 |ジョー| 70000 | 3 |
| 2 |ヘンリー| 80000 | 4 |
| 3 | SAM | 60000 | NULL |
| 4 |最大| 90000 | NULL |
+ ---- + ------- + -------- + ----------- +
employeeテーブル与え、あなたは彼らの従業員よりも多くを獲得マネージャーの名前を取得することができますSQLクエリを記述します。上記の表では、ジョーは、彼のマネージャーのスタッフよりも多くを獲得唯一のものです。

+ ---------- +
| 従業員|
+ ---------- +
| ジョー|
+ ---------- +

出典:ボタン(LeetCode)滞在
:リンクhttps://leetcode-cn.com/problems/employees-earning-more-than-their-managers
すべてのネットワークからの控除が著作権を保有します。商業転載は許可公式、非商用の転載は、ソースを明記してくださいお問い合わせください。

答え

最初のアイデアは、接続するので、減算を行います。

---- 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
) c
where 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その回答ではなく、効率的で接続が付属しており、テストされません。

おすすめ

転載: www.cnblogs.com/hider/p/11723669.html