sql统计各科成绩⼤于平均分的⼈_⼀份虐你千百遍的SQL语句
⾯试题,请笑纳
⼀、现有数据库casemanage中表结构如下图
TABLENAME:afinfo
1)请编写sql语句对年龄进⾏升序排列
select * from afinfo order by birth;
2)请编写sql语句查询对“徐”姓开头的⼈员名单
select * from afinfo where name like '徐%';
3)请编写sql语句修改“陈晓”的年龄为“45”
update afinfo set age=45 and birth=birth-YEAR(45) where name="陈晓";
4)请编写sql删除王芳芳这表数据记录。
delete from afinfo where name="王芳芳";
⼆、现有以下⼏个表
1)查询出所有学⽣信息,SQL怎么编写?
select * from afinfo order by birth;
2)新学⽣⼩明,学号为005,需要将信息写⼊学⽣信息表,SQL语句怎么编写?
3)李四语⽂成绩被登记错误,成绩实际为85分,更新到考试信息表中,SQL语句怎么编写?
select * from afinfo where name like '徐%';
4)查询出各科成绩的平均成绩,显⽰字段为:学科、平均分,SQL怎么编写?
5)查询出所有学⽣各科成绩,显⽰字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学⽣也需要列出,SQL怎么编写?update afinfo set age=45 and birth=birth-YEAR(45) where name="陈晓";
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”课程成绩⾼的所有学⽣的学号。
delete from afinfo where name="王芳芳";
2、查询平均成绩⼤于60分的同学的学号和平均成绩。
3、查询所有同学的学号、姓名、选课数、总成绩。
4、查询姓李的⽼师的个数。
select count(distinct(tname)) from Teacher where tname like '李';
5、查询没学过“叶平”⽼师课的同学的学号、姓名
select Student.s_no,Student.sname from Student where s_no not in(select distinct (Sc.s_no) from Sc,Course,Teacher where Sc.s_no=Course.c_no and T
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no='002' and exists(select * from Sc as Sc1 where Sc.s_no=S 7、查询所有课程成绩⼩于60分的同学的学号、姓名。
select s_no,sname from Student where s_no not in (select S.s_no from Student AS S,Sc where S.s_no=Sc.s_no and score>60);
8、查询没有学全所有课的同学的学号、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no,Student.sname having count(c_no)
9、查询⾄少学过学号为“001”同学所有⼀门课的其他同学学号和姓名。
select distinct s_no,sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no in (select c_no from Sc where s_no='1001');
10、把“sc”表中“叶平”⽼师教的课的成绩都更改为此课程的平均成绩。
update Sc set score=(select avg(Sc_2.score) from Sc Sc_2 where SC_2.c_no=Sc.c_no ) from Course,Teacher where Course.c_no=Sc.c_no and Course.t 11、查询和“1002”号同学学习的课程完全相同的其他同学学号和姓名。
select s_no from Sc where c_no in (select c_no from Sc where s_no='1002') group by s_no having count(*)=(select count(*) from Sc where s_no='1002');
12、删除学习“叶平”⽼师课的sc表记录。
delete Sc from course,Teacher where Course.c_no=SC.c_no and Course.t_no=Teacher.t_no and tname='叶平';
13、向sc表中插⼊⼀些记录,这些记录要求符合⼀下条件:没有上过编号“003”课程的同学学号
insert into Sc select s_no from Student where s_no not in (Select s_no from Sc where c_no='003');
14、查询各科成绩最⾼和最低的分:以如下形式显⽰:课程ID,最⾼分,最低分。
SELECT L.c_no As c_no,L.score AS max_score,R.score AS mix_score FROM Sc L ,Sc AS RWHERE L.c_no = R.c_no andL.score = (SELECT MAX(IL.sco 15、查询不同⽼师所教不同课程平均分从⾼到低显⽰。
select c_no,avg(score) avg_score from Sc group by c_no order by avg_score desc
16、统计各科成绩,各分数段⼈数:课程ID,课程名称,【100-85】,【85-70】,【70-60】,【<60】
select Course.c_no,cname,count(case when score>85 and score<=100 then score end) '[85-100]',count(case when score>70 and score<=85 then score e 17、查询每门课程被选修的学⽣数
select c_no,count(*) from Sc group by c_no;
18、查询出只选修了⼀门课程的全部学⽣的学号和姓名
select Student.s_no,Student.sname,count(c_no) from Student join Sc on Student.s_no=Sc.s_no group by Student.s_no, Student.sname having count(c_no 19、查询男⽣、⼥⽣⼈数
select count(*) from Student group by sex;
20、查询姓“张”的学⽣名单
select * from Student where sname like '张%';
多表查询sql语句面试题21、查询同名同性学⽣名单,并统计同名⼈数。
select sname ,count(*) from Student group by sname having count(*)>1;
22、查询1994年出⽣的学⽣名单(注:student表中sage列的类型是datatime)
select * from Student where year(curdate())-age='1994';
23、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列。
select c_no ,avg(score)from Sc group by c_no order by avg(score) asc,c_no desc;
24、查询平均成绩都⼤于85的所有学⽣的学号,姓名和平均成绩
select Student.s_no,Student.sname,avg(score) from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no, Student.sname having avg(score)>
25、查询课程名称为“数据库”且分数低于60的学⽣姓名和分数
select Student.sname,Sc.score from Student,Sc where Student.s_no=Sc.s_no and Sc.score<60 and Sc.c_no=(select c_no from Course where cname='数据库');
26、查询所有学⽣的选课情况
select Student.s_no,Student.sname,Sc.s_no,Courseame from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no;
27、查询不及格的课程,并按课程号从⼤到⼩排序。
select Student.sname,Sc.c_no,Courseame,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no and Sc.score<60 28、查询课程编号为003且课程成绩在80分以上的学⽣的学号和姓名。
select Student.s_no,Student.sname from Student,Sc,Course where Sc.score>80 and Course.c_no='003';
29、求选修了课程的学⽣⼈数。
select count(*) from (select count(*) from Sc group by s_no) b;
30、查询选修了“冯⽼师”所授课程的学⽣中,成绩最⾼的学⽣姓名及其成绩。
select Student.sname,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no order by score desc limit 1;
31、查询各个课程及相应的选修⼈数。
select Course.c_no,Courseame,count(s_no) from Course join Scon Course.c_no=Sc.c_no group by Course.c_no, Courseame;
32、查询每门课程最好的前两名。
select a.s_no,a.c_no,a.score from Sc a where (select count(distinct score) from Sc b where b.c_no=a.c_no and b.score>=a.score)<=2 order by a.c_no,a.s
33、查询每门课程的学⽣选修⼈数(超过10⼈的课程才统计)。要求输出课程号和选修⼈数,查询结果按⼈数降序排列,查询结果按⼈数降
序排列,若⼈数相同,按课程号升序排列。
select Sc.c_no,count(*) from Sc group by c_no having count(*)>10 order by count(*) desc,c_no;
34、检索⾄少选修两门课程的学⽣学号。
select s_no from Sc group by s_no having count(*)>2;
35、查询全部学⽣都选修的课程的课程号和课程名。
select Course.c_no,Courseame from Course join Sc on Course.c_no=Sc.c_no join (select c_no,count(s_no) from Sc group by c_no having count(s_no) 36、查询两门以上不及格课程的同学的学号及其平均成绩。
select s_no,avg(score) from Sc where s_no in (select s_no from Sc where score<60 group by s_no having count(*)>2) group by s_no;
以上是本⽂的全部内容,希望对⼤家的学习有帮助,觉得有⽤,有需要就⽀持⼀下吧

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。