SQL练习之简单问题2(有答案)

题目:设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

表(提示:在做题目不影响的条件下可以对自己不熟悉的数据类型自己可以适当的做调整。):

 

 

表中的数据:

 

 

 

 

查询问题:

  1 1、 查询Student表中的所有记录的Sname、Ssex和Class列。
  2 
  3 select Sname,Ssex,Class from student;
  4 
  5 2、 查询教师所有的单位即不重复的Depart列。
  6 
  7 select DISTINCT Depart from teacher;
  8 
  9 3、 查询Student表的所有记录。
 10 
 11 select * from student;
 12 
 13 4、 查询Score表中成绩在60到80之间的所有记录。
 14 
 15 select * from score where Degree between 60 and 80;
 16 
 17 5、 查询Score表中成绩为85,86或88的记录。
 18 
 19 select * from score where Degree in(85,86,88);
 20 
 21 6、 查询Student表中“95031”班或性别为“女”的同学记录。
 22 
 23 select * from student where Class='95031' or Ssex='';
 24 
 25 7、 以Class降序查询Student表的所有记录。
 26 
 27 select * from student order by Class desc;
 28 
 29 8、 以Cno升序、Degree降序查询Score表的所有记录。
 30 
 31 select * from score order by Cno,Degree desc;
 32 
 33 9、 查询“95031”班的学生人数。
 34 
 35 select count(*) from student where Class='95031';
 36 
 37 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
 38 
 39 select Sno,Cno from score order by Degree desc limit 1;
 40 
 41 select Sno,Cno from score where Degree=(select max(Degree) from score);
 42 
 43 11、 查询每门课的平均成绩。
 44 
 45 select Cno,avg(Degree) Degree from score group by Cno;
 46 
 47 select
 48 
 49 c.Cname,
 50 
 51 s.degree
 52 
 53 from course c
 54 
 55 join (select s1.Cno cno,avg(s1.Degree) degree from score s1 group by Cno) s
 56 
 57 on c.Cno=s.cno;
 58 
 59 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 60 
 61 select Cno,avg(Degree) Degree from score where Cno like '3%' group by Cno having count(*)>4;
 62 
 63 select
 64 
 65 s1.Cno Cno,
 66 
 67 s1.a Degree
 68 
 69 from (select Cno,avg(Degree) a,count(*) c from score where Cno like '3%' group by Cno) s1
 70 
 71 where s1.c > 4;
 72 
 73 13、查询分数大于70,小于90的Sno列。
 74 
 75 select Sno from score where Degree between 70 and 90;
 76 
 77 select Sno from score where Degree > 70 and Degree < 90;
 78 
 79 14、查询所有学生的Sname、Cno和Degree列。
 80 
 81 select s1.Sname,s2.Cno,s2.Degree from student s1 join score s2 on s1.Sno=s2.Sno;
 82 
 83 15、查询所有学生的Sno、Cname和Degree列。
 84 
 85 select s2.Sno,c1.Cname,s2.Degree from course c1 join score s2 on c1.Cno=s2.Cno;
 86 
 87 16、查询所有学生的Sname、Cname和Degree列
 88 
 89 select
 90 
 91 s1.Sname,
 92 
 93 c1.Cname,
 94 
 95 s2.Degree
 96 
 97 from score s2
 98 
 99 join student s1
