数据库练习 | 面试题

数据库练习题 | 面试题 学生课程管理系统

环境:我这用的是MySQL 也可以用SQL Server这些,只不过建表语句和某些SQL语句语法不一样

代码及答案如下:

  1 创建表和输入数据
  2 CREATE DATABASE Test1;
  3 USE Test1;
  4 学生表(学号,姓名,性别,生日,班级)
  5 CREATE TABLE STUDENT
  6 (SNO VARCHAR(3) NOT NULL, 
  7    SNAME VARCHAR(4) NOT NULL,
  8    SSEX VARCHAR(2) NOT NULL, 
  9    SBIRTHDAY DATETIME,
 10    CLASS VARCHAR(5));
 11 
 12 
 13 课程(课程号,姓名,老师编号)
 14 CREATE TABLE COURSE
 15 (  CNO VARCHAR(5) NOT NULL, 
 16    CNAME VARCHAR(10) NOT NULL, 
 17    TNO VARCHAR(10) NOT NULL);
 18 
 19 
 20 分数(学生编号,课程编号,分数)
 21 CREATE TABLE SCORE 
 22 (  SNO VARCHAR(3) NOT NULL, 
 23    CNO VARCHAR(5) NOT NULL, 
 24    DEGREE NUMERIC(10, 1) NOT NULL) 
 25 
 26 
 27 老师(老师编号,老师姓名,生日,系名)
 28 CREATE TABLE TEACHER 
 29 (TNO VARCHAR(3) NOT NULL, 
 30    TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL, 
 31    TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6), 
 32    DEPART VARCHAR(10) NOT NULL)
 33 DELETE FROM Student;
 34 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'' ,'1977-09-01','95033');
 35 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'' ,'1975-10-02','95031');
 36 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'' ,'1976-01-23','95033');
 37 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'' ,'1976-02-20','95033');
 38 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'' ,'1975-02-10','95031');
 39 INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'' ,'1974-06-03','95031');
 40 
 41 INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论','825')
 42 INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,'804');
 43 INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,'856');
 44 INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,'100');
 45 
 46 
 47 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('103','3-245',86);
 48 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('105','3-245',75);
 49 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('109','3-245',68);
 50 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('103','3-105',92);
 51 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('105','3-105',88);
 52 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('109','3-105',76);
 53 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('101','3-105',64);
 54 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('107','3-105',91);
 55 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('108','3-105',78);
 56 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('101','6-166',85);
 57 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('107','6-106',79);
 58 INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES ('108','6-166',81);
 59 
 60 INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) 
 61 VALUES ('804','李诚','','1958-12-02','副教授','计算机系');
 62 INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) 
 63 VALUES ('856','张旭','','1969-03-12','讲师','电子工程系');
 64 INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
 65 VALUES ('825','王萍','','1972-05-05','助教','计算机系');
 66 INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) 
 67 VALUES ('831','刘冰','','1977-08-14','助教','电子工程系');
 68 
 69 
 70 
 71 # 练习题目
 72 
 73 #  1、 查询Student表中的所有记录的Sname、Ssex和Class列。
 74 SELECT SName,SSex,Class
 75 FROM Student;
 76 
 77 #  2、 查询教师所有的单位即不重复的Depart列。
 78 SELECT Depart
 79 FROM Teacher 
 80 GROUP BY Depart
 81   
 82 
 83 #  3、 查询Student表的所有记录。
 84  SELECT * 
 85  FROM Student;
 86  
 87 #  4、 查询Score表中成绩在60到80之间的所有记录。
 88 SELECT *
 89 FROM Score
 90 WHERE Degree BETWEEN 60 AND 80;
 91 #  5、 查询Score表中成绩为85,86或88的记录。
 92 SELECT * 
 93 FROM Score
 94 WHERE Degree IN (85,86,88);
 95 
 96 #  6、 查询Student表中“95031”班或性别为“女”的同学记录。
 97 SELECT *
 98 FROM Student
 99 WHERE Class='95031' OR Ssex='';
