- ~
題目描述
獲取員工其當前的薪水比其manager當前薪水還高的相關信息,當前表示to_date='9999-01-01',
結果第一列給出員工的emp_no,
第二列給出其manager的manager_no,
第三列給出該員工當前的薪水emp_salary,
第四列給該員工對應的manager當前的薪水manager_salary
CREATE TABLE dept_emp
(emp_no
int(11) NOT NULL,dept_no
char(4) NOT NULL,from_date
date NOT NULL,to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE dept_manager
(dept_no
char(4) NOT NULL,emp_no
int(11) NOT NULL,from_date
date NOT NULL,to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE salaries
(emp_no
int(11) NOT NULL,salary
int(11) NOT NULL,from_date
date NOT NULL,to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));spa
本題主要思想是建立兩張表(一張記錄當前全部員工的工資,另外一張只記錄部門經理的工資)進行比較,具體思路以下: 一、先用INNER JOIN鏈接salaries和demp_emp,創建當前全部員工的工資記錄sem 二、再用INNER JOIN鏈接salaries和demp_manager,創建當前全部員工的工資記錄sdm 三、最後用限制條件sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary找出同一部門中工資比經理高的員工,並根據題意依次輸出emp_no、manager_no、emp_salary、manager_salary SELECT sem.emp_no AS emp_no, sdm.emp_no AS manager_no, sem.salary AS emp_salary, sdm.salary AS manager_salary FROM (SELECT s.salary, s.emp_no, de.dept_no FROM salaries s INNER JOIN dept_emp de ON s.emp_no = de.emp_no AND s.to_date = '9999-01-01' ) AS sem, (SELECT s.salary, s.emp_no, dm.dept_no FROM salaries s INNER JOIN dept_manager dm ON s.emp_no = dm.emp_no AND s.to_date = '9999-01-01' ) AS sdm WHERE sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary