SQL exercise three

1. Get the current manager of all employees. If the current manager is one's own, the result will not be displayed, and the current means to_date='9999-01-01'.

As a result, the first column gives the emp_no of the current employee, and the second column gives the manager_no corresponding to its manager.
CREATE TABLE dept_emp(
emp_noint(11) NOT NULL,
dept_nochar(4) NOT NULL,
from_datedate NOT NULL,
to_datedate NOT NULL,
PRIMARY KEY ( emp_no, dept_no));
CREATE TABLE dept_manager(
dept_nochar(4) NOT NULL,
emp_noint(11) NOT NULL,
from_datedate NOT NULL,
to_datedate NOT NULL,
PRIMARY KEY ( emp_no, dept_no));
Insert picture description here
Note: When answering, although the position is different, the department number is the same.

select d.emp_no, m.emp_no as manager_no
from dept_emp d, dept_manager m
where d.to_date='9999-01-01' 
and m.to_date='9999-01-01'
and d.dept_no =m.dept_no
and d.emp_no !=m.emp_no

2. Get the relevant information about the highest salary of the current employee in all departments, and give dept_no, emp_no and their corresponding salary. The
default current is to_date='9999-01-01'.

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 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));
Insert picture description here

select dept_no, d.emp_no, max(salary) 
from dept_emp d, salaries s
where d.emp_no=s.emp_no
and d.to_date='9999-01-01'and s.to_date='9999-01-01'
group by dept_no

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/105824438