Repository navigation
Expand file tree
/
Copy pathT50.sql
More file actions
98 lines (73 loc) · 2.13 KB
/
Copy pathT50.sql
File metadata and controls
98 lines (73 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
sql复习,50题;https://zhuanlan.zhihu.com/p/38354000
/*
分析思路
先分解题目:
1)[两门以上][不及格课程]限制条件
2)[同学的学号及其平均成绩],也就是每个学生的平均成绩,显示学号,平均成绩
分析过程:
第1步:得到每个学生的平均成绩,显示学号,平均成绩
第2步:再加上限制条件:
/*
第1步:得到每个学生的平均成绩,显示学号,平均成绩
select 查询结果 [学号,平均成绩:汇总函数avg(成绩)]
from 从哪张表中查找数据 [涉及到成绩:成绩表score]
where 查询条件 [没有]
group by 分组 [每个学生的平均:按学号分组]
having 对分组结果指定条件 [没有]
order by 对查询结果排序[没有];
*/
select 学号, avg(成绩) as 平均成绩
from score
group by 学号;
select id, count(xxxxx)
from score
group by id
having count(xx) >=2;
查询所有课程成绩小于60分学生的学号、姓名
select 学号,姓名
from student
where 学号
in (
select 学号
from student
where 成绩 < 60
);
查询没有学全所有课的学生的学号、姓名|
select 学号,姓名
from student
where 学号 in(
select 学号
from score
group by 学号
having count(课程号) < (select count(课程号) from course)
);
查询出只选修了两门课程的全部学生的学号和姓名|
select 学号,姓名
from student
where 学号 in(
select 学号
from score
group by 学号
having count(课程号)=2
);
topN sql
关联子查询来实现
成绩最大值所在行的数据。
select *
from score as a
where 成绩 = (
select max(成绩)
from score as b
where b.课程号 = a.课程号);
查询各科成绩前两名的记录
-- union all, 增加列;
(select * from score where 课程号 = '0001' order by 成绩 desc limit 2)
union all
(select * from score where 课程号 = '0002' order by 成绩 desc limit 2)
union all
(select * from score where 课程号 = '0003' order by 成绩 desc limit 2);
--多表查询
select a.学号,a.姓名,count(b.课程号) as 选课数,sum(b.成绩) as 总成绩
from student as a left join score as b
on a.学号 = b.学号
group by a.学号;