1.显示所有员工的姓名,部门号和部门名称。
select a.last_name, a.department_id, b.department_name from employees as a,departments as b where a.department_id=b.department_id
# 2.查询90号部门员工的job_id和90号部门的location_id
SELECT e.job_id,d.location_id from employees as e join departments as d on d.department_id=e.department_id WHERE d.department_id=90
# 3.选择所有有奖金的员工的 last_name , department_name , location_id , city
select employees.last_name, departments.department_name, locations.location_id, locations.city from employees left join departments on departments.department_id=employees.department_id left join locations on departments.location_id=locations.location_id where commission_pct is not null
# 4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
select last_name, job_id, employees.department_id , department_name from employees left join departments on employees.department_id=departments.department_id left join locations on departments.location_id=locations.location_id where locations.city='Toronto'
# 5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’
select department_name,city,last_name, job_title, salary from departments left join employees on employees.department_id=departments.department_id left join locations on departments.location_id=locations.location_id left join jobs on employees.job_id=jobs.job_id where department_name="Executive"
# 6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式employees Emp# manager Mgr#kochhar 101 king 100
select e.last_nameemployees, e.employee_idEmp#,m.last_namemanager, m.employee_idMgr#from employees e left join employees m on e.manager_id = m.employee_id
# 7.查询哪些部门没有员工
select department_name from departments left join employees on departments.department_id=employees.department_id where employees.department_id is null
# 8. 查询哪个城市没有部门
SELECT city from locations left join departments on locations.location_id = departments.location_id where departments.location_id is null
# 9. 查询部门名为 Sales 或 IT 的员工信息
select employees.* from employees left join departments on employees.department_id = departments.department_id where department_name = "Sales" or department_name = "IT"