牛客网SQL试题库(中等)

SQL16 统计每个工种的员工平均工资

试题连接

select titles.title,AVG(salaries.salary)
from titles inner join salaries
on titles.emp_no = salaries.emp_no
group by titles.title
order by AVG(salaries.salary) asc

分析:

  • 使用内连接,连接两个表,成为新表
  • 再工种用分组,再计算每个组的 AVG(salary)

SQL19:查找员工的姓名以及部门名称

已知有三表:

  • 员工表(emp_no, emp_name)
  • 部门表(dept_no, dept_name)
  • 员工-部门表(emp_no, dept_no)

三表连接

select t1.last_name,t1.first_name,t3.dept_name
from employees as t1 left outer join dept_emp as t2
on t1.emp_no = t2.emp_no
left join departments as t3 
on t2.dept_no = t3.dept_no

SQL22:统计各部门的工资记录次数

三表查询
分组,计算每个部门的记录行数 count(1)

select t1.dept_no,t1.dept_name,count(1)
from departments as t1 left outer join dept_emp as t2
on t1.dept_no = t2.dept_no
left outer join salaries t3
on t2.emp_no = t3.emp_no
group by t1.dept_no
order by t1.dept_no asc

上一篇:MySQL存储过程


下一篇:Spring Cloud 入门