Niu Ke.com database SQL combat 03-find all the information of the latest employees

Niu Ke.com database SQL combat 03-find all the information of the latest employees

Title description

Find the current (to_date = '9999-01-01') leadership salary details of each department and its corresponding department number 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`));

Enter description:

no

Output description:

emp_no birth_date birth_date last_name last_name hire_date
10008 1958-02-19 Saniya Kalloufi M 1994-09-15
emp_no salary from_date to_date dept_no
10002 72527 2001-08-02 9999-01-01 d001
10004 74057 2001-11-27 9999-01-01 d004
10005 94692 2001-09-09 9999-01-01 d003
10006 43311 2001-08-02 9999-01-01 d002
10010 94409 2001-11-23 9999-01-01 d006

My answer

select s.emp_no,s.salary,s.from_date,s.to_date,d.dept_no
from dept_manager d
join salaries s
on d.emp_no = s.emp_no
where d.to_date='9999-01-01'
and s.to_date='9999-01-01'

My method did not pass the test, personally feel that the problem is not given to the table data, and it is not clear

After the test, it was found that the s and d tables could be passed by changing the position. . .

I think the best answer

SELECT s.*, d.dept_no FROM salaries s ,  dept_manager d 
WHERE s.to_date='9999-01-01' 
AND d.to_date='9999-01-01' 
AND s.emp_no = d.emp_no;
Published 136 original articles · Like 58 · Visits 360,000+

Guess you like

Origin blog.csdn.net/sunbocong/article/details/105445872