SQL Exercise 3: Find the current leader salary and department number of each department

SQL Exercise 3: Find the current leader salary and department number of each department

Topic link: Niuke.com

Title description
Find the (dept_manager.to_date='9999-01-01')current (salaries.to_date='9999-01-01')salary details of the current leaders of each department and their corresponding department numbers dept_no
(Note: the output results are salaries.emp_nosorted in ascending order, and please note that the output results dept_noare the last column)

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`));

CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL, -- '部门编号'
`emp_no` int(11) NOT NULL, --  '员工编号'
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));

Solution
First observe the structure of the two tables. Both tables use composite primary keys. What needs to be queried is the salary details in the salaries table, that is, all the information in the table, and then find the department number dept_no in dept_manager through the corresponding emp_no, and then pass the question Filter by the given conditions. Finally through emp_no for ascending order

select s.*, dept_no from salaries as s, dept_manager as d
where s.emp_no = d.emp_no and s.to_date='9999-01-01' and d.to_date='9999-01-01' 
order by emp_no;

Guess you like

Origin blog.csdn.net/qq_43965708/article/details/113195567