mysql> select sno,cno from score where degree = (select max(degree) from score);
+-----+-------+
| sno | cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
mysql> select sno,cno from score order by degree desc limit 0,1;
+-----+-------+
| sno | cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
#查询每门课的平均成绩。
mysql> select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno | avg(degree) |
+-------+-------------+
| 3-105 | 81.5000 |
| 3-245 | 76.3333 |
| 6-166 | 81.6667 |
+-------+-------------+
3 rows in set (0.02 sec)
#查询score表中至少有5名学生选修的并以3开头的课程的平均分数。
mysql> select avg(degree) from score where cno like '3%' and cno in (select cno from score group by cno having count(*) >= 5);
+-------------+
| avg(degree) |
+-------------+
| 81.5000 |
+-------------+
1 row in set (0.01 sec)
# 查询每门课的平均成绩。
mysql> select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno | avg(degree) |
+-------+-------------+
| 3-105 | 81.5000 |
| 3-245 | 76.3333 |
| 6-166 | 81.6667 |
+-------+-------------+
3 rows in set (0.02 sec)
mysql> select avg(degree) from score where cno like '3%' and cno in (select cno from score group by cno having count(*) >= 5);
+-------------+
| avg(degree) |
+-------------+
| 81.5000 |
+-------------+
1 row in set (0.01 sec)
#查询所有学生的Sname、Cname和Degree列。
mysql> select sname,cname,degree from student join score on student.sno = score.sno join course on course.cno = score.cno;
+--------+-----------------+--------+
| sname | cname | degree |
+--------+-----------------+--------+
| 陆君 | 操作系统 | 86 |
| 匡明 | 操作系统 | 75 |
| 王芳 | 操作系统 | 68 |
| 陆君 | 计算机导论 | 92 |
| 匡明 | 计算机导论 | 88 |
| 王芳 | 计算机导论 | 76 |
| 陆君 | 计算机导论 | 64 |
| 匡明 | 计算机导论 | 91 |
| 王芳 | 计算机导论 | 78 |
| 陆君 | 数字电路 | 85 |
| 匡明 | 数字电路 | 79 |
| 王芳 | 数字电路 | 81 |
+--------+-----------------+--------+
12 rows in set (0.01 sec)
# 查询“95031”班学生的平均分
mysql> select avg(degree) as 'class = 95031' from score where sno in (select sno from student where class = '95031');
+---------------+
| class = 95031 |
+---------------+
| 80.2500 |
+---------------+
1 row in set (0.00 sec)
#查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
mysql> select * from score where cno = '3-105' and degree > (select max(degree) from score where sno = '109' and cno = '3-105');
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 105 | 3-105 | 91 |
+-----+-------+--------+
3 rows in set (0.02 sec)
#选了多门课程并且是这个课程下不是最高分的
mysql> select * from score a where sno in (select sno from score group by sno having count(*) > 1) and degree < (select max(degree) from score b where b.cno = a.cno);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 91 |
| 109 | 3-105 | 78 |
| 105 | 6-166 | 79 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
9 rows in set (0.02 sec)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
mysql> use 45exercise;
Database changed
#查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
mysql> select sno,sname,sbirthday from student where year(sbirthday) = (select year(sbirthday) from student where sno = '108');
+-----+--------+---------------------+
| sno | sname | sbirthday |
+-----+--------+---------------------+
| 108 | 曾华 | 1977-09-01 00:00:00 |
+-----+--------+---------------------+
1 row in set (0.00 sec)
#查询“张旭“教师任课的学生成绩。
mysql> select sno,degree from score,course where score.cno = course.cno and course.tno = (select tno from teacher where tname = '张旭');
+-----+--------+
| sno | degree |
+-----+--------+
| 103 | 85 |
| 105 | 79 |
| 109 | 81 |
+-----+--------+
3 rows in set (0.01 sec)
#查询选修某课程的同学人数多于5人的教师姓名。
mysql> select tname from teacher,course where teacher.tno = course.tno and course.cno = (select cno from score group by cno having count(*) > 5 );
+--------+
| tname |
+--------+
| 王萍 |
+--------+
1 row in set (0.00 sec)
#查询95033班和95031班全体学生的记录
mysql> select * from student where class in ('95033','95031');
mysql> select * from course where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系' ));
+-------+-----------------+-----+
| cno | cname | tno |
+-------+-----------------+-----+
| 3-245 | 操作系统 | 804 |
| 3-105 | 计算机导论 | 825 |
+-------+-----------------+-----+
2 rows in set (0.00 sec)
#查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
mysql> select tname, prof from teacher where depart = '计算机系' and prof in (select prof from teacher where depart = '电子工程系') union select tname,prof from teacher where depart = '电子工程系' and prof not in (select prof from teacher where depart = '计算机系');
mysql> select tname,cname from course,teacher where course.tno = teacher.tno and teacher.tsex = '男';
+--------+--------------+
| tname | cname |
+--------+--------------+
| 李诚 | 操作系统 |
| 张旭 | 数字电路 |
+--------+--------------+
2 rows in set (0.02 sec)
#查询最高分同学的Sno、Cno和Degree列。
mysql> select sno,cno,degree from score order by degree desc limit 0,1;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
+-----+-------+--------+
1 row in set (0.01 sec)
#查询和“李军”同性别的所有同学的Sname
mysql> select sname from student where ssex = (select ssex from student where sname = '李军');
+--------+
| sname |
+--------+
| 李军 |
| 陆君 |
| 匡明 |
| 曾华 |
+--------+
4 rows in set (0.00 sec)
#查询和“李军”同性别并同班的同学Sname.
mysql> select sname from student where ssex = (select ssex from student where sname = '李军') and class = (select class from student where sname = '李军');
+--------+
| sname |
+--------+
| 李军 |
| 曾华 |
+--------+
2 rows in set (0.01 sec)
#查询所有选修“计算机导论”课程的“男”同学的成绩表。
mysql> select sno,cno,degree from score where cno = (select cno from course where cname = '计算机导论') and sno in (select sno from student where ssex = '男');