100 
101 #  7、 以Class降序查询Student表的所有记录
102 SELECT * 
103 FROM Student
104 ORDER BY Class DESC;
105        
106 #  8、 以Cno升序、Degree降序查询Score表的所有记录。
107 SELECT *
108 FROM Score
109 ORDER BY Cno,Degree DESC;
110 
111 #  9、 查询“95031”班的学生人数。
112 SELECT COUNT(*) AS 人数
113 FROM Student
114 WHERE Class='95031';
115 
116 
117 #  10、查询Score表中的最高分的学生学号和课程号。
118 SELECT Sno,Cno
119 FROM Score
120 ORDER BY Degree DESC
121 LIMIT 1;
122   
123 #  11、查询‘3-105’号课程的平均分。
124 SELECT AVG(Degree)
125 FROM Score
126 WHERE Cno='3-105';
127     
128 #  12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
129 SELECT AVG(Degree)
130 FROM Score
131 WHERE Cno LIKE '3%'
132 HAVING COUNT(*)>5;
133     
134 #  13、查询最低分大于70,最高分小于90的Sno列。
135 SELECT Sno
136 FROM Score
137 GROUP BY Sno
138 HAVING MIN(Degree)>70 AND MAX(Degree)<90;
139  
140 #  14、查询所有学生的Sname、Cno和Degree列。
141 SELECT SName,Cno,Degree
142 FROM Student stu 
143 INNER JOIN Score sco ON stu.Sno=sco.Sno 
144 
145 #  15、查询所有学生的Sno、Cname和Degree列。
146 SELECT Sno,CName,Degree
147 FROM Score s
148 INNER JOIN Course c ON s.Cno=c.Cno;
149 
150 
151 #  16、查询所有学生的Sname、Cname和Degree列。
152 SELECT SName,CName,Degree
153 FROM Score s
154 INNER JOIN Student stu ON s.Sno=stu.Sno
155 INNER JOIN Course c ON s.Cno = c.Cno
156 
157 #  17、查询“95033”班所选课程的平均分。
158  SELECT AVG(Degree)
159  FROM Score
160  WHERE Sno IN(
161    SELECT Sno 
162    FROM Student WHERE Class='95033'
163  );
164 
165 #  18、假设使用如下命令建立了一个grade表:
166 grade表将分数和对应等级进行比对
167     create TABLE grade(
168     low   INT,
169     upp   INT,
170     rank  CHAR(1)
171 );
172     
173 INSERT INTO grade VALUES(90,100,'A');
174 INSERT INTO grade VALUES(80,89,'B');
175 INSERT INTO grade VALUES(70,79,'C');
176 INSERT INTO grade VALUES(60,69,'D');
177 INSERT INTO grade VALUES(0,59,'E');
178 
179 #现查询所有同学的Sno、Cno和rank列。
180 SELECT Sno,Cno,Rank AS 等级
181 FROM Score 
182 INNER JOIN Grade ON Score.`DEGREE` BETWEEN Grade.`low` AND Grade.`upp`
183  
184 
185 #  19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
186 SELECT s1.*
187 FROM Score s1,Score s2
188 WHERE s1.Cno='3-105' AND s2.Cno='3-105' AND s2.Sno='109' AND s1.Degree>s2.`DEGREE`
189 
190 #  20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
191 SELECT *
192 FROM Score s
193 WHERE Degree NOT IN(
194   SELECT MAX(Degree) FROM Score WHERE s.Sno=Score.Sno
195 );
196 
197 #  21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
198 SELECT *
199 FROM Score
200 WHERE Degree>(
201   SELECT Degree 
202   FROM Score WHERE Sno='109' AND Cno='3-105'
203 );    
204 #  22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列
205 SELECT Sno,Sname,Sbirthday
206 FROM Student
207 WHERE YEAR(Sbirthday)=(
208   SELECT YEAR(Sbirthday)
209   FROM Student WHERE Sno='108'
210 );
211 
212 #  23、查询“张旭“教师任课的学生成绩。
213 SELECT *
214 FROM Score
215 WHERE Cno IN (
216   SELECT Cno 
217   FROM Course WHERE Tno = (
218     SELECT Tno 
219     FROM Teacher WHERE TName='张旭'  
220   )
221 );
222  
223 #  24、查询选修某课程的同学人数多于5人的教师姓名。
224 SELECT TName
225 FROM Teacher t
226 INNER JOIN Course c ON t.`TNO`=c.`TNO`
227 INNER JOIN Score s ON c.`CNO`=s.`CNO`
228 GROUP BY t.`TNO`
229 HAVING COUNT(*)>5
230 
231 #  25、查询95033班和95031班全体学生的记录。
232 SELECT *
233 FROM Student
234 WHERE Class='95033' OR Class='95031';
235 
236 #  26、查询存在有85分以上成绩的课程Cno.
237 SELECT Cno 
238 FROM Score
239 WHERE Degree>85
240 GROUP BY Cno
241 
242 #  27、查询出“计算机系“教师所教课程的成绩表。
243 SELECT *
244 FROM Score
245 WHERE Cno IN(
246   SELECT Cno 
247   FROM Course WHERE Tno IN (
248     SELECT Tno 
249     FROM Teacher WHERE Depart='计算机系'
250   )
251 );
252 
253 
254 #  28、查询“计算机系”与“电子工程系”不同职称的教师的Tname和Prof。
255  SELECT *
256  FROM Teacher
257  WHERE Depart='计算机系' OR Depart='电子工程系'
258 
259 #  29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
260 SELECT s1.*
261 FROM Score s1,Score s2
262 WHERE s1.Cno='3-105' AND s2.Cno='3-245' AND s1.Degree>s2.Degree
263 GROUP BY s1.Sno
264 ORDER BY Degree DESC;
265 
266 #  30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree。
267 SELECT s1.Cno,s1.Sno,s1.Degree
268 FROM Score s1
269 INNER JOIN Score s2 ON s1.Sno=s2.Sno
270 WHERE s1.Cno='3-105' AND s2.Cno='3-245' AND s1.Degree>s2.Degree
271 
272 #  31、查询所有教师和同学的name、sex和birthday.
273 SELECT Sname,Ssex,Sbirthday FROM Student
274 UNION 
275 SELECT Tname,Tsex,Tbirthday FROM Teacher;
276     
277 #  32、查询所有“女”教师和“女”同学的name、sex和birthday.
278 SELECT Sname,Ssex,Sbirthday
279 FROM Student WHERE sSex=''
280 UNION 
281 SELECT Tname,Tsex,Tbirthday 
282 FROM Teacher WHERE tSex='';
283 
284 #  33、查询成绩比该课程平均成绩低的同学的成绩表。
285 SELECT s1.*
286 FROM Score s1 
287 INNER JOIN (
288   SELECT Cno,AVG(Degree) AS avg_score
289   FROM Score GROUP BY Cno
290 ) AS s2 ON s1.Cno=s2.Cno AND s1.`DEGREE`<s2.avg_score
291 
292 
293  
294 #  34、查询所有任课教师的Tname和Depart.
295 SELECT Tname,Depart
296 FROM Teacher
297 WHERE Tno IN (
298   SELECT Tno FROM Course GROUP BY Tno
299 );
300 #  35、查询所有未讲课的教师的Tname和Depart.
301 SELECT Tname,Depart
302 FROM Teacher
303 WHERE Tno NOT IN (
304   SELECT Tno FROM Course GROUP BY Tno
305 );
306 
307 SELECT Tname,Depart
308 FROM Teacher
309 WHERE NOT EXISTS (
310   SELECT * FROM Course WHERE Teacher.Tno=Course.Tno
311 );
312 #  36、查询至少有2名男生的班号。
313 SELECT Class
314 FROM Student
315 WHERE sSex=''
316 GROUP BY Class
317 HAVING COUNT(*)>1
318 
319 #  37、查询Student表中不姓“王”的同学记录。
320 SELECT *
321 FROM Student
322 WHERE sName NOT LIKE '王%';
323     
324 #  38、查询Student表中每个学生的姓名和年龄。
325 SELECT sName AS 姓名,(YEAR(NOW())-YEAR(SBIRTHDAY)) AS 年龄
326 FROM Student
327 
328 #  39、查询Student表中最大和最小的Sbirthday日期值。
329 SELECT MIN(Sbirthday) AS 最小日期,MAX(Sbirthday) AS 最大日期
330 FROM Student
331 
332 #  40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
333 SELECT *
334 FROM Student
335 ORDER BY Class DESC,(YEAR(NOW())-YEAR(SBIRTHDAY)) DESC
336 
337 #  41、查询“男”教师及其所上的课程。
338 SELECT Cno,Cname
339 FROM Course
340 WHERE Tno IN (
341   SELECT Tno
342   FROM Teacher WHERE Tsex=''
343 );
344 
345 #  42、查询最高分同学的Sno、Cno和Degree列。
346 SELECT Sno,CNo,Degree
347 FROM Score
348 ORDER BY Degree DESC
349 LIMIT 1;
350 
351 #  43、查询和“李军”同性别的所有同学的Sname.
352 SELECT Sname
353 FROM Student
354 WHERE Ssex=(
355   SELECT Ssex
356   FROM Student WHERE SName='李军'
357 ); 
358 
359 #  44、查询和“李军”同性别并同班的同学Sname.
360 SELECT Sname
361 FROM Student
362 WHERE Ssex=(
363   SELECT Ssex
364   FROM Student WHERE SName='李军'
365 ) AND Class=(
366   SELECT Class
367   FROM Student WHERE SName='李军'
368 ); 
369 
370 SELECT stu1.SName
371 FROM Student stu1 
372 INNER JOIN Student stu2 ON stu2.`SNAME`='李军'
373 WHERE stu1.`SSEX`=stu2.`SSEX` AND stu1.`CLASS`=stu2.`CLASS`
374 
375 #  45、查询所有选修“计算机导论”课程的“男”同学的成绩表
376 SELECT *
377 FROM Score
378 WHERE Cno = (
379   SELECT Cno 
380   FROM Course WHERE CName='计算机导论'
381 ) AND Sno IN (
382   SELECT Sno 
383   FROM Student WHERE Ssex=''
384 );
数据库练习题及答案

 

你可能感兴趣的:(数据库练习 | 面试题)