Mysql初学第三课

作业

1、各部门工资最高的员工(难度:中等)

创建 Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。创建 Department 表,包含公司所有部门的信息。
Mysql初学第三课

语句

读取每个部门收入最高的人,部门,以及工资

SELECT d.Name Department,
       e.Name Employee,
       e.Salary 
FROM department as d,
     employee as e ,
     (SELECT MAX(Salary) salary,
                     Departmentid id 
      FROM Employee 
      GROUP BY Departmentid) 
      as a 
WHERE d.Id=e.Departmentid 
  and a.id=e.Departmentid 
  and a.salary=e.Salary;

Mysql初学第三课

2小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。

Mysql初学第三课

语句

select (case 
        when id%2=1 
        and id!=(select max(id) 
                 from seat) 
        then id+1 
        when id%2=0 
        then id-1 
        else id 
        end) as id, 
        student 
from seat 
order by id;

Mysql初学第三课

3分数排名(难度:中等)

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
Mysql初学第三课

语句

 select s.Score,
        (select count(distinct Score) 
        from scores 
        where Score>=s.Score) Rank 
 from scores as s 
 order by s.Score desc;

Mysql初学第三课

select s.Score,
       (select count(*)+1 
       from scores 
       where Score>s.Score) Rank 
from scores as s 
order by s.Score desc;

Mysql初学第三课

其他作业

1)各部门前3高工资的员工
根据分组无法找到前3收入最高的工资,学习其他同学的作业,是三个表综合查询,能够请老师分步骤写清楚每个查询,以及最后结果。
2)行程和用户
可以分别建立各自的表,最后查询读题不懂,请老师说明分析步骤,以及核心思想。