美文网首页
SQL笔试题总结

SQL笔试题总结

作者: 庆庆_ce88 | 来源:发表于2019-04-20 10:38 被阅读0次

    【建表并导入数据】

    --建表

    --学生表

    CREATE TABLE `Student`(

    `s_id` VARCHAR(20),

    `s_name` VARCHAR(20) NOT NULL DEFAULT '',

    `s_birth` VARCHAR(20) NOT NULL DEFAULT '',

    `s_sex` VARCHAR(10) NOT NULL DEFAULT '',

    PRIMARY KEY(`s_id`)

    );

    --课程表

    CREATE TABLE `Course`(

    `c_id` VARCHAR(20),

    `c_name` VARCHAR(20) NOT NULL DEFAULT '',

    `t_id` VARCHAR(20) NOT NULL,

    PRIMARY KEY(`c_id`)

    );

    --教师表

    CREATE TABLE `Teacher`(

    `t_id` VARCHAR(20),

    `t_name` VARCHAR(20) NOT NULL DEFAULT '',

    PRIMARY KEY(`t_id`)

    );

    --成绩表

    CREATE TABLE `Score`(

    `s_id` VARCHAR(20),

    `c_id` VARCHAR(20),

    `s_score` INT(3),

    PRIMARY KEY(`s_id`,`c_id`)

    );

    --插入学生表测试数据

    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' , '女');

    --课程表测试数据

    insert into Course values('01' , '语文' , '02');

    insert into Course values('02' , '数学' , '01');

    insert into Course values('03' , '英语' , '03');

    --教师表测试数据

    insert into Teacher values('01' , '张三');

    insert into Teacher values('02' , '李四');

    insert into Teacher values('03' , '王五');

    --成绩表测试数据

    insert into Score values('01' , '01' , 80);

    insert into Score values('01' , '02' , 90);

    insert into Score values('01' , '03' , 99);

    insert into Score values('02' , '01' , 70);

    insert into Score values('02' , '02' , 60);

    insert into Score values('02' , '03' , 80);

    insert into Score values('03' , '01' , 80);

    insert into Score values('03' , '02' , 80);

    insert into Score values('03' , '03' , 80);

    insert into Score values('04' , '01' , 50);

    insert into Score values('04' , '02' , 30);

    insert into Score values('04' , '03' , 20);

    insert into Score values('05' , '01' , 76);

    insert into Score values('05' , '02' , 87);

    insert into Score values('06' , '01' , 31);

    insert into Score values('06' , '03' , 34);

    insert into Score values('07' , '02' , 89);

    insert into Score values('07' , '03' , 98);

    笔试题

    *1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号。

    思路:使用自联结把01课程当作表a查出成绩,02课程当作表b查出成绩,表a和表b的学号相等,保证查询的是同一个人,然后进行成绩的比较

    -- 写法一:

    select a.s_id

    from Score a join Score b on a.s_id = b.s_id and a.s_score >b.s_score

    where a.c_id = '01' and b.c_id= '02';

    -- 写法二:

    select a.s_id

    from (select * from Score where c_id = '01') as a

    join (select * from Score where c_id='02') as b

    on a.s_id = b.s_id

    where a.s_score > b.s_score;

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

    思路: 按学号进行分组,计算出每个学生各科的成绩,然后求出平均成绩

    select s_id, avg(s_score)

    from Score

    group by s_id

    having avg(s_score)>60;

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

    思路:姓名属于Student表,其它属于Score表,考虑内联结

    select Score.s_id, Student.s_name,count(c_id),sum(s_score)

    from Score join Student on Score.s_id = Student.s_id

    group by s_id,s_name;

    4、查询姓“张”的老师的个数

    思路:like的用法

    select count(t_id)

    from Teacher

    where t_name like '张%';

    *5.查询没学过“张三”老师课的学生的学号、姓名(重点)

    思路: 查出张三老师教过什么课,并把张三老师教过的课当作一张表,而查询的学生不在这张表中

    select s_id, s_name

    from Student

    where s_id not in

    (select s_id from Score join Course on Score.c_id = Course.c_id

    join Teacher on Course.t_id = Teacher.t_id

    where t_name = '张三');

    6、查询学过“张三”老师所教的所有课的同学的学号、姓名

    select s_id, s_name

    from Student

    where s_id in

    (select s_id from Score join Course on Score.c_id = Course.c_id

    join Teacher on Course.t_id = Teacher.t_id

    where t_name = '张三');

    *7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

    自联结。把学过表01课程的学生当作表a,学过02课程的学生当作表b,分别从这两张表中取出同一个学生学习过的课程

    select s_id, s_name

    from Student

    where s_id in

    (select a.s_id from

    (select s_id from Score where c_id = '01') as a

    join (select s_id from Score where c_id ='02') as b

    on a.s_id= b.s_id);

    8、查询课程编号为“02”的总成绩

    select sum(s_score)

    from Score

    where c_id = '02';

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

    思路:按学生进行分组,查询每个学生所有课程的成绩

    select Student.s_id, Student.s_name

    from Student join Score on Student.s_id= Score.s_id

    where Score.s_score <60

    group by s_id, s_name;

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

    思路:按学生进行分组,查出每个学生学习课程的数量,再跟课程总数相比

    select Student.s_id, Student.s_name

    from Student join Score on Score.s_id = Student.s_id

    group by s_id, s_name

    having count(Score.c_id) < (select count(c_id) from Course);

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

    思路:1. 查出01学生学了什么课程;2. 查询出的结果要排除01学生

    select distinct Student.s_id, Student.s_name

    from Student join Score on Student.s_id= Score.s_id

    where c_id in

    (select c_id from Score where s_id = '01')

    and Student.s_id<>'01';

    *12、查询和“01”号同学所学课程完全相同的其他同学的学号

    思路:课程完全相同表示学习的科目相同,学习的课程数也相同。

    select s_id

    from Score

    where c_id in

    (select c_id from Score where s_id='01')

    and s_id <> '01'

    group by s_id

    having count(c_id)=(select count(c_id) from Score where s_id='01');

    *13、把“SCORE”表中“张三”老师教的课的成绩都更改为此课程的平均成绩

    思路:表有主键的情况下,不可以使用insert,应该使用update。使用update需要注意:set不能接复杂条件。把Score当成一张表,把计算出张三老师教的课的平均成绩当作另外一张表

    update Score as a

    join

    (select avg(s_score) as t, Score.c_id from Score join Course on Score.c_id= Course.c_id

    join Teacher on Teacher.t_id= Course.t_id

    where t_name ='张三'

    group by c_id) as b

    on a.c_id= b.c_id

    set a.s_score= b.t;

    14、查询和“02”号的同学学习的课程完全相同的其他同学学号和姓名(同12题)

    15、删除学习“张三”老师课的SC表记录

    delete from Score

    where c_id in

    (select c_id from Course join Teacher on Course.t_id=Teacher.t_id

    where t_name ='张三');

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

    思路:按学生进行分组,显示学生各科成绩。由于科目唯一时,会返回多个值(例如查询数据库的成绩,会返回多个学生对应数据库的成绩),此时应注意使用别名进行区别嵌套

    select s_id as '学生ID',

    (select s_score from Score where Score.s_id= sc.s_id and c_id = '04') as '数据库',

    (select s_score from Score where Score.s_id= sc.s_id and c_id = '01') as '企业管理',

    (select s_score from Score where Score.s_id= sc.s_id and c_id = '06') as '英语',

    count(c_id) as 有效课程数,

    avg(s_score) as 有效平均分

    from Score as sc

    group by s_id

    order by avg(s_score) DESC;

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

    思路:按科目进行分组

    select c_id as 课程ID,

    max(s_score) as 最高分,

    min(s_score) as 最低分

    from Score

    group by c_id;

    *19、按各科平均成绩从低到高和及格率的百分数从高到低排列,以如下形式显示:课程号,课程名,平均成绩,及格百分数

    思路:及格百分数=及格率=及格人数/总人数。

    select Course.c_id as '课程号',

    Course.c_name as '课程名',

    avg(Score.s_score) as '平均成绩',

    concat(d.c/a.b*100,'%') as '及格百分数'

    from (select c_id,count(distinct s_id) as b from Score group by c_id) as a

    join (select c_id,count(distinct s_id) as c from Score where s_score >=60 group by c_id) as d

    on a.c_id=d.c_id

    join Course on a.c_id=Course.c_id

    join Score on Course.c_id=Score.c_id

    group by Course.c_id,Course.c_name

    order by 平均成绩, 及格百分数 DESC;

    20、查询如下课程平均成绩和及格率的百分数(用1行显示),其中企业管理为001,马克思为002,UML为003,数据库为004

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

    思路:按老师、科目进行分组,不同表之间要内联结

    select Teacher.t_id,Teacher.t_name,Course.c_name,avg(Score.s_score)

    from Teacher join Course on Teacher.t_id=Course.t_id

    join Score on Course.c_id=Score.c_id

    group by t_id,t_name,c_name

    order by avg(Score.s_score) DESC;

    *23、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称

    思路:case表达式的用法

    select Course.c_id, Course.c_name,

    sum(case when Score.s_score between 85 and 100 then 1 else 0 end) as '优秀',

    sum(case when Score.s_score between 70 and 85 then 1 else 0 end) as '良好',

    sum(case when Score.s_score between 60 and 70 then 1 else 0 end) as '及格',

    sum(case when Score.s_score<60 then 1 else 0 end) as '不及格'

    from Score join Course on Score.c_id=Course.c_id

    group by c_id,c_name;

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

    思路:名次排序使用rank按平均成绩降序排序

    select s_id, avg(s_score), rank() over (order by avg(s_score) DESC)

    from Score

    group by s_id;

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

    思路:从排序好名次的表中取数据,因不考虑并列,因此使用row_number进行排序

    select c_id, s_score

    from (select *, row_number () over (partition by c_id order by s_score DESC) as a from Score) as b

    where a in(1,2,3);

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

    select c_id, count(s_id)

    from Score

    group by c_id;

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

    select Student.s_id,Student.s_name, count(Score.c_id)

    from Student join Score on Student.s_id= Score.s_id

    group by s_id,s_name

    having count(c_id)=2;

    28、查询男生、女生人数

    思路:按性别进行分组,分别计算不同分组的性别数

    select s_sex, count(s_sex)

    from Student

    group by s_sex;

    29、查询名字中含有“风”字的学生信息

    select *

    from Student

    where s_name like '%风%';

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

    思路:按学生姓名进行分组,然后查询每组人数,若同名同姓,则分组中人数会大于1

    select s_name, count(s_id)

    from Student

    group by s_name

    having count(s_id)>1;

    31、1990年出生的学生名单(注:Student表中s_birth列的类型是datetime)

    思路:可以用like取出相关年份,也可以用year

    -- 方法一

    select s_name

    from Student

    where s_birth like '1990%';

    -- 方法二

    select s_name

    from Student

    where year(s_birth)=1990;

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

    思路:按学生进行分组

    select Student.s_id, Student.s_name, avg(Score.s_score)

    from Student join Score on Student.s_id=Score.s_id

    group by s_id,s_name

    having avg(Score.s_score) >85;

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

    select c_id, avg(s_score)

    from Score

    group by c_id

    order by avg(s_score),c_id DESC;

    34、查询课程名称为“数学”且分数低于60的学生姓名和分数

    select Student.s_name, Score.s_score

    from Student join Score on Student.s_id=Score.s_id

    join Course on Course.c_id=Score.c_id

    where Course.c_name='数学'

    and Score.s_score <60;

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

    select Student.s_id, Student.s_name, Course.c_id, Course.c_name

    from Student join Score on Student.s_id=Score.s_id

    join Course on Score.c_id=Course.c_id

    group by Student.s_id, Student.s_name, Course.c_id, Course.c_name;

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

    select Student.s_name,Course.c_name,Score.s_score

    from Student join Score on Student.s_id=Score.s_id

    join Course on Score.c_id=Course.c_id

    where s_score >70

    group by Student.s_name,Course.c_name,Score.s_score;

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

    select Score.c_id, Course.c_name,Score.s_id,Student.s_name

    from Score join Course on Score.c_id=Course.c_id

    join Student on Student.s_id=Score.s_id

    where Score.s_score<60

    order by c_id DESC;

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

    select Student.s_id,Student.s_name

    from Student join Score on Student.s_id=Score.s_id

    where Score.c_id='03'

    and Score.s_score>80;

    39、查询选了课程的学生人数

    select c_id,count(s_id)

    from Score

    group by c_id;

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

    思路:查出张三老师教了哪些学生,然后把这些学生的成绩进行排序,选择第一名

    select Student.s_name, Score.s_score

    from Student join Score on Student.s_id=Score.s_id

    where Score.s_id in

    (select Score.s_id from Score join Course on Score.c_id=Course.c_id

    join Teacher on Course.t_id=Teacher.t_id

    where t_name='张三')

    order by Score.s_score DESC

    limit 1;

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

    select c_id, count(s_id)

    from Score

    group by c_id;

    *42、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

    思路:不同课程说明c_id不同

    select distinct a.s_id,a.c_id,a.s_score

    from Score a join Score b

    on a.s_id=b.s_id and a.c_id<> b.c_id

    where a.s_score=b.s_score;

    43、查询每门课程成绩最好的前两名

    select s_id, c_id, s_score

    from (select *,rank() over (partition by c_id order by s_score DESC) as b from Score) as a

    where b in (1,2);

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

    select c_id, count(s_id)

    from Score

    group by c_id

    having count(s_id)>5

    order by count(s_id) DESC,c_id;

    45、查询至少选修两门课程的学生学号

    select s_id

    from Score

    group by s_id

    having count(c_id)>=2;

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

    思路:该学生的选修课数量等于可供选择的所有选修课数量之和

    select Student.s_id, Student.s_name,Student.s_birth, Student.s_sex

    from Student join Score on Student.s_id=Score.s_id

    group by Student.s_id, Student.s_name,Student.s_birth, Student.s_sex

    having count(Score.c_id)=(select count(distinct c_id) from Score);

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

    select s_name

    from Student

    where s_id not in

    (select Score.s_id from Score join Course on Score.c_id=Course.c_id

    join Teacher on Teacher.t_id=Course.t_id

    where t_name = '张三');

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

    select s_id, avg(s_score)

    from Score

    where s_score <60

    group by s_id

    having count(c_id)>=2;

    49、检索课程编号为“04”且分数小于60的学生学号,结果按按分数降序排列

    select s_id

    from Score

    where c_id='04'

    and s_score <60

    order by s_score DESC;

    50、删除学生编号为“02”的课程编号为“01”的成绩

    delete from Score

    where s_id='02'

    and c_id='01';

    相关文章

      网友评论

          本文标题:SQL笔试题总结

          本文链接:https://www.haomeiwen.com/subject/ojskgqtx.html