zoukankan      html  css  js  c++  java
  • SQL语句练习

    前提条件

    1.mysql环境,可以装个phpstudy,简单方便
    2.建立数据表:
    学生表:
    Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
    课程表:
    Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
    教师表:
    Teacher(t_id,t_name) –教师编号,教师姓名
    成绩表:
    Score(s_id,c_id,s_s_score) –学生编号,课程编号,分数

    --建表
    --学生表
    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`)
    );
    

    3.填充数据

    --插入学生表测试数据
    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”的课程成绩高的所有学生的学号

    select a.*,b.s_score from
    (select s_id,s_score from score where c_id='01') as a,
    (select s_id,s_score from score where c_id = '02') as b
    where a.s_score>b.s_score and a.s_id = b.s_id;
    

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

    select s_id, avg(s_score)
    from  score
    group by s_id
    having avg(s_score)>60;
    

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

    select st.s_id,st.s_name,count(sc.c_id),avg(sc.s_score)
    from student st,score sc
    where st.s_id = sc.s_id
    group by s_id,s_name;
    

    或者使用:

    select st.s_id,st.s_name,count(sc.c_id),avg(sc.s_score)
    from student st join score sc
    on st.s_id = sc.s_id
    group by s_id,s_name;
    

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

    select count(distinct(t_name)) number from teacher where t_name like '张%';
    

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

    select distinct(st.s_id),st.s_name
    from student st
    where st.s_id not in
    (select sc.s_id from score sc,teacher te,course co
    where sc.c_id=co.c_id and te.t_id = co.t_id and t_name='张三');
    
    

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

    select distinct(st.s_id),st.s_name
    from student st
    where st.s_id in
    (select sc.s_id from score sc,teacher te,course co
    where sc.c_id=co.c_id and te.t_id = co.t_id and t_name='张三');
    

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

    select distinct(st.s_id),st.s_name
    from student st
    where st.s_id in
    (select a.s_id from (select s_id,c_id from score where c_id='01') a
    inner join (select s_id,c_id  from score where c_id='02') b
    on a.s_id=b.s_id
    );
    

    或者(下面这个可以很好地改为学过“01”没学过“02”等情况)

    select distinct(st.s_id),st.s_name
    from student st
    where st.s_id in
    (select s_id from score where c_id='01')
    and s_id in
    (select s_id  from score where c_id='02')
    

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

    select sum(s_score)
    from score
    where c_id='02';
    

    9.查询存在课程成绩小于60分的学生的学号、姓名

    select distinct(st.s_id), st.s_name
    from student st,score sc
    where st.s_id=sc.s_id and sc.s_score<60;
    

    如果是所有课程小于60分,则为

    select distinct(st.s_id), st.s_name
    from student st
    where st.s_id not in
    (select s_id from score where s_score>60)
    

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

    select st.s_id,st.s_name
    from student st inner join score sc on st.s_id=sc.s_id
    group by st.s_id,st.s_name
    having count(c_id)<(select count(distinct(c_id)) from course);
    

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

    select distinct(st.s_id),st.s_name
    from student st inner join score sc on st.s_id=sc.s_id
    where sc.c_id in
    (select c_id from score where s_id='01') and st.s_id !='01';
    

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

    select sc.s_id, st.s_name
    from score sc inner join student st on sc.s_id=st.s_id
    group by sc.s_id,st.s_name
    having group_concat(c_id order by c_id)
    =(select group_concat(c_id order by c_id) from score where s_id='01')
    and sc.s_id!='01'
    

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

    select st.s_id,st.s_name
    from student st
    where s_id not in
    (select sc.s_id
    from score sc
    inner join course co on sc.c_id=co.c_id
    inner join teacher te on co.t_id=co.t_id
    where te.t_name='张三');
    

    或者

    select distinct st.s_id,st.s_name
    from student st
    where st.s_id not in
    (select sc.s_id from score sc,teacher te,course co
    where co.t_id = te.t_id and sc.c_id=co.c_id and te.t_name='张三');
    

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

    select distinct st.s_id,st.s_name ,avg(sc.s_score)
    from student st inner join score sc on st.s_id=sc.s_id
    where not sc.s_score >= 60
    group by st.s_id,st.s_name
    having count(c_id)>=2;
    

    SELECT student.s_id,student.s_name,avg(score.s_score)
    From student inner join score on student.s_id = score.s_id
    Where student.s_id in
    (select tem.s_id from (select * from score sc where sc.s_score<60) tem
    group by tem.s_id
    having count(tem.c_id)>=2)
    group by student.s_id,student.s_name
    

    SELECT student.s_id,student.s_name,avg(score.s_score)
    From student inner join score on student.s_id = score.s_id
    Where student.s_id in
    (select sc.s_id from score sc where sc.s_score<60
    group by sc.s_id
    having count(sc.c_id)>=2)
    group by student.s_id,student.s_name
    

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

    select *
    from student st inner join score sc
    on st.s_id=sc.s_id
    where sc.s_score<60 and sc.c_id='01'
    order by sc.s_score desc;
    

    16.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

    select s_id,
    max(case when c_id='01' then s_score else NUll end) '01',
    max(case when c_id='02' then s_score else NULL end) '02',
    max(case when c_id='03' then s_score else NUll end) '03',
    avg(s_score)
    from score
    group by s_id
    order by avg(s_score) desc
    

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

    及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

    select sc.c_id, co.c_name, max(s_score) max, min(s_score) min, avg(s_score) avg,
    avg(case when s_score>=60 then 1.0 else 0.0 end) '及格率',
    avg(case when s_score>=70 and s_score<80 then 1.0 else 0.0 end) '中等率',
    avg(case when s_score>=80 and s_score<90 then 1.0 else 0.0 end) '优良率',
    avg(case when s_score>=90 then 1.0 else 0.0 end) '优秀率'
    from score sc inner join course co on sc.c_id=co.c_id
    group by sc.c_id;
    

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

    select c_id, avg(s_score)
    from score
    group by c_id
    order by avg(s_score) asc,c_id desc;
    

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

    select s_id,sum(s_score)
    from score sc
    group by s_id
    order by sum(s_score) desc;
    

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

    select te.t_id, te.t_name, sc.c_id,avg(sc.s_score)
    from score sc inner join course co on sc.c_id=co.c_id
    inner join teacher te on co.t_id=te.t_id
    group by te.t_id,te.t_name,sc.c_id
    order by avg(sc.s_score) desc;
    

    参考:

    1.sql面试题(学生表_课程表_成绩表_教师表)
    2.SQL面试必会50题

    感谢阅读,如有问题,请批评指正,谢谢。
  • 相关阅读:
    10 种保护 Spring Boot 应用的绝佳方法
    Redis 如何分析慢查询操作?
    Spring Boot 主类及目录结构介绍
    Redis 再牛逼,也得设置密码!!
    Spring Data Redis 详解及实战一文搞定
    Spring Boot Redis Cluster 实战干货
    超详细的 Redis Cluster 官方集群搭建指南
    Redis Linux 安装运行实战全记录
    hdu 4790 Just Random (思路+分类计算+数学)
    poj 1328 Radar Installation(贪心)
  • 原文地址:https://www.cnblogs.com/clwsec/p/11615615.html
Copyright © 2011-2022 走看看