一份虐你千百遍的SQL语句面试题,请笑纳

一、现有数据库casemanage中表结构如下图

TABLENAME:afinfo

1)请编写sql语句对年龄进行升序排列

select*fromafinfoorderbybirth;2)请编写sql语句查询对“徐”姓开头的人员名单

select*fromafinfowherenamelike'徐%';3)请编写sql语句修改“陈晓”的年龄为“45”

updateafinfosetage=45andbirth=birth-YEAR(45)wherename="陈晓";4)请编写sql删除王芳芳这表数据记录。

deletefromafinfowherename="王芳芳";

二、现有以下几个表

1)查询出所有学生信息,sql怎么编写?

select*fromafinfoorderbybirth;

2)新学生小明,学号为005,需要将信息写入学生信息表,sql语句怎么编写?

3)李四语文成绩被登记错误,成绩实际为85分,更新到考试信息表中,sql语句怎么编写?

select*fromafinfowherenamelike'徐%';

4)查询出各科成绩的平均成绩,显示字段为:学科、平均分,sql怎么编写?

5)查询出所有学生各科成绩,显示字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学生也需要列出,sql怎么编写?

updateafinfosetage=45andbirth=birth-YEAR(45)wherename="陈晓";

6)查询出单科成绩最高的,显示字段为:姓名、学号、学科、成绩,sql怎么编写?

7)列出每位学生的各科成绩,要求输出格式:姓名、学号、语文成绩、数学成绩、英语成绩,sql怎么编写?

三、根据要求写出sql语句。

Student(s_no,sname,sage,sex)学生表

Course(c_no,cname,t_no)课程表

Sc(s_no,c_no,score)成绩表

Teacher(t_no,tname)教师表

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

deletefromafinfowherename="王芳芳";

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

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

4、查询姓李的老师的个数。

selectcount(distinct(tname))fromTeacherwheretnamelike'李';

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

selectStudent.s_no,Student.snamefromStudent

wheres_nonotin(

selectdistinct(Sc.s_no)fromSc,Course,Teacher

whereSc.s_no=Course.c_no

andTeacher.t_no=Course.t_no

andTeacher.tname='叶平'

);

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

selectStudent.s_no,Student.snamefromStudent,Sc

whereStudent.s_no=Sc.s_no

andSc.c_no='002'

andexists(

select*fromScasSc1

whereSc.s_no=Sc1.s_no

andSc1.s_no='002'

);

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

selects_no,snamefromStudent

wheres_nonotin(

selectS.s_nofromStudentASS,Sc

whereS.s_no=Sc.s_no

andscore>60

);

8、查询没有学全所有课的同学的学号、姓名。

selectStudent.s_no,Student.snamefromStudent,Sc

whereStudent.s_no=Sc.s_no

groupbyStudent.s_no,Student.sname

havingcount(c_no)<(

selectcount(*)fromCourse

);

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

selectdistincts_no,sname

fromStudent,Sc

whereStudent.s_no=Sc.s_no

andSc.c_noin(

selectc_no

fromSc

wheres_no='1001'

);

10、把“sc”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩。

updateScsetscore=(

selectavg(Sc_2.score)

fromScSc_2

whereSC_2.c_no=Sc.c_no

)

fromCourse,Teacher

whereCourse.c_no=Sc.c_no

andCourse.t_no=Teacher.t_no

andTeacher.tname='叶平'

);

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

selects_nofromSc

wherec_noin(

selectc_nofromScwheres_no='1002'

)

groupbys_no

havingcount(*)=(

selectcount(*)fromSc

wheres_no='1002'

);

12、删除学习“叶平”老师课的sc表记录。

deleteScfromcourse,Teacher

whereCourse.c_no=SC.c_no

andCourse.t_no=Teacher.t_no

andtname='叶平';

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

insertintoSc

selects_nofromStudent

wheres_nonotin(

Selects_nofromScwherec_no='003'

);

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

SELECTL.c_noAsc_no,L.scoreASmax_score,R.scoreASmix_scoreFROMScL,ScASR

WHEREL.c_no=R.c_noand

L.score=(SELECTMAX(IL.score)

FROMScASIL,StudentASIM

WHEREL.c_no=IL.c_noandIM.s_no=IL.s_no

GROUPBYIL.c_no)

AND

R.score=(SELECTMIN(IR.score)

FROMScASIR

WHERER.c_no=IR.c_no

GROUPBYIR.c_no

)orderbyL.c_no;

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

selectc_no,avg(score)avg_score

fromSc

groupbyc_no

orderbyavg_scoredesc16、统计各科成绩,各分数段人数:课程ID,课程名称,【100-85】,【85-70】,【70-60】,【<60】

selectCourse.c_no,cname,

count(casewhenscore>85andscore<=100thenscoreend)'[85-100]',

