SQL資料庫練習 之超過經理收入的員工

2021-10-19 08:50:14 字數 853 閱讀 5427

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 |

±---------+

查詢 表1員工.名字 別名設定為employee

表有 employee 表1員工表別名為staff 和 employee 表2經理 別名為manager

條件 員工.工資 > 經理.工資 and 員工.經理id = 經理.id

select staff.name as employee 

from employee as staff,employee as manager

where staff.salary > manager.salary and staff.managerid = manager.id

資料庫 181 超過經理收入的員工

官方題解 超過經理收入的員工 裡存有每個雇員經理的資訊,我們也許需要從這個表裡獲取兩次資訊。select from employee as a,employee as b 從兩個表裡使用 select 語句可能會導致產生 笛卡爾乘積 在這種情況下,輸出會產生 4 4 16 個記錄。然而我們只對雇員工...

LeetCode資料庫題 超過經理收入的員工

employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 null 給定 ...

181 超過經理收入的員工(SQL)

一 題目描述 employee 表包含所有員工,他們的經理也屬於員工。每個員工都有乙個 id,此外還有一列對應員工的經理的 id。id name salary managerid 1 joe 70000 3 2 henry 80000 4 3 sam 60000 null 4 max 90000 n...