首页 > 开发 > 综合 > 正文

SQL数据库面试题以及答案

2024-07-21 02:49:09
字体:
来源:转载
供稿:网友
SQL数据库面试题以及答案

Student(stuId,stuName,stuAge,stuSex)学生表

stuId:学号;stuName:学生姓名;stuAge:学生年龄;stuSex:学生性别

Course(courseId,courseName,teacherId)课程表

courseId,课程编号;courseName:课程名字;teacherId:教师编号

Scores(stuId,courseId,score)成绩表

stuId:学号;courseId,课程编号;score:成绩

Teacher(teacherId,teacherName)教师表

teacherId:教师编号;teacherName:教师名字

问题:

1、查询“001”课程比“002”课程成绩高的所有学生的学号;

selecta.stuIdfrom(selectstuId,scorefromScoreswherecourseId='001')a,(selectstuId,score

fromScoreswherecourseId='002')b

wherea.score>b.scoreanda.stuId=b.stuId;

2、查询平均成绩大于60分的同学的学号和平均成绩;

selectstuId,avg(score)

fromScores

groupbystuIdhavingavg(score)>60;

3、查询所有同学的学号、姓名、选课数、总成绩;

selectStudent.stuId,Student.stuName,count(Scores.courseId),sum(score)

fromStudentleftOuterjoinScoresonStudent.stuId=Scores.stuId

groupbyStudent.stuId,stuName

4、查询姓“李”的老师的个数;

selectcount(distinct(teacherName))

fromTeacher

whereteacherNamelike'李%';

5、查询没学过“叶平”老师课的同学的学号、姓名;

selectStudent.stuId,Student.stuName

fromStudent

wherestuIdnotin(selectdistinct(Scores.stuId)fromScores,Course,TeacherwhereScores.courseId=Course.courseIdandTeacher.teacherId=Course.teacherIdandTeacher.teacherName='叶平');

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

selectStudent.stuId,Student.stuNamefromStudent,ScoreswhereStudent.stuId=Scores.stuIdandScores.courseId='001'andexists(Select*fromScoresasScores_2whereScores_2.stuId=Scores.stuIdandScores_2.courseId='002');

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

selectstuId,stuName

fromStudent

wherestuIdin(selectstuIdfromScores,Course,TeacherwhereScores.courseId=Course.courseIdandTeacher.teacherId=Course.teacherIdandTeacher.teacherName='叶平'groupbystuIdhavingcount(Scores.courseId)=(selectcount(courseId)fromCourse,TeacherwhereTeacher.teacherId=Course.teacherIdandteacherName='叶平'));

8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

SelectstuId,stuNamefrom(selectStudent.stuId,Student.stuName,score,(selectscorefromScoresScores_2whereScores_2.stuId=Student.stuIdandScores_2.courseId='002')score2

fromStudent,ScoreswhereStudent.stuId=Scores.stuIdandcourseId='001')S_2wherescore2<score;

9、查询所有课程成绩小于60分的同学的学号、姓名;

selectstuId,stuName

fromStudent

wherestuIdnotin(selectStudent.stuIdfromStudent,ScoreswhereS.stuId=Scores.stuIdandscore>60);

10、查询没有学全所有课的同学的学号、姓名;

selectStudent.stuId,Student.stuName

fromStudent,Scores

whereStudent.stuId=Scores.stuIdgroupbyStudent.stuId,Student.stuNamehavingcount(courseId)<(selectcount(courseId)fromCourse);

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

selectstuId,stuNamefromStudent,ScoreswhereStudent.stuId=Scores.stuIdandcourseIdinselectcourseIdfromScoreswherestuId='1001';

12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;

selectdistinctScores.stuId,stuName

fromStudent,Scores

whereStudent.stuId=Scores.stuIdandcourseIdin(selectcourseIdfromScoreswherestuId='001');

13、把“Scores”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

updateScoressetscore=(selectavg(Scores_2.score)

fromScoresScores_2

whereScores_2.courseId=Scores.courseId)fromCourse,TeacherwhereCourse.courseId=Scores.courseIdandCourse.teacherId=Teacher.teacherIdandTeacher.teacherName='叶平');

14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

selectstuIdfromScoreswherecourseIdin(selectcourseIdfromScoreswherestuId='1002')

groupbystuIdhavingcount(*)=(selectcount(*)fromScoreswherestuId='1002');

15、删除学习“叶平”老师课的Scores表记录;

DelectScores

fromcourse,Teacher

whereCourse.courseId=Scores.courseIdandCourse.teacherId=Teacher.teacherIdandteacherName='叶平';

16、向Scores表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2、

号课的平均成绩;

InsertScoresselectstuId,'002',(Selectavg(score)

fromScoreswherecourseId='002')fromStudentwherestuIdnotin(SelectstuIdfromScoreswherecourseId='002');

17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

SELECTstuIdas学生ID

,(SELECTscoreFROMScoresWHEREScores.stuId=t.stuIdANDcourseId='004')AS数据库

,(SELECTscoreFROMScoresWHEREScores.stuId=t.stuIdANDcourseId='001')AS企业管理

,(SELECTscoreFROMScoresWHEREScores.stuId=t.stuIdANDcourseId='006')AS英语

,COUNT(*)AS有效课程数,AVG(t.score)AS平均成绩

