首页 > 数据库 >【MySQL练习】多表查询练习(一)

【MySQL练习】多表查询练习(一)

时间:2022-10-17 18:36:59浏览次数:76  
标签:多表 name employees 练习 location MySQL department id SELECT

表数据:https://www.cnblogs.com/zhishu/p/16452950.html

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

所有员工,用left join。
left outer join和left join的效果是一样的。

#107条数据,有的员工没有部门
SELECT a.last_name,a.department_id,b.department_name
FROM employees a
LEFT JOIN departments b ON a.department_id = b.department_id;

#错误的,106条数据
SELECT a.last_name,a.department_id,b.department_name
FROM employees a,departments b
WHERE a.department_id = b.department_id;

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

SELECT a.job_id,b.location_id
FROM employees a
JOIN departments b ON a.department_id = b.department_id
WHERE b.department_id = '90';

#不用join的方式
SELECT a.job_id,b.location_id
FROM employees a,departments b
WHERE a.department_id = b.department_id AND b.department_id = '90';

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

所有员工,使用left join

SELECT a.last_name,b.department_name,b.location_id,c.city
FROM employees a
LEFT JOIN departments b ON a.department_id = b.department_id
LEFT JOIN locations c ON b.location_id = c.location_id
WHERE a.commission_pct IS NOT NULL;

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

SELECT a.last_name,a.job_id,a.department_id,b.department_name
FROM employees a
JOIN departments b ON a.department_id = b.department_id
JOIN locations c ON b.location_id = c.location_id
WHERE c.city = 'Toronto';

#或
SELECT a.last_name,a.job_id,b.department_id,b.department_name
FROM employees a,departments b,locations c
WHERE a.department_id = b.department_id AND b.location_id = c.location_id;

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

SELECT b.department_name,CONCAT(c.state_province,c.street_address) AS address,a.last_name,d.job_title,a.salary
FROM employees a
JOIN departments b ON a.department_id = b.department_id
JOIN locations c ON b.location_id = c.location_id
JOIN jobs d ON a.job_id = d.job_id
WHERE b.department_name = 'Executive';

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

employees    Emp#    manager    Mgr#
kochhar           101    king             100

SELECT emp.last_name AS "employees",emp.employee_id AS "Emp",mgr.last_name AS "manager",mgr.employee_id AS "Mgr"
FROM employees emp
LEFT JOIN employees mgr ON emp.manager_id = mgr.employee_id;

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

b表的哪个字段为空都行,一般写b.department_id IS NULL

#第一种方式
SELECT a.department_id,a.department_name
FROM departments a
LEFT JOIN employees b ON a.department_id = b.department_id
WHERE b.employee_id IS NULL;

#第二种方式:查询不存在相等的,即为没有部门的员工
SELECT a.department_id,a.department_name
FROM departments a
WHERE NOT EXISTS(
	SELECT * 
	FROM employees b
	WHERE a.department_id =b.department_id
);

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

SELECT a.city
FROM locations a
LEFT JOIN departments b ON a.location_id = b.location_id
WHERE b.department_id IS NULL;

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

SELECT a.employee_id,a.last_name,b.department_name
FROM employees a
JOIN departments b ON a.department_id = b.department_id
WHERE b.department_name = 'Sales' OR b.department_name = 'IT';

#或
SELECT employee_id,last_name,department_name
FROM employees e,departments d
WHERE e.department_id = d.`department_id`
AND d.`department_name` IN ('Sales','IT');

标签:多表,name,employees,练习,location,MySQL,department,id,SELECT
From: https://www.cnblogs.com/zhishu/p/16799834.html

相关文章

  • python学习记录9:代码雨效果(random库练习)源码
     importsysimportrandomimportpygamefrompygame.localsimport*#屏幕大小WIDTH=800HEIGHT=600#下落速度范围SPEED=[15,30]#字母大小范围SIZ......
  • 【图形学】计算机图形学-练习题6
    【图形学】计算机图形学-练习题6​​一、题目一​​​​1.1作业题目​​​​1.2作业解答​​​​二、题目二​​​​2.1作业题目​​​​2.2作业解答​​一、题目一1.1......
  • MYSQL锁表查询
    --第一个会话执行droptableifexiststest1;createtabletest1(idintnotnull,nameint,primarykey(id),uniquekey(name))engine=......
  • mysql安装时经常遇到的Access denied for user 'root'@XXXXXXX
    经常我们安装了mysql后,本地(安装mysql的服务器上)通过shell的方式可以登录,但是无法通过工具远程连接,这时候可能是因为mysql的初始的几个账号密码为空的原因;只需要修改下默认......
  • mysql 存储结构介绍及执行过程分析
    MySQL体系结构介绍 1mysql 的体系结构  MySQL整体的逻辑结构可以分为4层,客户层、服务层、存储引擎层、数据层客户层客户层:进行相关的连接处理、权限控制、......
  • JavaWeb(一):MySql基础
    目录​​1、数据库相关概念​​​​1.1数据库​​​​1.2数据库管理系统​​​​1.3常见的数据库管理系统​​​​1.4SQL​​​​2、MySQL​​​​2.1MySQL安装​​​......
  • ResultSet练习
    练习:定义一个方法,查询emp表的数据将其封装未对象,然后装载集合,返回1,定义emp类2,定义方法 publicList<Emp>findAll(){}3,实现方法 select* from emp......
  • 为什么用 Redis 作为 MySQL 的缓存?
    主要是因为 Redis具备「高性能」和「高并发」两种特性。1、Redis具备高性能假如用户第一次访问MySQL中的某些数据。这个过程会比较慢,因为是从硬盘上读取的。将该用......
  • 多表查询练习1和多表查询练习2,多表查询练习3
    创建表:--部门表CREATETABLEdept(idINTPRIMARYKEYPRIMARYKEY,--部门iddnameVARCHAR(50),--部门名称locVARCHAR(50)--部门所在地);--添......
  • mysql忘记密码怎么解决
    mysql忘记密码怎么解决1.检查mysql服务是否启动,如果启动,关闭mysql服务运行命令:ps-ef|grep-imysql  如果开着就运行关闭的命令:servicemysqldstop或者syst......