MySQL练习题

MySQL的课后练习题

  • 一、基本的SELECT语句
    • 1. 查询员工12个月工资总和,并起别名为ANNUAL SALARY
    • 2. 查询employees表中去除重复的job_Id以后的数据
    • 3. 查询工资大于12000的员工姓名和工资
    • 4. 查询员工号为176的员工的姓名和部门号
    • 5. 显示表departments的结构并查询全部数据
  • 二、运算符
    • 1. 查询工资不在5000到12000的员工姓名和工资
    • 2. 查询在20或50号部门工作的员工姓名和部门号
    • 3. 查询公司中没有管理者的姓名员工及job_Id
    • 4. 查询公司中有奖金的员工姓名、工资及工资级别
    • 5. 查询员工第三个字母是a的员工姓名
    • 6. 查询姓名中有字母a和k的员工姓名
    • 7. 查询表employees中first_name以'e'结尾的员工信息
    • 8. 查询表employees部门编号在80到100之间的姓名、工种
    • 9. 查询表employees的manager_id是100,101,110的员工姓名、工资、管理者id
  • 三、排序与分页
    • 1. 查询员工的姓名、部门号、年薪,按照年薪降序、姓名升序排序
    • 2. 选择工资不在8000到17000的员工的姓名和工资,按照工资降序、显示第21到40位的数据
    • 3. 查询邮箱中包含e的员工信息,并按照邮箱的字节数降序、部门升序排序
  • 四、多表查询
    • 1. 显示所有员工的姓名、部门号和部门名称
    • 2. 查询90号部门员工的job_id和90号部门的location_id
    • 3.选择所有有奖金的员工的last_name、department_name、location_id、city
    • 4. 选择city在Toronto工作的员工的last_name、job_id、department_id、department_name
    • 5. 查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为'Executive'
    • 6. 查询员工的姓名,员工号,以及他的管理者的姓名和员工号
    • 7. 查询哪些部门没有员工
    • 8. 查询哪个城市没有部门
    • 9. 查询部门名为Sales或IT的员工信息
  • 五、单行函数
  • 六、多行函数
    • 1. where子句可否使用组函数进行过滤?NO
    • 2.查询公司员工工资的最大值、最小值、平均值、总和
    • 3.查询各job_id的员工工资的最大值、最小值、平均值、总和
    • 4.选择具有各个job_id的员工人数
    • 5.查询员工最高工资和最低工资的差距(DIFFERENCE)
    • 6.查询各个管理者手下员工的最低工资、其中最低工资不能低于6000, 没有管理者的员工不计算在内
    • 7.查询所有部门的名字、location_id、员工数量和平均工资,并按平均工资降序
    • 8.查询每个部门的部门名、部门包含的工种名和部门最低工资
  • 七、子查询
    • 1. 查询和Zlotkey相同部门的员工姓名和工资
    • 2. 查询工资比公司平均工资高的员工的员工号、姓名和工资
    • 3. 选择工资大于所有job_id= 'SA_MAN'的员工工资的员工的last_name、job_id、salary
    • 4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
    • 5. 查询在部门的location_id为1700的部门工作的员工的员工号
    • 6.查询管理者是King的员工姓名和工资
    • 7.查询工资最低的员工信息:last_name、salary
    • 8. 查询平均工资最低的部门信息
    • 9. 查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
    • 10. 查询平均工资最高的job信息
    • 11. 查询平均工资高于公司平均工资的部门有哪些?
    • 12. 查询出公司中所有manager的详细信息
    • 13. 各个部门中最高工资中最低的那个部门的最低工资是多少?
    • 14. 查询平均工资最高的部门的manager的详细信息:last_name、 department_id、email、salary
    • 15. 查询部门的部门号,其中不包括含有job_id是"ST_CLERK"的部门号
    • 16. 查询所有没有管理者的员工的last_name
    • 17. 查询员工号、姓名、雇用时间、工资,其中员工的管理者为'De Haan '
    • 18. 查询各部门中工资比本部门平均工资高的员工的员工号、姓名和工资(相关子查询)
    • 19. 查询每个部门下的部门人数大于5的部门名称(相关子查询)
    • 20. 查询每个国家下的部门个数大于2的国家编号(相关子查询)