FROMScoresASt

GROUPBYstuId

ORDERBYavg(t.score)

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

SELECTL.courseIdAs课程ID,L.scoreAS最高分,R.scoreAS最低分

FROMScoresL,ScoresASR

WHEREL.courseId=R.courseIdand

L.score=(SELECTMAX(IL.score)

FROMScoresASIL,StudentASIM

WHEREL.courseId=IL.courseIdandIM.stuId=IL.stuId

GROUPBYIL.courseId)

AND

R.score=(SELECTMIN(IR.score)

FROMScoresASIR

WHERER.courseId=IR.courseId

GROUPBYIR.courseId

);

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

SELECTt.courseIdAS课程号,max(course.courseName)AS课程名,isnull(AVG(score),0)AS平均成绩

,100*SUM(CASEWHENisnull(score,0)>=60THEN1ELSE0END)/COUNT(*)AS及格百分数

FROMScoresT,Course

wheret.courseId=course.courseId

GROUPBYt.courseId

ORDERBY100*SUM(CASEWHENisnull(score,0)>=60THEN1ELSE0END)/COUNT(*)DEScores

20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):企业管理(001),马克思(002),OO&UML(003),数据库(004)

SELECTSUM(CASEWHENcourseId='001'THENscoreELSE0END)/SUM(CASEcourseIdWHEN'001'THEN1ELSE0END)AS企业管理平均分

,100*SUM(CASEWHENcourseId='001'ANDscore>=60THEN1ELSE0END)/SUM(CASEWHENcourseId='001'THEN1ELSE0END)AS企业管理及格百分数

,SUM(CASEWHENcourseId='002'THENscoreELSE0END)/SUM(CASEcourseIdWHEN'002'THEN1ELSE0END)AS马克思平均分

,100*SUM(CASEWHENcourseId='002'ANDscore>=60THEN1ELSE0END)/SUM(CASEWHENcourseId='002'THEN1ELSE0END)AS马克思及格百分数

,SUM(CASEWHENcourseId='003'THENscoreELSE0END)/SUM(CASEcourseIdWHEN'003'THEN1ELSE0END)ASUML平均分

,100*SUM(CASEWHENcourseId='003'ANDscore>=60THEN1ELSE0END)/SUM(CASEWHENcourseId='003'THEN1ELSE0END)ASUML及格百分数

,SUM(CASEWHENcourseId='004'THENscoreELSE0END)/SUM(CASEcourseIdWHEN'004'THEN1ELSE0END)AS数据库平均分

,100*SUM(CASEWHENcourseId='004'ANDscore>=60THEN1ELSE0END)/SUM(CASEWHENcourseId='004'THEN1ELSE0END)AS数据库及格百分数

FROMScores

21、查询不同老师所教不同课程平均分从高到低显示

SELECTmax(Z.teacherId)AS教师ID,MAX(Z.teacherName)AS教师姓名,C.courseIdAS课程ID,MAX(C.courseName)AS课程名称,AVG(score)AS平均成绩

FROMScoresAST,CourseASC,TeacherASZ

whereT.courseId=C.courseIdandC.teacherId=Z.teacherId

GROUPBYC.courseId

ORDERBYAVG(score)DEScores

22、查询如下课程成绩第3名到第6名的学生成绩单:企业管理(001),马克思(002),UML(003),数据库(004)

[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

SELECTDISTINCTtop3

Scores.stuIdAs学生学号,

Student.stuNameAS学生姓名,

T1.scoreAS企业管理,

T2.scoreAS马克思,

T3.scoreASUML,

T4.scoreAS数据库,

ISNULL(T1.score,0)+ISNULL(T2.score,0)+ISNULL(T3.score,0)+ISNULL(T4.score,0)as总分

FROMStudent,ScoresLEFTJOINScoresAST1

ONScores.stuId=T1.stuIdANDT1.courseId='001'

LEFTJOINScoresAST2

ONScores.stuId=T2.stuIdANDT2.courseId='002'

LEFTJOINScoresAST3

ONScores.stuId=T3.stuIdANDT3.courseId='003'

LEFTJOINScoresAST4

ONScores.stuId=T4.stuIdANDT4.courseId='004'

WHEREstudent.stuId=Scores.stuIdand

ISNULL(T1.score,0)+ISNULL(T2.score,0)+ISNULL(T3.score,0)+ISNULL(T4.score,0)

NOTIN

(SELECT

DISTINCT

TOP15WITHTIES

ISNULL(T1.score,0)+ISNULL(T2.score,0)+ISNULL(T3.score,0)+ISNULL(T4.score,0)

FROMScores

LEFTJOINScoresAST1

ONScores.stuId=T1.stuIdANDT1.courseId='k1'

LEFTJOINScoresAST2

ONScores.stuId=T2.stuIdANDT2.courseId='k2'

LEFTJOINScoresAST3

ONScores.stuId=T3.stuIdANDT3.courseId='k3'

LEFTJOINScoresAST4

ONScores.stuId=T4.stuIdANDT4.courseId='k4'

ORDERBYISNULL(T1.score,0)+ISNULL(T2.score,0)+ISNULL(T3.score,0)+ISNULL(T4.score,0)DEScores);

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[<60]

SELECTScores.courseIdas课程ID,courseNameas课程名称

,S

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表