– 34. 查询所有员工的姓名和部门名称,没有部门的员工不需要展示
SELECTe.NAME '员工姓名',d.NAME '部门名称'
FROMt_emp eINNER JOIN t_dept d ON e.dept_id = d.id;
– 35. 查询所有员工的姓名和部门名称,没有部门的员工展示BOSS
SELECTe.NAME '员工姓名',ifnull( d.NAME, 'BOSS' ) '部门名称'
FROMt_emp eLEFT JOIN t_dept d ON e.dept_id = d.id;
– 36. 查询jones所在部门的部门名称
SELECTe.NAME '员工姓名',d.NAME '部门名称'
FROMt_emp eINNER JOIN t_dept d ON e.dept_id = d.id
WHEREe.NAME = 'jones';
– 37. 找出谁是领导
SELECT DISTINCTe.NAME '员工姓名'
FROMt_emp eINNER JOIN t_emp e2 ON e.id = e2.manager_id;
– 38. 找出谁不是领导
SELECT`name`
FROMt_emp e
WHERENOT EXISTS ( SELECT 1 FROM t_emp e2 WHERE e2.manager_id = e.id );
– 39. 找出那些部门没有员工
SELECTd.NAME
FROMt_dept dLEFT JOIN t_emp e ON e.dept_id = d.id
WHEREe.id IS NULL;
– 40. 找出那些部门有员工
SELECT DISTINCTd.NAME
FROMt_dept dLEFT JOIN t_emp e ON e.dept_id = d.id
WHEREe.id IS NOT NULL;
– 41. 找出所有工资比martin高的员工
SELECTe1.NAME,e1.salary
FROMt_emp e1,t_emp e2
WHEREe1.salary > e2.salary AND e2.NAME = 'martin' AND e1.dept_id IS NOT NULL;