一、基本的SELECT语句

1. 查询员工12个月工资总和,并起别名为ANNUAL SALARY

SELECT  employee_id, last_name, (salary * 12 * (1 + IFNULL(commission_pct, 0))) "ANNUAL SALARY"
FROM employees;

2. 查询employees表中去除重复的job_Id以后的数据

select distinct job_Id 
from employees; 

3. 查询工资大于12000的员工姓名和工资

select last_name, salary 
from employees 
where salary > 12000;

4. 查询员工号为176的员工的姓名和部门号

select last_name, department_id 
from employees 
where employee_id = 176;

5. 显示表departments的结构并查询全部数据

desc departments;
select * from departments;

二、运算符

1. 查询工资不在5000到12000的员工姓名和工资

select last_name, salary 
from employees 
where salary < 5000 or salary > 12000;
# 或者
select last_name, salary 
from employees 
where salary not between 5000 and 12000;

2. 查询在20或50号部门工作的员工姓名和部门号

select last_name, department_Id 
from employees 
where department_id = 20 or department_Id = 50;
# 或者
select last_name, department_id 
from employees 
where department_id in (20, 50);

3. 查询公司中没有管理者的姓名员工及job_Id

select last_name, job_Id, manager_id  
from employees 
where manager_id is null;
# 或者
select last_name, job_id, manager_id 
from employee 
where manager_id <=> null;

4. 查询公司中有奖金的员工姓名、工资及工资级别

select last_name, salary, commission_pcy 
from employees 
where commission_pct is not null;
# 或者
select last_name, salary, commission_pcy 
from employees 
where not commission_pct <=> null;

5. 查询员工第三个字母是a的员工姓名

select last_name 
from employees 
where last_name like '__a%';

6. 查询姓名中有字母a和k的员工姓名

select last_name 
from employees 
where last_name like '%a%kf%' or last_name like '%k%a%';
# 或者
select last_name 
from employees 
where last_name like '%a%' or last_name like '%k%';

7. 查询表employees中first_name以’e’结尾的员工信息

select first_name 
from employees 
where first_name like '%e';

8. 查询表employees部门编号在80到100之间的姓名、工种

select last_name, job_id 
from employees 
where department_id >= 80 and department_id <= 100;
# 或者
select last_name, job_id 
from employees 
where department_id between 80 and 100;

9. 查询表employees的manager_id是100,101,110的员工姓名、工资、管理者id

select last_name, salary, manager_id 
from employees 
where manager_id in (100, 101, 110);

三、排序与分页

1. 查询员工的姓名、部门号、年薪,按照年薪降序、姓名升序排序

select last_name, department_id, (salary * 12 * (1 + IFNULL(commission_pct, 0))) annual_salary 
from employees 
orderby annual_salary desc, last_name asc;

2. 选择工资不在8000到17000的员工的姓名和工资,按照工资降序、显示第21到40位的数据

select last_name 
from employees 
where salary not between 8000 and 17000
order by salary desc
limit 20, 20;

3. 查询邮箱中包含e的员工信息,并按照邮箱的字节数降序、部门升序排序

select * 
from employees
where email like '%e%'
order by length(email) desc, department_id asc;

四、多表查询

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

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

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

select e.job_id, d.locations_id
from employees e
join departments d
on e.department_id = d.department_id;
where e.department_id = 90;

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

select e.last_name, d.department_name, d.location_id, l.city
from employees e
left join departments d
on e.department_id = d.department_id
left join locations l
on d.location_id= l.location_id
where e.commission_pct is not null;

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

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

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

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

6. 查询员工的姓名,员工号,以及他的管理者的姓名和员工号

