欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQL笔试 I 经典20题及答案解析(上)

程序员文章站 2024-03-15 18:15:36
...

SQL笔试 I 经典20题及答案解析(上)

面试经常碰到SQL面试题,好久没实操已生疏?今天给大家简单准备20道SQL经典面试题,还有下篇;喜欢可关注作者,静候分享,温故而知新,可以为师矣.

01 建表语句

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 Course(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);

02 表结构预览

--学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数

1. 查询“01”课程比“02”课程成绩高的所有学生的学号;

select distinct t1.sid as sidfrom 
    (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 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
            ,count(if(cid='01',score,null)) as count1
            ,count(if(cid='02',score,null)) as count2
        from sc
        group by sid
        having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0
    )t
left join student
    on t.sid=student.sid

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

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

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. 查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;



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

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



#注意是和'01'号同学课程完全相同但非学习课程数相同的,这里我用左连接解决这个问题select
    t1.sid,sname
from
    (
        select
            sc.sid
            ,count(distinct sc.cid)
        from 
            (
                select
                    cid
                from sc
                where sid='01'
            )t1 #选出01的同学所学的课程
        left join sc
            on t1.cid=sc.cid
        group by sc.sid
        having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = '01')
    )t1
left join student
    on t1.sid=student.sid
where t1.sid!='01'

13. 把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;

#暂跳过update题目

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



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='张三'
    )

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



select
    t1.sid,sname,avg_score
from 
    (
        select
            sid,count(if(score<60,cid,null)),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

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



select 
    sid,if(cid='01',score,100)from sc
where if(cid='01',score,100)<60
order by if(cid='01',score,100) desc


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



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

18. 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程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

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



#这里先按照平均成绩排序,再按照及格百分数排序,
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

20. 查询学生的总成绩并进行排名



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

本文来自知乎,作者:tomocat,如果侵权请联系删除

SQL笔试 I 经典20题及答案解析(上)

ps:星球最近在大力度优惠,戳阅读原文了解加入星球

觉得小编充满爱点个赞SQL笔试 I 经典20题及答案解析(上)