06多表查询SQL练习

题目

在这里插入图片描述

1.显示所有员工姓名,部门号和部门名称

select last_name,employees.department_id,department_name
from employees left outer join departments
on employees.department_id = departments.department_id;

在这里插入图片描述
在这里插入图片描述
注:遇到所有字眼时需要考虑外连接查询

2.查询90号部门员工的job_id和90号部门的location_id

select job_id,location_id
from departments join employees
on departments.department_id = employees.department_id
where departments.department_id = 90;

在这里插入图片描述

3.选择所有有奖金的员工的last_name,department_name,location_id,city

select last_name,department_name,locations.location_id,city
from employees join departments
on employees.department_id = departments.department_id
join locations
on departments.location_id = locations.location_id
where employees.commission_pct is not NULL;

在这里插入图片描述
在这里插入图片描述

4.选择city在Totonto工作的员工last_name,job_id,department_id,department_name

select last_name,job_id,employees.department_id,department_name
from employees join departments
on employees.department_id = departments.department_id
join locations
on departments.location_id = locations.location_id
where city = 'Toronto';

在这里插入图片描述

5.查询员工所在的部门名称,部门地址,姓名,工作,工资,其中员工所在部门的部门名称为‘Executive’

select department_name,city,last_name,job_id,salary
from employees join departments
on employees.department_id = departments.department_id
join locations
on departments.location_id = locations.location_id
where department_name = 'Executive';

在这里插入图片描述

6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于以下形式

在这里插入图片描述

select worker.last_name "employees",worker.employee_id "Emp#",manager.last_name "manager",manager.employee_id "man#"
from employees worker join employees manager
on worker.manager_id = manager.employee_id;

在这里插入图片描述

7.查询哪些部门没有员工

select departments.department_id,department_name
from employees right outer join departments
on employees.department_id = departments.department_id
where employees.employee_id is null;

在这里插入图片描述

8.查询哪个城市没有部门

select city
from departments right outer join locations
on departments.location_id = locations.location_id
where departments.department_id is null;

在这里插入图片描述

9.查询部门名为Sales或IT的员工信息

select last_name,employees.employee_id
from employees join departments
on employees.department_id = departments.department_id
where department_name = 'Sales' or department_name = 'IT';

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值