美文网首页
SQL练习题-2

SQL练习题-2

作者: AoEliauk | 来源:发表于2020-05-08 11:28 被阅读0次

员工表结构:


部门表结构:

部门员工表结构:

部门经理表结构:

薪资表结构:

1.获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'。结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。

分析:join连接两表,限制条件dept_no要相等,而两表emp_no不相等,两表to_date='9999-01-01'

select de.emp_no, dm.emp_no as manager_no from dept_emp as  de inner join dept_manager dm 

on de.dept_no=dm.dept_no 

where de.to_date='9999-01-01' 

and dm.to_date='9999-01-01'

 and de.emp_no<>dm.emp_no;

2.获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary

分析:对部门group by分组,max()函数找到salary最大值,限定条件to_date='9999-01-01'

select d.dept_no, s.emp_no,max(salary) from dept_emp d, salaries s 

where d.emp_no=s.emp_no 

and d.to_date=s.to_date 

and s.to_date='9999-01-01' 

group by d.dept_no;

3.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。

titles表结构:

分析:group by分组,count() 函数记录行数,having过滤条件。

select title,count(*) t from titles group by title having count(*)>=2;

4.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。注意对于重复的emp_no进行忽略。

分析:要忽略重复的emp_no,先对title分组,然后count(distinct emp_no)可以统计同一title下不同员工的个数

select title,count(distinct emp_no) t from titles group by title having t>=2;

5.查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列

分析: 使用where过滤条件, 奇数判断可用emp_no%2=1,

select * from employees where emp%2=1 and last_name<>Mary order by hire_date desc;

6.统计出当前各个title类型对应的员工当前(to_date='9999-01-01')薪水对应的平均工资。结果给出title以及平均工资avg。

分析:联合查询,限定条件t.emp_no=s.emp_no,限制当前(9999-01-01)条件,对title进行分组,avg()函数求平均值

select t.title, avg(salary) avg from titles t,salaries s 

where t.emp_no=s.emp_no 

and t.to_date=s.to_date 

and s.to_date='9999-01-01' 

group by t.title; 

7.获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary

分析:要考虑到多人同一薪资的情况,对薪水分组降序排序,用limit m,n 查询第二条记录

select emp_no,salary from salaries where to_date='9999-01-01' and salary=(select salary from salaries group by salary order by salary desc limit1,1);

8.查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name

分析:max()函数找到第二高的薪水,连接两表,限制条件为e.emp_no=s.emp_no,to_date='9999-01-01'

select e.emp_no,s.salary,e.last_name,e.first_name from employees e join salaries s 

on e.emp_no=s.emp_no and s.salary=

(select max(salary) from salaries  where salary < 

(select max(salary) from salaries where to_date='9999-01-01'01') and to_date='9999-01-01') 

and e.to_date='9999-01-01';

9.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工

部门表结构:

分析:“员工表”、“部门员工表”和“部门表”,用left join连接employees和dept_emp两表,再用left join连接departments表,最后得到想要的结果。

select e.last_name,e.first_name,d.dept_name from employees e

left join dept_emp de on e.emp_no = de.emp_no

left join departments d on de.dept_no = d.dept_no;

10.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth

分析:分别找出该员工入职时的薪水及最后一次的薪水,然后做差,别名为growth

select (

(select salary from salaries where emp_no='10001' order by to_date desc limit 1)-

(select salary from salaries where emp_no='10001' order by to_date asc limit 1)

) as growth;

相关文章

  • SQL练习题(2)

    SQL练习 题目均取自sqlzoo, 在此只写下自己的答案。 4. SELECT within SELECT ...

  • SQL练习题-2

    员工表结构: 部门表结构: 部门员工表结构: 部门经理表结构: 薪资表结构: 1.获取所有员工当前的manager...

  • SQL练习题2

    接上第一篇SQL之练习题,详细数据表见上篇文章。 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 ...

  • Mysql-Sql练习

    Sql练习题 1.表和数据的准备 员工表 部门表 工资等级表 2.练习题目 查询出部门编号为30的所有员工的编号和...

  • 视图与子查询

    《SQL基础教程第2版》Chap.5练习题 5-2 向视图 ViewPractice5_1 插入数据. ❓为什么S...

  • MySQL 练习题

    01第一天20180703 【SQL练习】经典SQL练习题 02第二天20180704 sql语句练习50题(My...

  • bWAPP学习笔记 - A1 Injection (二)

    SQL Injection (GET/Search) 手注练习题 (^_^) Level: Low 先输入单引号'...

  • 2018-12-01

    练习题1 练习题2 练习题3

  • LeetCode-SQL-nine

    Leetcode-sql-nine 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

  • LeetCode-SQL-five

    LeetCode-SQL-five 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

网友评论

      本文标题:SQL练习题-2

      本文链接:https://www.haomeiwen.com/subject/iwlzghtx.html