select e.last_name, e.employee_id, m.last_name manager_name, m.employee_Id manager_employee_id
from employees e
left join employees m
on e.manager_id = m.employee_id 

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

select d.department_id
from departments d
left  join employees e
on d.department_id = e.department_id
where e.department_id is null;

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

select l.city
from locations l
left join departments d
on l.location_id = d.locaion_Id
where d.location_id is null;

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

select e.last_name 
from employees e
join departments d
on e.department_id = d.department_id
where d.department_name in ('Sales', 'IT');

五、单行函数

六、多行函数

1. where子句可否使用组函数进行过滤?NO

2.查询公司员工工资的最大值、最小值、平均值、总和

select max(salary) max_sal, min(salary) min_sal, avg(salary) avg_sal, sum(salary) sum_sal
from employees;

3.查询各job_id的员工工资的最大值、最小值、平均值、总和

select job_id, max(salary) max_sal, min(salary) min_sal, avg(salary) avg_sal, sum(salary) sum_sal
from employees;
group by job_id

4.选择具有各个job_id的员工人数

select job_id, count(*)
from employees
group by job_id;

5.查询员工最高工资和最低工资的差距(DIFFERENCE)

select max(salary) - min(salary) "DIFFERENCE"
from empoyees;

6.查询各个管理者手下员工的最低工资、其中最低工资不能低于6000, 没有管理者的员工不计算在内

select manager_id, min(salary)
from employees
whrere manager_id is not null;
group by manager_id
having min(salary) >= 6000;

7.查询所有部门的名字、location_id、员工数量和平均工资,并按平均工资降序

select d.department_name, d.location_id, count(e.department_id), avg(salary) avg_salary
from departments d
left join employees e
on d.department_id = e.department_id
group by d.department_name, d.loation_id
order by avg_salary;

8.查询每个部门的部门名、部门包含的工种名和部门最低工资

select d.department_id, e.job_Id, min(e.salary) min_salary
from departments d
left join employees e
on d.department_id = e.department_id

七、子查询

1. 查询和Zlotkey相同部门的员工姓名和工资

select last_name, salary
from employees
where department_Id in (
				select department_Id
				from employees
				where last_name = 'Zlotkey'
);

2. 查询工资比公司平均工资高的员工的员工号、姓名和工资

select employee_id, last_name, salary
from employees
where salary > (
			select avg(salary)
			from employees;
);

3. 选择工资大于所有job_id= 'SA_MAN’的员工工资的员工的last_name、job_id、salary

select last_name, job_id, salary
from employees
where salary > (
			select max(salary)
			from employees
			where job_Id = 'SA_MAN';
);
# 或者
select last_name, job_id, salary
from employees
where salary >  ALL (
			select salary
			from employees
			where job_id = 'SA_MAN';
);

4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名

select employee_id, last_name
from employees
where department_id in (
			select distinct department_id 
			from employees
			where last_name like '%u%'
);

5. 查询在部门的location_id为1700的部门工作的员工的员工号

select employee_id
from employees
where department_id in (
				select department_id
				from departments
				where location_id = 1700
)

6.查询管理者是King的员工姓名和工资

select last_name, salary
from employees
where manager_id in (
				select employee_id
				from employees
				where last_name = 'King'
)

7.查询工资最低的员工信息:last_name、salary

select last_name, salary
from employees
where salary = (
			select min(salary)
			from employees
);

8. 查询平均工资最低的部门信息

select * 
from departments
where department_id = (
				select department_id
				from departments
				group by department_id
				having avg(salary) = ( 
							select min(avg_salary)
							from (
										select avg(salary) avg_salary
										from employees
										group by department_id
							) t_dept_avh_sal
				)
);
# 或者
select * 
from departments
where department_id = (
				select department_id
				from departments
				group by department_id
				having avg(salary) <= ALL (
									select avg(salary)
									from employees
									group by department_id
				)
);
# 或者
select * 
from departments
where department_id = (
				select department_id
				from departments
				group by department_id
				having avg(salary) = (
									select avg(salary) avg_salary
									from employees
									group by department_id
									order by avg_salary asc
									limit 0, 1
				)
);
# 或者
select d.*
from departments d
join (
	select department_Id, avg(salary) avg_salary
	fromemployees
	group by department_id
	order by avg_salary asc
	limit 0, 1
) t_dept_avg_sal
on d.department_id = t_dept_avg_sal.department_id;

