当前位置:网站首页>数据库面试题+解析
数据库面试题+解析
2022-07-07 21:52:00 【天蝎座的程序员】
以下是四张表,1.学生表-t_student,2.教师表-t_teacher,3.课程表-t_course, 4.成绩表-t_score
下面是表设计:
-- 1.学生表-t_student
-- sid 学生编号,sname 学生姓名,sage 学生年龄,ssex 学生性别
create table t_student(
sid varchar(10) primary key comment '学生编号',
sname varchar(20) not null comment '学生姓名',
sage varchar(10) not null comment '学生年龄',
sex varchar(20) not null comment '学生性别'
)comment '学生信息表';
-- 2.教师表-t_teacher
-- tid 教师编号,tname 教师名称
create table t_teacher(
tid varchar(10) primary key comment '教师编号',
tname varchar(20) not null comment '教师名称'
)comment '教师信息表';
-- 3.课程表-t_course
-- cid 课程编号,cname 课程名称,tid 教师名称
create table t_course(
cid varchar(10) primary key comment '课程编号',
cname varchar(20) not null comment '课程名称',
tid varchar(10) not null comment '教师名称',
foreign key(tid) references t_teacher(tid)
)comment '课程信息表';
-- 4.成绩表-t_score
-- sid 学生编号,cid 课程编号,score 成绩
create table t_score(
sid varchar(10) not null comment '学生编号',
cid varchar(10) not null comment '课程编号',
score float default 0 comment '成绩',
foreign key(sid) references t_student(sid),
foreign key(cid) references t_course(cid)
)comment '成绩信息表';
select * from t_student;
select * from t_teacher;
select * from t_course;
select * from t_score;
下面是面试题+解题结果(解题的方式不是只有这一种,我写的而只是其中之一)
#01)查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select s.*,temp.s1,temp.s2 from(
select t1.sid,t1.s1,t2.s2 from
(select sid,score as s1 from t_score where cid='01')t1,
(select sid,score as s2 from t_score where cid='02')t2
where t1.sid=t2.sid and t1.s1>t2.s2)temp,t_student s
where temp.sid=s.sid
#02)查询同时存在" 01 "课程和" 02 "课程的情况
select* from
(select sid,score as s1 from t_score where cid='01') t1,
(select sid,score as s2 from t_score where cid='02') t2
where t1.sid=t2.sid;
#03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select sid,score from t_score where cid='01')t1 left join
(select sid,score from t_score where cid='02')t2 on t1.sid=t2.sid
#04)查询不存在" 01 "课程但存在" 02 "课程的情况
select * from t_score where sid not in (
select sid from t_score where cid='01') and cid='02';
select * from t_score where sid='07';
#05)查询平均成绩(分组 group by,筛选:having avg)大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select s.sid,s.sname,
round(avg(sc.score),2) score
from
t_student s,t_score sc
where s.sid=sc.sid
group by s.sid,s.sname
having avg(sc.score)>=60;
#06)查询在t_score表存在成绩的学生信息
select distinct s.*
from
t_score sc,t_student s where sc.sid=s.sid
#07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select s.sid,s.sname, count(sc.cid) cn,
sum(sc.cid) sm
from
t_student s left join t_score sc on sc.sid=s.sid
group by s.sid,s.sname
#08)查询「李」姓老师的数量
select count(tid) from t_teacher where tname like '李%';
#09)查询学过「张三」老师授课的同学的信息
select
s.*
from t_teacher t,
t_course c,
t_score sc,
t_student s
where
t.tid=c.tid and
c.cid=sc.cid and
sc.sid=s.sid and t.tname='张三'
#10)查询没有学全所有课程的同学的信息
select * from t_student where sid not in (
select sid from t_score
group by sid having count(cid)=(select count(cid) from t_course))
#11)查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from t_student where sid not in(
select sc.sid from t_teacher t,t_course c,t_score sc where
t.tid=c.tid and t.tname='张三' and
c.cid=sc.cid)
#12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select
s.sid,
s.sname,
round(avg(sc.score),2) sc
from t_score sc,t_student s
where sc.sid=s.sid and sc.score<60
group by s.sid,s.sname
having count(sc.cid)>1
#13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select *
from t_student a,
t_score b
where a.sid = b.sid
and b.cid = '01'
and score < 60
order by score desc;
#14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc.sid,
round(avg(sc.score),2) as '平均成绩',
sum(if(sc.cid='01',sc.score,0)) as '语文',
sum(if(sc.cid='02',sc.score,0)) as '数学',
sum(if(sc.cid='03',sc.score,0)) as '英语'
from
t_score sc
group by sc.sid,sc.cid
order by avg(sc.score) desc
#15)查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select
c.cid,
c.cname,
count(c.cid) as '选修人数',
max(sc.score) as '最高分',
min(sc.score) as '最低分',
round(avg(sc.score),2) as '平均分',
concat(round(sum(if(sc.score>=60,1,0))/count(s.sid)*100,2),'%') as '及格率',
concat(round(sum(if(sc.score>=70 and sc.score<80,1,0))/count(s.sid)*100,2),'%') as '中等率',
concat(round(sum(if(sc.score>=80 and sc.score<90,1,0))/count(s.sid)*100,2),'%') as '优良率',
concat(round(sum(if(sc.score>=90,1,0))/count(s.sid)*100,2),'%') as '优秀率'
from
t_score sc,t_course c,t_student s
where
sc.cid=c.cid and sc.sid=s.sid
group by c.cid,c.cname
以上就是今天的分享,在面试官给你题目的时候,你面前是没与电脑的,你需要在脑海中构思你的表设计。这是面试的一大难题,需要足够的空间想象能力。good luck!
边栏推荐
- 给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
- HDU 4747 mex "recommended collection"
- Tree background data storage (using webmethod) [easy to understand]
- USB (XV) 2022-04-14
- B / Qurt Utilisateur Guide (36)
- How can we make money by making video clips from our media?
- 谷歌浏览器怎么登录及开启同步功能
- Class C design questions
- USB (XIV) 2022-04-12
- UE4_ Ue5 combined with Logitech handle (F710) use record
猜你喜欢
Flash encryption process and implementation of esp32
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
Extended tree (I) - graphic analysis and C language implementation
Markdown
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
Three questions TDM
SAP HR labor contract information 0016
First week of July
Right click the idea file to create new. There is no solution to create new servlet
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
随机推荐
SAP HR social work experience 0023
B_QuRT_User_Guide(40)
What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
How to login and enable synchronization function in Google browser
MATLAB signal processing [Q & A essays · 2]
USB (XVI) 2022-04-28
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
B_ QuRT_ User_ Guide(36)
windows设置redis开启自动启动
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
8.31 Tencent interview
Anxin vb01 offline voice module access intelligent curtain guidance
2022第六季完美童模陕西总决赛圆满落幕
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
HDU 4747 mex "recommended collection"
B_QuRT_User_Guide(37)
【7.5】15. 三数之和
Slam interview summary
SAP HR 社会工作经历 0023
List. How to achieve ascending and descending sort() 2020.8.6