- 求每门课的最高分同学的数据
select t1.学号,t1.课程号,t1.成绩
from table t1
where t1.成绩 = (
select max(t2.成绩)
from table t2
where t2.课程号 = t1.课程号
group by t2.课程号
)
- 求每个部门薪水前三名同学的数据
select *
from table a left join table b
on a.id=b.id and a.salary<b.salary
group by a.id
having count(distinct b.salary)<=2;
- 连接
笛卡尔积:m * n表格
自然连接natural join :自动同属性同值的连接
内连接inner join:类似自然连接
外连接:左连右补,右连左补,必须用using/on指明连接的字段