SQL常见面试题

create database dathonsql1
use dathonsql1
create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));

insert into Student values('01' , '赵雷 ' , '1990 01 01' , '男');
insert into Student values('02' , '钱电 ' , '1990 12 21' , '男');
insert into Student values('03' , '孙风 ' , '1990 05 20' , '男');
insert into Student values('04' , '李云 ' , '1990 08 06' , '男');
insert into Student values('05' , ' 周梅 ' , '1991 12 01' , '女');
insert into Student values('06' , '吴兰 ' , '1992 03 01' , '女');
insert into Student values('07' , '郑竹 ' , '1989 07 01' , '女');
insert into Student values('08' , '王菊 ' , '1990 01 20' , '女');

create table Co
urse(cid varchar(10),cname varchar(10),tid varchar(10));
insert into Course values('01' , '语文 ' , '02');
insert into Course values('02' , '数学 ' , '01');
insert into Course values('03' , '英语 ' , '03');

create table Teacher(tid varchar(10),tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

题目 

1、查询"01"课程比"02"课程成绩高的所有学生的学号

select distinct t1.sid as sid
from
(select * from sc where cid='01')t1
left join (select * from sc where cid='02')t2
on t1.sid=t2.sid
where t1.score>t2.score

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

select
sid ,avg(score)
from sc
group by sid
having avg(score)>60

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

select student.sid as sid ,sname
       ,count(distinct cid) course_cnt
       ,sum(score) as total_score
from student
left join sc
on student.sid=sc.sid
group by student.sid,sname

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

select count(distinct tid) as teacher_cnt
from teacher
where tname like '李'

5、查询没学过 张三 老师课的同学的学号、姓名 

select sid,sname
from student
where sid not in(select sc.sid
                 from teacher
                 left join course
                 on teacher.tid=course.tid
                 left join sc
                 on course.cid=sc.cid
                 where teacher.tname='张三')

6、查询学过编号 "01"并且也学过编号"02"课程的同学的学号、姓名

select t.sid as sid,sname
from (select sid,
      from sc
      group by sid
      having count(if(cid='01',score,null))>0 and count(if(cid='02',scor e,null))>0 )t
left join student
on t.sid=student.sid

7、查询学过 张三 老师所教的课的同学的学号、姓名 

select student.sid,sname
from student as st
inner join score as s on s.sid=st.sid
inner join course as c on s.cid=c.cid
inner join teacher as t on t.tid=c.tid
where t.tname = '张三'
order by st.sid

8、查询课程编号"01"的成绩比课程编号"02"课程低的所有同学的学号、姓名 

select t1.sid,sname
from (select distinct t1.sid as sid
      from (select * from sc where cid = '01')t1
      left join (select * from sc where cid = '02')t2
      on t1.sid=t2.sid
      where t1.score < t2.score)t1
left join student
on t1.sid = student.sid

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

select t1.sid,sname
from (select sid,max(score)
      from sc
      group by sid
      having max(score) < 60)t1
left join student
on t1.sid = student.sid

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

select t1.sid,sname
from (select count(cid),sid
      from sc
      group by sid
      having count(cid) < (select count(distinct cid) from course)t1
left join student
on t1.sid=student.sid

11、查询至少有一门课与学号为 “ 的同学所学相同的同学的学号和姓名 

select distinct sc.sid
from (select cid
      from sc
      where sid = '01')t1
left join sc
on t1.cid = sc.cid

12、查询和 "01"号的同学学习的课程完全相同的其他同学的学号和姓名 

select t2.sid,t3.sname from
(select * from
(select sid,group_concat(cid) new_col from sc group by sid)t1
where t1.new_col =
(select group_concat(cid) as new_col from sc where sid = '01' group by sid)t2
inner join student t3
on t2.sid = t3.sid

13、查询没学过 张三 老师讲授的任一门课程的学生姓名 

select sname
from student
where sid not in (select distinct sid
                  from sc
                  left join course
                  on sc.cid = course.cid
                  left join teacher
                  on course.tid = teacher.tid
                  where tname = '张三')

14、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 

select t1.sid,sname,avg_score
from (select sid,avg(score) as avg_score
      from sc
      group by sid
      having count(if(score < 60,cid,null) >= 2)t1
left join student
on t1.sid = student.sid

15、检索 "01" 课程分数小于 60 ,按分数降序排列的学生信息

select *
from sc
where cid = '01' and score < 60
order by score desc

16、按平均成绩从高到低显示所有学生的平均成绩 

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

17、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID ,课程 name ,最高分,最低分,平均分,及格率 

select sc.cid,cname
       max(score) as max_score,
       min(score) as min_score,
       avg(score) as avg_score,
       count(if(score >= 60,sid,null)/count(sid) as pass_rate
from sc
left join course
on sc.cid = course.cid
group by sc.cid
       

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

select cid,avg(score) as avg_score,
       count(if(score >=60,sid,null)/count(sid) as pass_rate
from sc
group by cid
order by avg_score,pass_rate desc

19、查询学生的总成绩并进行排名 

select sid,sum(score) as sum_score
from sc
group by sid
order by sum_score desc

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

select tid,avg(score) as avg_score
from course
left join sc
on course.cid = sc.cid
group by tid
order by avg_acore desc

21、查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩 

select sid,rank_num,score,cid
from (select rank() over (partition by cid order by score desc) as rank_num,
      sid,score,cid
      from sc)t
where rank_num in (2,3)

 22、统计各科成绩各分数段人数:课程编号 课程名称 ,[100 85],[85 70],[70 60],[0 60] 及所占百分比

select sc.cid,cname,
       count(if(score between 85 and 100,sid,null)/count(sid),
       count(if(score between 70 and 85,sid,null)/count(sid),
       count(if(score between 60 and 70,sid,null)/count(sid),
       count(if(score between 0 and 60,sid,null)/count(sid)
from sc
left join course
on sc.cid = course.cid
group by sc.cid,cname

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

select sid,avg_score,rank() over (order by avg_score desc)
from (select sid,avg(score) as avg_score
from sc
group by sid)t

 24、查询各科成绩前三名的记录

select sid,cid,rank1
from (select cid,sid,rank() over(partition by cid order by score desc) as rank1
      from sc)t
where rank1 <=3

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

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

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

select * from
(select sid
from sc
group by sid
having count(cid)=1)t1
inner join (select sid,sname from student)t2
on t1.sid = t2.sid

27、查询男生、女生人数 

select ssex,count(distinct sid)
from student
group by ssex

28、查询名字中含有 风 字的学生信息

select sid,sname
from student
where sname like '%风%'

29、查询 1990 年出生的学生名单 注: Student 表中 Sage 列的类型是 datetime) 

select sid,sname,sage
from student
where year(sage)=1990

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

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

 31、查询课程名称为数学,且分数低于60的学生姓名和分数

select s.sid,s.score,st.sname from score as s
inner join course as c on s.cid=c.cid
inner join student as st on s.sid=st.sid
where c.cname='数学' and s.score<60

 32、查询所有学生的课程及分数情况

select s.*,c.cname,
max(case when c.cname='数学' then s.score else null end)as '数学',
max(case when c.cname='语文' then s.score else null end)as '语文',
max(case when c.cname='英语' then s.score else null end)as '英语'
from score as s
inner join course as c on s.cid=c.cid
inner join student as st on s.sid=st.sid
group by st.sid,st.sname

 33、查询课程成绩在70分以上课程名称,分数和学生姓名

select c.cname,s.score,c.cname,s.score from score as s
inner join course as c on s.cid=c.cid
inner join student as t on s.sid=t.sid
where s.score>70

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

select s.sid,st.sname,c.cid,c.cname,s.score
from score as s
inner join course as c on s.cid=c.cid
inner join student as st on s.sid=st.sid
where s.score < 60
order by c.cid desc

 35、查询课程编号为'01'课程成绩在60分以上的学生的学号和姓名

select sid,cid,score
from sc
where cid = '01' and score > 60

 36、求每门课程的学生人数

select c.cid.c.cname,count(distinct sid)from score
inner join course as c on c.cid=s.cid
group by c.cid,c.cname

 37、查询选修张三老师所授课程的学生中,成绩最高的学生姓名及其成绩

select sc.sid,sname,cname,score
from sc
left join course 
on sc.cid=course.cid
left join teacher
on course.tid=teacher.tid
left join student
on sc.sid=student.sid
where tname='张三'
order by score desc
limit 1

 38、查询不同课程成绩相同的学生的学生编号,课程编号,学生成绩

select sid from
(select b.sid,b.score from score as b
inner join
(select sid from score
group by sid having count(distinct cid)>1) as c
on b.sid=c.sid
group by b.sid,b.score)as a
group by sid having count(sid)=1

 39、查询每门功课成绩最好的前两名

select cid,sid,rank1
from(select cid,sid,rank() over(partition by cid order by score desc) as rank1
     from sc)t
where rank1 <=2

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

select cid,count(sid) as cnt
from sc
group by cid
having cnt>=5
order by count(sid) desc,cid

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

select sid,count(cid)
from sc
group by sid
having count(cid)>=2

 42、查询选修了全部课程的学生信息

select sid,count(cid)
from sc
group by sid
having count(cid)=(select count(distinct cid) from sc)

 43、查询各学生的年龄

select sid,sname,year(curdate())-year(sage) as sage
from student

 44、查询本周过生日的学生

select sid,sname,sage
from student
where weekofyear(sage)=weekofyear(curdate())

 45、查询下周过生日的学生

select sid,sname,sage
from student
where weekofyear(sage) = weekofyear(date_add(curdate(),interval 1 week)

 46、查询本月过生日的学生

select sid,sname,sage
from student
where month(sage)=month(curdate())

 47、查询下月过生日的学生

select sid,sname,sage
from student
where month(date_sub(sage,interval 1 month))=month(curdate())

 48、求每门课程的学生人数

select c.cid,count(distinct sid)from score as s
inner join course as c on c.cid=s.cid
group by c.cid,c.cname

 49、查询课程编号为02的总成绩

select sum(score),avg(score),count(score),count(distinct sid) from score
where cid = '02'

 50、查询出只有两门课程的全部学生的学号和姓名

select sid,sname from student
where sid in
(select sid from score
group by sid having count(distinct cid)=2)

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

select a.sid,a.sname,b.avg_score from student as a
inner join
(select sid,avg(score) as avg_score from score
group by sid having avg_score >=85)as b
on a.sid=b.sid

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值