9. 查询平均工资最低的部门信息和该部门的平均工资(相关子查询)

select d.* ,(select avg(salary) from employees where department_id = d.department_id) avg_salary
from departments d
where department_id = (
				select department_id
				from departments d
				group by department_id
				having avg(salary) = ( 
							select min(avg_salary)
							from (
										select avg(salary) avg_salary
										from employees
										group by department_id
							) t_dept_avh_sal
				)
);
# 或者
select d.* ,(select avg(salary) from employees where department_id = d.department_id) avg_salary
from departments d
where department_id = (
				select department_id
				from departments
				group by department_id
				having avg(salary) <= ALL (
									select avg(salary)
									from employees
									group by department_id
				)
);
# 或者
select d.* ,(select avg(salary) from employees where department_id = d.department_id) avg_salary
from departments d
where department_id = (
				select department_id
				from departments
				group by department_id
				having avg(salary) = (
									select avg(salary) avg_salary
									from employees
									group by department_id
									order by avg_salary asc
									limit 0, 1
				)
);
# 或者
select d.* ,(select avg(salary) from employees where department_id = d.department_id) avg_salary
from departments d
join (
	select department_Id, avg(salary) avg_salary
	fromemployees
	group by department_id
	order by avg_salary asc
	limit 0, 1
) t_dept_avg_sal
on d.department_id = t_dept_avg_sal.department_id;

10. 查询平均工资最高的job信息

select *
from jobs
where job_id = (
			select job_id
			from employees
			group by job_id
			having avg(salary) = (
							select max(avg_salary)
							from (
								select avg(salary) avg_salary
								from employees
								group by job_id
							) t_job_avg_sal
			)
);
# 或者
select *
from jobs
where job_id = (
			select job_id
			from employees
			group by job_id
			having avg(salary) >= ALL (
								select avg(salary) avg_salary
								from employees
								group by job_id
			)
);
# 或者
select *
from jobs
where job_id = (
			select job_id
			from employees
			group by job_id
			having avg(salary) = (
								select avg(salary) avg_salary
								from employees
								group by job_id
								order by avg_salary desc
								limit 0, 1
			)
);
# 或者
select j.*
from jobs j
join (
	select job_id, avg(salary) avg_salary
	from employees
	group by job_id
) t_job_avg_sal
on j.job_id = t_job_avg_sal.job_Id;

11. 查询平均工资高于公司平均工资的部门有哪些?

select department_id
from employees
where department_id is not null
group by department_id
having avg(salary) > (# 注意,having中不能用select里面设置的别名
			select avg(salary)
			from employees
);

12. 查询出公司中所有manager的详细信息

select distinct *
from employees m
join employees e
on m.employee_id = e.employee_id;

13. 各个部门中最高工资中最低的那个部门的最低工资是多少?

select min(salary)
from employees
where department_id = (
	select department_id
	from employees
	group by department_id
	having max(salary) = (
			select min(max_salary)
			from (
					select max(salary) max_salary
					from employees
					gorup by department_id
			) t_dept_max_salary
	)
);
# 或者
select min(salary)
from employees
where department_id = (
	select department_id
	from employees
	group by department_id
	having max(salary) <= ALL (
					select max(salary) max_salary
					from employees
					gorup by department_id
	)
);
# 或者
select min(salary)
from employees
where department_id = (
	select department_id
	from employees
	group by department_id
	having max(salary) =  (
					select max(salary) max_salary
					from employees
					gorup by department_id
					oreder by max_salary asc
					limit 0, 1
	)
);
# 或者
select min(salary)
from employees e
join(
	select department_id, max(salary) max_salary
	from employees
	gorup by department_id
	oreder by max_salary asc
	limit 0, 1
) t_dept_max_sal
on e.department_id = t_dept_max_sal.department_id;

