SQL数据库面试题以及答案(50例题)_sql面试题,想提高开发效率的必看

时间:2024-10-28 08:57:36

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
img

正文

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

insert into sc select sid,‘002’,
(select avg(score) from sc where cid=‘0022’)
from student
where sid not in (select sid from sc where cid=‘002’);

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

select sid as 学生id,
(SELECT score from sc
where = and cid=‘004’) as 数据库,
(select score from sc
where = and cid=‘001’) as 企业管理,
(select score from sc
where = and cid=‘015’) as 英语,
count(cid) as 有效课程数, avg() as 平均成绩
from sc as t
group by sid
order by avg();

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

select as 课程id, as 最高分,
as 最低分
from sc l,sc r
where =
and =
(select max() from sc t
where = group by )
and = (select min() from sc t
where = group by )
order by ;

select cid as 课程id,max(score) as 最高分,
min(score) as 最低分
from sc
group by cid;

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

SELECT as 课程号,
as 课程名,
COALESCE(avg(score),0) as 平均成绩,
100*sum(case
when COALESCE(score,0)>=60
then 1 else 0 END)/count(*) as 及格百分数
from sc t
left join course c
on =
group by
order by 100*sum(case
when COALESCE(score,0)>=60
then 1 else 0 END)/count(*);

20、查询如下课程平均成绩和及格率的百分数(用”1行”显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004):
21、查询不同老师所教不同课程平均分从高到低显示:

select as 教师id,
as 教师姓名,
as 课程id,
avg(score) as 平均成绩
from sc as sc
LEFT JOIN course c on =
left join teacher t on =
group by
order by avg() desc;

22、查询如下课程成绩第3名到第6名的学生成绩单:企业管理(001),马克思(002),UML(003),数据库(004):
23、统计下列各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ 小于60] :

select as 课程id,cname as 课程名称,
sum(case when score between 85 and 100 then 1 else 0 end) as ‘[100-85]’,
sum(case when score between 70 and 85 then 1 else 0 end) as ‘[85-70]’,
sum(case when score between 60 and 70 then 1 else 0 end) as ‘[70-60]’,
sum(case when score<60 then 1 else 0 end) as ‘[60-0]’
from sc as sc
left join course as c
on =
group by ;

24、查询学生平均成绩及其名次:

select 1+(select count(distinct 平均成绩)
from (select sid,avg(score) as 平均成绩
from sc group by sid)t1
where 平均成绩>t2.平均成绩) as 名次,
sid as 学生学号,平均成绩
from (select sid,avg(score) 平均成绩 from sc group by sid) as t2
order by 平均成绩 desc;

25、查询各科成绩前三名的记录(不考虑成绩并列情况):

select sid,cid,score
from sc sc_1
where (
select count(3) from sc sc_2
where sc_1.cid = sc_2.cid
and sc_2.score>=sc_1.score)<=2
order by sc_1.cid
);

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

select cid, count(sid)
from sc
group by cid;

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

select ,,
count() as 课程数
from sc as sc
LEFT JOIN student as s
on =
group by
having count()=1;

28、查询男生、女生人数:

select count(ssex) as 男生人数
from student
group by ssex
having ssex = ‘男’;
select count(2) from student
where ssex = ‘女’;

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

select sid,sname
from student
where sname like ‘张%’;

30、查询同名同姓的学生名单,并统计同名人数:

select sname,count(8)
from student
group by sname
having count(8)>1;

31、1981年出生的学生名单(注:student表中sage列的类型是datetime):
32、查询平均成绩大于85的所有学生的学号、姓名和平均成绩:

select ,,avg() as 平均成绩
from sc as sc
left join student as s
on =
group by
having avg()>85;

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

select cid,avg(score)
from sc
group by cid
order by avg(score),cid desc;

34、查询课程名称为“数据库”,且分数低于60的学生名字和分数:

select ,,,
from course c
left join sc on =
LEFT JOIN student s on =
where = ‘数据库’ and <60;

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

select ,,,
from sc
LEFT JOIN course c on =
left join student s on = ;

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数:

select distinct ,,,
from sc
left join student s on =
left join course c on =
where >70;

37、查询不及格的课程,并按课程号从大到小的排列:

select cid
from sc
where score<60
ORDER BY cid;

38、查询课程编号为“003”且课程成绩在80分以上的学生的学号和姓名:

select ,
from sc
left join student s on =
where = ‘003’ and >80;

39、求选了课程的学生人数:

select count(2) from
(select distinct sid from sc)a;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩:

select ,
from sc sc
left join student s on =
left join course c on =
left join teacher t on =
where = ‘叶平’
and = (
select max(score)
from sc sc_1
where = sc_1.cid);

41、查询各个课程及相应的选修人数:

select cid,count(*) from sc group by cid;

42、查询不同课程成绩相同的学生和学号、课程号、学生成绩:

select DISTINCT ,,
from sc as a ,sc as b
where =
and <> ;

43、查询每门课程成绩最好的前两名:
44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序:

select cid as 课程号,count(8) as 选修人数
from sc
group by cid
HAVING count(sid)>10
order by count(8) desc,cid;

45、检索至少选修两门课程的学生学号:

select sid
from sc
group by sid
having count(8)>=2;

46、查询全部学生选修的课程和课程号和课程名:

select cid,cname
from course
where cid in (select cid from sc group by cid);

47、查询没学过”叶平”老师讲授的任一门课程的学生姓名:

select sname
from student
where sid not in (
select sid
from sc,course,teacher
where = and =
and =‘叶平’
);

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

select sid,avg(COALESCE(score,0))
from sc
where sid in (
select sid
from sc
where score<60
group by sid
having count(8)>2
)
group by sid;

49、检索“004”课程分数小于60,按分数降序排列的同学学号:

select sid,score
from sc
where cid=‘004’
and score<60
order by score desc;

50、删除“002”同学的“001”课程的成绩:

delete from sc
where sid = ‘002’
and cid = ‘001’;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
select sid,score
from sc
where cid=‘004’
and score<60
order by score desc;

50、删除“002”同学的“001”课程的成绩:

delete from sc
where sid = ‘002’
and cid = ‘001’;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-hohMojWx-1713162713089)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!