create
table
ifnot
exists employee (id int
, name varchar
(255
), salary int
, managerid int
)truncate
table employee
insert
into employee (id, name, salary, managerid)
values
('1'
,'joe'
,'70000'
,'3'
)insert
into employee (id, name, salary, managerid)
values
('2'
,'henry'
,'80000'
,'4'
)insert
into employee (id, name, salary, managerid)
values
('3'
,'sam'
,'60000'
,'none'
)insert
into employee (id, name, salary, managerid)
values
('4'
,'max'
,'90000'
,'none'
)
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
是唯一乙個收入超過他的經理的員工。
方法1:使用join+----------+
| employee |
+----------+
| joe |
+----------+
耗時:520msselect e1.name as employee
from employee as e1
right
join employee as e2
on e1.managerid = e2.id
where e1.salary > e2.salary
耗時:560msselect e1.name as employee
from employee as e1,
employee as e2
where e1.managerid is
notnull
and e1.managerid = e2.id
and e1.salary > e2.salary
耗時:828msselect name as employee
from employee as e1
where e1.managerid is
notnull
and e1.salary >
(select salary
from employee as e2
where e1.managerid = e2.id)
資料庫 181 超過經理收入的員工
官方題解 超過經理收入的員工 裡存有每個雇員經理的資訊,我們也許需要從這個表裡獲取兩次資訊。select from employee as a,employee as b 從兩個表裡使用 select 語句可能會導致產生 笛卡爾乘積 在這種情況下,輸出會產生 4 4 16 個記錄。然而我們只對雇員工...
181 超過經理收入的員工
employee表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 null 給定em...
181 超過經理收入的員工
employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 null 給定 ...