14. 查询平均工资最高的部门的manager的详细信息:last_name、 department_id、email、salary

select last_name, department_id, email, salary
from employees
where manager_Id in (
			select distinct  manager_id
			from employees
			where department_id = (
						select department_id
						from employees
						group by department_id
						having avg(salary) = (
									select max(avg_salary)
									from (
										select department_id, avg(salary) avg_salary
										from employees
										group by department_id
									) t_dept_avg_salary
						)
			)
);
# 或者
select last_name, department_id, email, salary
from employees
where manager_Id in (
			select distinct manager_id
			from employees
			where department_id = (
						select department_id
						from employees
						group by department_id
						having avg(salary) = (
										select avg(salary) avg_salary
										from employees
										group by department_id
						)
			)
);
# 或者
select last_name, department_id, email, salary
from employees
where manager_id in (
			select distinct manager_id
			from employees
			where department_id = (
						select department_id
						from employees
						group by department_id
						having avg(salary) >= ALL (
										select department_id, avg(salary) avg_salary
										from employees
										group by department_id
										order by avg_salary desc
										limit 0,1
						)
			)
);
# 或者
select last_name, department_id, email, salary
from employees e
where manager_id in (
			select distinct  manager_id 
			from employees e
			join  (
							select department_id, avg(salary) avg_salary
							from employees
							group by department_id
							order by avg_salary desc
							limit 0,1
			) t_dept_avg_salary
			on e.department_id = t_dept_avg_salary.department_id
)

15. 查询部门的部门号,其中不包括含有job_id是"ST_CLERK"的部门号

select department_id
from departments
where department_id not in(
	select department_id
	from employees
	where job_Id = 'ST_CLERK'
);
# 或者
select department_id
from departments d
where not exists(
		select *
		from employees e
		where d.department_id = e.department_id
		and e.job_id = 'ST_CLERK'
);

16. 查询所有没有管理者的员工的last_name

select last_name
from employees emp
where not exits(
			select *
			from employees mgr
			emp.manager_id = mgr.employee_id
)

17. 查询员工号、姓名、雇用时间、工资,其中员工的管理者为’De Haan ’

select employee_id, last_name, hire_date,salary
from employees
where manager_id in (
	select employee_id
	from employees
	where last_name = 'De Haan'
);
# 或者
select e1.employee_id, e1.last_name, e1.hire_date, e1.salary
from employees e1
where exists(
		select *
		from employees e2
		where e1.manager_id = e2.employee_id
		and e2.last_name = 'De Haan'
);

18. 查询各部门中工资比本部门平均工资高的员工的员工号、姓名和工资(相关子查询)

# 非相关子查询写法
select employee_id, last_name, salary
from employees e
join(
	select department_id, avg(salary) avg_salary
	from employees
	gorup by department_id
) t_dept_avg_sal
on e.department_id = t_dept_avg_sal.department_id
where e.salary > t.dept_avg_sal.avg_salary;

# 相关子查询写法
select employee_id, last_name, salary
from empoyees e1
where salary > (
			select avg(salary)
			from employees e2
			where e2.department_id = e1.department_id
);

19. 查询每个部门下的部门人数大于5的部门名称(相关子查询)

select department_name
from deparments d
where  5 < (
			select count(*)
			from employee e
			where d.department_id = e.department_id
);

20. 查询每个国家下的部门个数大于2的国家编号(相关子查询)

select country_id
from locations l
where 2 < (
			select count(*)
			from departments d
			where l.location_id = d.location_id
);

你可能感兴趣的:(数据库之MySQL,mysql,数据库,sql)