Niu Ke.com database SQL combat 04-find the last_name and first_name and dept_no of all employees in the assigned department
Title description
Find last_name and first_name and dept_no of all employees who have been assigned to the department
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 `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
Enter description:
no
Output description:
last_name | first_name | dept_no |
---|---|---|
Facello | Georgi | d001 |
Omitted | Omitted | Omitted |
Piveteau | Duangkaew | Duangkaew |
My answer
select e.last_name,e.first_name,d.dept_no
from employees e
join dept_emp d
on e.emp_no = d.emp_no
The title does not give the data and specific requirements, directly use natural connection
I think the best answer
The answer is output in the order of the employees table, so when using an inner join query, the employees table must be placed first.
select last_name,first_name,dept_no from employees,dept_emp where dept_emp.emp_no = employees.emp_no;
When using the left join query, employees in the employees who are not assigned to the department (not recorded in the dept_emp table) the dept_no field is automatically NULL and then output, so it should be eliminated (compound conditional join query).
select last_name,first_name,dept_no
from employees left join dept_emp
on employees.emp_no = dept_emp.emp_no
where dept_emp.dept_no<>'';