100 
101 on s2.Sno = s1.Sno
102 
103 join course c1
104 
105 on s2.Cno = c1.Cno;
106 
107 17、 查询“95033”班学生的平均分。
108 
109 select avg(s1.Degree) from score s1 join student s2 on s2.Class = '95033' where s1.Sno = s2.Sno;
110 
111 18、 假设使用如下命令建立了一个grade表:
112 
113 create table grade(low  int(3),upp  int(3),rank  char(1))
114 
115 insert into grade values(90,100,’A’)
116 
117 insert into grade values(80,89,’B’)
118 
119 insert into grade values(70,79,’C’)
120 
121 insert into grade values(60,69,’D’)
122 
123 insert into grade values(0,59,’E’)
124 
125 现查询所有同学的Sno、Cno和rank列。
126 
127 select s1.Sno,s1.Cno,g1.rank from score s1,grade g1 where s1.Degree between g1.low and g1.upp;
128 
129 select distinct s1.Sno,s1.Cno,s2.rank from score s1
130 
131 join (select * from grade)s2
132 
133 on s1.Degree between s2.low and s2.upp;
134 
135 19、  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
136 
137 select * from score where Cno='3-105' and Degree > (select Degree from score where Sno='109' and Cno='3-105');
138 
139 20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
140 
141 select s1.Sno,s1.Cno,s1.Degree from score s1
142 
143 join (select Sno,max(Degree) Degree from score group by Sno having count(*)>1)s2
144 
145 on s1.Sno=s2.Sno and s1.Degree<>s2.Degree;
146 
147 21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
148 
149 select * from score where Degree > (select Degree from score where Sno='109' and Cno='3-105');
150 
151 22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
152 
153 select s1.Sno,s1.Sname,s1.Sbirthday
154 
155 from student s1
156 
157 join (select left(Sbirthday,4) birthday from student where Sno='108')s2
158 
159 on left(s1.Sbirthday,4)=s2.birthday and s1.Sno<>'108';
160 
161 23、查询“张旭“教师任课的学生成绩。
162 
163 select s1.Degree
164 
165 from course c1
166 
167 join (select Tno from teacher where Tname='张旭') t1
168 
169 on c1.Tno=t1.Tno
170 
171 join score s1
172 
173 on s1.Cno=c1.Cno;
174 
175 24、查询选修某课程的同学人数多于5人的教师姓名。
176 
177 select t1.Tname
178 
179 from course c1
180 
181 join (select Cno from score group by Cno having count(*) > 5) s1
182 
183 on c1.Cno=s1.Cno
184 
185 join teacher t1
186 
187 on c1.Tno=t1.Tno;
188 
189 25、查询95033班和95031班全体学生的记录。
190 
191 select * from student where Class in('95033','95031');
192 
193 26、  查询存在有85分以上成绩的课程Cno.
194 
195 select Cno from score where Degree >= 85;
196 
197 27、查询出“计算机系“教师所教课程的成绩表。
198 
199 select s1.Degree
200 
201 from course c1
202 
203 join (select Tno from teacher where Depart='计算机系') t1
204 
205 on c1.Tno=t1.Tno
206 
207 join score s1
208 
209 on s1.Cno=c1.Cno;
210 
211 28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
212 
213 select Tname,Prof from teacher where Depart in('计算机系','电子工程系');
214 
215 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
216 
217 select Cno,Sno from score where Cno='3-105' and Degree >
218 
219 (
220 
221 select max(Degree) from score where Cno='3-245'
222 
223 )
224 
225 order by Degree desc;
226 
227 30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
228 
229 select * from score where Cno='3-105' and Degree >
230 
231 (
232 
233 select max(Degree) from score where Cno='3-245'
234 
235 )
236 
237 31、 查询所有教师和同学的name、sex和birthday.
238 
239 select
240 
241 distinct
242 
243 t.Tname,
244 
245 t.Tsex,
246 
247 t.Tbirthday,
248 
249 s.Sname,
250 
251 s.Ssex,
252 
253 s.Sbirthday
254 
255 from teacher t
256 
257 inner join student s
258 
259 on true;
260 
261 32、查询所有“女”教师和“女”同学的name、sex和birthday.
262 
263 select
264 
265 distinct
266 
267 t.Tname,
268 
269 t.Tsex,
270 
271 t.Tbirthday,
272 
273 s.Sname,
274 
275 s.Ssex,
276 
277 s.Sbirthday
278 
279 from teacher t
280 
281 join student s
282 
283 on t.Tsex='' and s.Ssex=''
284 
285  
286 
287 select Tname,Tsex,Tbirthday from teacher where Tsex='';
288 
289 select Sname,Ssex,Sbirthday from student where Ssex='';
290 
291 33、 查询成绩比该课程平均成绩低的同学的成绩表。
292 
293 select DISTINCT s1.Sno,s1.Cno,s1.Degree from score s1
294 
295 join (select Cno,avg(Degree) Degree from score group by Cno) s2
296 
297 on s1.Cno=s2.Cno and s1.Degree<s2.Degree
298 
299 34、 查询所有任课教师的Tname和Depart.
300 
301 select t.Tname,t.Depart
302 
303 from course c
304 
305 join (select Cno from score group by Cno)s
306 
307 on c.Cno=s.Cno
308 
309 join teacher t
310 
311 on t.Tno=c.Tno
312 
313 35 、 查询所有未讲课的教师的Tname和Depart.
314 
315 select t.Tname,t.Depart
316 
317 from course c
318 
319 join teacher t
320 
321 on c.Cno not in (select Cno from score group by Cno)
322 
323 and c.Tno=t.Tno
324 
325 36、查询至少有2名男生的班号。
326 
327 select Class from student where Ssex='' group by Class having count(*)>1
328 
329 37、查询Student表中不姓“王”的同学记录。
330 
331 select * from student where Sname not like '王%';
332 
333 38、查询Student表中每个学生的姓名和年龄。
334 
335 select Sname,Ssex from student;
336 
337 39、查询Student表中最大和最小的Sbirthday日期值。
338 
339 select max(Sbirthday),min(Sbirthday) from student;
340 
341 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
342 
343 select * from student order by Class desc,Ssex desc;
344 
345 41、查询“男”教师及其所上的课程。
346 
347 select c.Cname
348 
349 from course c
350 
351 join teacher t
352 
353 on t.Tsex='' and t.Tno=c.Tno
354 
355 select Cname from course where Tno
356 
357 in(select Tno from teacher where Tsex='')
358 
359 42、查询最高分同学的Sno、Cno和Degree列。
360 
361 select * from score where Degree=
362 
363 (select max(Degree) from score)
364 
365 43、查询和“李军”同性别的所有同学的Sname.
366 
367 select s1.Sname from student s1
368 
369 join (select * from student where Sname = '李军')s2
370 
371 on s1.Ssex=s2.Ssex and s1.Sname<>'李军';
372 
373 44、查询和“李军”同性别并同班的同学Sname.
374 
375 select s1.Sname from student s1
376 
377 join (select * from student where Sname = '李军')s2
378 
379 on s1.Ssex=s2.Ssex and s1.Sname<>'李军' and s1.Class=s2.Class;
380 
381 45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
382 
383 select s1.Sno,s1.Cno,s1.Degree from score s1
384 
385 join (select Cno from course where Cname='计算机导论') s2
386 
387 on s1.Cno=s2.Cno
388 
389 join (select Sno from student where Ssex='') s3
390 
391 on s1.Sno = s3.Sno; 

 

posted @ 2021-07-27 11:28  zheng_newbie  阅读(120)  评论(0编辑  收藏  举报