MySQL练习题

使用到的相关表
在这里插入图片描述


1.查询所有员工信息,查询员工编号,员工姓名,工资,职务名称,职务描述。

/*
分析:
	1.员工编号,员工姓名,工资,需要查询emp表。职务名称,职务描述 需要查询job表
	2.查询条件 emp。job_id = job.id;
*/
select
	t1.id, -- 员工编号
	t1.ename, -- 员工姓名
	t1.salary, -- 工资
	t2.jname, -- 职务名称,
	t2.description -- 职务描述
from
	emp t1,job t2
where
	t1.job_id = t2.id;


2.查询员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置

/*
分析:
	1.员工编号、员工姓名、工资 emp表。职务名称、职务描述 job表 部门名称 部门位置 salaries表
	2.条件:emp。job_id = job.id and emp.dept_id
*/
select
	t1.id, -- 员工编号
	t1.ename, -- 员工姓名
	t1.salary, -- 工资
	t2.jname, -- 职务名称
	t2.description, -- 职务描述
	t3.dname, -- 部门名称
	t3.loc -- 部门位置
from
	emp t1,job t2,dept t3
where
	t1.job_id = t2.id and t1.dept_id = t3.id;



3.查询员工姓名,工资,工资等级。

分析:
	1.员工姓名,工资 emp表  工资等级 salarygrade表
	2.条件 emp.salary between salarygrade.losalary and salarygrade.hisalary

select
	t1.name,
	t1.salary,
	t2.grade
from
	emp t1,salarygrade t2
where
	t1.salary between t2.losalary and t2 .hisalary;


4.查询员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级

分析:
	1.员工姓名 工资 emp 职务名称,职务描述 job 部门名称 部门位置 dept
select
	t1.ename,
	t1.salary,
	t2.jname,
	t2.description,
	t3.dname,
	t3.loc,
	t4.grade
from
	emp t1,job t2,dept t3,salarygrade t4
where
	t1.job_id = t2.id
	and t1.dept_id = t3.id
	and t1.salary between t4.losalary and t4.hisalary;


5.查询出部门编号,部门名称,部门位置,部门人数

分析
	1.部门编号,部门名称,部门位置 dept 部门人数 emp
	2.使用分组查询。按照emp。dept_id完成分组,查询count(id)
	3.使用子查询将第二步的查询结果和dept表进行关联查询
	
select
	t1.id,
	t1.dname,
	t1.loc,
	t2.total
from
	dept t1,
	(
        select
        	dept_id,count(id) total
        from
        	emp
        group by dept_id
    ) t2
where
	t1.id = t2.dept_id;


6.查询所有员工的姓名及其直接上级的姓名,没有领导的员工也要查询

分析
	1.姓名 emp,直接上级姓名 emp
		emp表的id和mgr是自关联
	2.条件 emp.id = emp.mgr
	
select
	t1.ename,
	t1.mgr,
	t2.id,
	t2,ename
from
	emp t1
left join emp t2
on t1.mgr = t2.id;
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值