count(casewhenscore>70andscore<=85thenscoreend)'[70-85]',

count(casewhenscore>=60andscore<=70thenscoreend)'[60-70]',

count(casewhenscore<60thenscoreend)'[<60]'

fromCourse,Sc

whereCourse.c_no=Sc.c_no

groupbyCourse.c_no,c_name;

17、查询每门课程被选修的学生数

selectc_no,count(*)fromScgroupbyc_no;

18、查询出只选修了一门课程的全部学生的学号和姓名

selectStudent.s_no,Student.sname,count(c_no)

fromStudent

joinSc

onStudent.s_no=Sc.s_no

groupbyStudent.s_no,Student.sname

havingcount(c_no)=1;

19、查询男生、女生人数

selectcount(*)fromStudentgroupbysex;

20、查询姓“张”的学生名单

select*fromStudentwheresnamelike'张%';

21、查询同名同性学生名单,并统计同名人数。

selectsname,count(*)fromStudentgroupbysnamehavingcount(*)>1;

22、查询1994年出生的学生名单(注:student表中sage列的类型是datatime)

select*fromStudentwhereyear(curdate())-age='1994';

23、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列。

selectc_no,avg(score)fromSc

groupbyc_no

orderbyavg(score)asc,c_nodesc;

24、查询平均成绩都大于85的所有学生的学号,姓名和平均成绩

selectStudent.s_no,Student.sname,avg(score)

fromStudent,Sc

whereStudent.s_no=Sc.s_no

groupbyStudent.s_no,Student.sname

havingavg(score)>85;

25、查询课程名称为“数据库”且分数低于60的学生姓名和分数

selectStudent.sname,Sc.score

fromStudent,Sc

whereStudent.s_no=Sc.s_no

andSc.score<60

andSc.c_no=(

selectc_nofromCoursewherecname='数据库'

);

26、查询所有学生的选课情况

selectStudent.s_no,Student.sname,Sc.s_no,Course.cname

fromStudent,Sc,Course

whereStudent.s_no=Sc.s_no

andSc.c_no=Course.c_no;

27、查询不及格的课程,并按课程号从大到小排序。

selectStudent.sname,Sc.c_no,Course.cname,Sc.score

fromStudent,Sc,Course

whereStudent.s_no=Sc.s_no

andSc.c_no=Course.c_no

andSc.score<60

orderbyc_no;

28、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名。

selectStudent.s_no,Student.sname

fromStudent,Sc,Course

whereSc.score>80

andCourse.c_no='003';

29、求选修了课程的学生人数。

selectcount(*)from(selectcount(*)fromScgroupbys_no)b;

30、查询选修了“冯老师”所授课程的学生中,成绩最高的学生姓名及其成绩。

selectStudent.sname,Sc.score

fromStudent,Sc,Course

whereStudent.s_no=Sc.s_no

andSc.c_no=Course.c_no

orderbyscoredesc

limit1;

31、查询各个课程及相应的选修人数。

selectCourse.c_no,Course.cname,count(s_no)fromCourse

joinSc

onCourse.c_no=Sc.c_no

groupbyCourse.c_no,Course.cname;

32、查询每门课程最好的前两名。

selecta.s_no,a.c_no,a.scorefromSca

where(

selectcount(distinctscore)

fromScb

whereb.c_no=a.c_no

andb.score>=a.score

)<=2

orderbya.c_no,a.scoredesc;

33、查询每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列。

selectSc.c_no,count(*)fromSc

groupbyc_no

havingcount(*)>10

orderbycount(*)desc,c_no;

34、检索至少选修两门课程的学生学号。

selects_nofromScgroupbys_nohavingcount(*)>2;

35、查询全部学生都选修的课程的课程号和课程名。

selectCourse.c_no,Course.cnamefromCourse

joinSc

onCourse.c_no=Sc.c_no

join(

selectc_no,count(s_no)fromSc

groupbyc_no

havingcount(s_no)=(

selectcount(*)fromStudent))asa

onCourse.c_no=a.c_no;

36、查询两门以上不及格课程的同学的学号及其平均成绩。

selects_no,avg(score)fromSc

wheres_noin(

selects_nofromSc

wherescore<60

groupbys_no

havingcount(*)>2

)groupbys_no;

以上是本文的全部内容,希望对大家的学习有帮助,觉得有用,有需要就支持一下吧

相关文章

在网易云游戏中做一些任务可以获得游戏时间,那么具体怎么做...
腾讯课堂是一款很好的学习软件,很多小伙伴都在上面学习,那...
很多小伙伴在使用微信转账的时候,想知道如何隐藏实名,下面...
七天学堂如何注销账号?很多小伙伴想知道在这款软件中该怎样注...
在哔哩哔哩中看游戏视频可以设置视频静音播放,那么具体怎么...
很多人不知道高德地图如何创建群组?今日为你们带来的文章是高...