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