当前位置:网站首页>数据库面试题+解析
数据库面试题+解析
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!
边栏推荐
- Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
- Dependency injection
- B_ QuRT_ User_ Guide(37)
- Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
- How can we make money by making video clips from our media?
- Caip2021 preliminary VP
- Sequence of entity layer, Dao layer, service layer and controller layer
- Have all the fresh students of 2022 found jobs? Is it OK to be we media?
- C method question 1
- Force deduction solution summary 648 word replacement
猜你喜欢

【实验分享】通过Console口登录到Cisco设备

电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

SAP HR 家庭成员信息

Unity3d Learning Notes 6 - GPU instantiation (1)
![MATLAB signal processing [Q & A essays · 2]](/img/be/0baa92767c3abbda9b0bff47cb3a75.png)
MATLAB signal processing [Q & A essays · 2]

城联优品作为新力量初注入,相关上市公司股价应声上涨150%
![[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud](/img/78/3cc67a28872ff4d465484cbaee2f6e.jpg)
[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud

The file format and extension of XLS do not match

C method question 1
随机推荐
648. Word replacement
Interface
HDU 4747 mex "recommended collection"
Summary of SQL single table query 2020.7.27
JS get the key and value of the object
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
windows设置redis开启自动启动
Tree background data storage (using webmethod) [easy to understand]
ASP. Net core middleware request processing pipeline
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
B / Qurt Utilisateur Guide (36)
欢聚时代一面
UE4_ Ue5 combined with Logitech handle (F710) use record
C simple question 2
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
谷歌浏览器怎么登录及开启同步功能
B_QuRT_User_Guide(39)
How can we make money by making video clips from our media?
MySQL Index Optimization Practice II
USB (XV) 2022-04-14