• sql语句练习


    科目表
    course

    c_id c_name t_id
    id 科目名称 讲师id

    学生信息表
    student

    s_id s_name s_birth s_sex

    id 学生姓名 出生年月日 性别


    教师信息表
    teacher

    t_id t_name
    讲师id 讲师姓名

    成绩表
    score

    s_id c_id s_score
    学生信息 科目id 成绩


    -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    select * from student as st
    inner join score as sc on st.s_id = sc.s_id
    and sc.c_id="01" inner join score as sc2
    on sc.s_id = sc2.s_id and sc2.c_id="02"
    and sc.s_score >sc2.s_score;
    -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
    select * from student as st
    inner join score as sc on st.s_id = sc.s_id
    and sc.c_id="01" inner join score as sc2
    on sc.s_id = sc2.s_id and sc2.c_id="02"
    and sc.s_score < sc2.s_score;
    -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
    select student.s_id, student.s_name, AVG(score.s_score) from student,score
    where student.s_id = score.s_id and score.s_score>60 group by score.s_id
    having count(*)>1;
    -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
    select student.s_id, student.s_name,score.s_score, AVG(score.s_score) from student,score
    where student.s_id = score.s_id and score.s_score<60 group by score.s_id having count(*)>1;
    -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
    SELECT a.s_id,a.s_name,b,c FROM student a JOIN(SELECT s_id,COUNT(c_id)AS b,SUM(s_score) AS c
    FROM score GROUP BY s_id)d ON a.s_id = a.s_id;
    -- 6、查询"李"姓老师的数量
    SELECT COUNT(*) FROM teacher where t_name like '%李%'
    -- 7、查询学过"张三"老师授课的同学的信息
    select a.* from student a where a.s_id in (select s_id from score
    where c_id = (select c_id from course
    where t_id = (select t_id from teacher where t_name = '张三')) group by s_id);
    -- 8、查询没学过"张三"老师授课的同学的信息
    select a.* from student a where a.s_id not in (select s_id from score
    where c_id = (select c_id from course
    where t_id = (select t_id from teacher where t_name = '张三')) group by s_id);
    -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    select * from (course inner join teacher on course.t_id = teacher.t_id) inner join student where c_id = 01 or c_id = 02
    -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
    select * from (course inner join teacher on course.t_id = teacher.t_id) inner join student where c_id = 01 or c_id != 02
    -- 11、查询没有学全所有课程的同学的信息
    select a.* from student a,score b where a.s_id = b.s_id group by b.s_id having count(b.c_id) != '3';
    select s.* from student s where s.s_id in(
    select s_id from score
    where
    s_id not in(select a.s_id from score a join score b on a.s_id = b.s_id
    and b.c_id = '02' join score c on a.s_id = c.s_id
    and c.c_id = '03'
    where a.c_id = '01'))
    -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
    select * from student
    where s_id in (select s_id from score t1 group by s_id having group_concat(c_id) = (
    select group_concat(c_id) as str2 from score where s_id = '01') and s_id != '01');
    -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
    select *from student
    where s_id in (select s_id from score t1 group by
    s_id having group_concat(c_id) = (select group_concat(c_id) as str2 from score where s_id = '01')
    and s_id != '01');
    -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
    select * from (course inner join teacher on course.t_id = teacher.t_id) inner join student where t_name != "张三"
    -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    select a.s_id,a.s_name,round(avg(b.s_score)) from student a left join score b on a.s_id = b.s_id
    where a.s_id in( select s_id from score
    where s_score<60 group by s_id having count(1)>= 2)
    group by a.s_id,a.s_name
    -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
    select * from((course inner join teacher on course.t_id = teacher.t_id)
    inner join score on course.c_id = score.c_id)
    inner join student where score.s_score < 60 order by s_score desc
    -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    select score.*,avg_score
    from score right join (select s_id,avg(s_score)as avg_score from score) student
    on score.s_id = student.s_id;
    -- 18、查询每门课程被选修的学生数
    select c_id,COUNT(*) from score GROUP BY c_id
    -- 19、查询出只有两门课程的全部学生的学号和姓名
    select student.*, r.a from student,(select s_id,COUNT(c_id) as a from score group by score.s_id)r where r.a =2 and student.s_id = r.s_id
    -- 20、查询男生、女生人数
    select s_sex,count(*) from student group by s_sex
    -- 21、查询名字中含有"风"字的学生信息
    select * from student where student.s_name like "%风%"

  • 相关阅读:
    cmp函数
    代码6
    代码5
    Google Code Jam在线測试题目--Alien Language
    Android ListView 和 ScrollView 冲突问题
    IOS的一个关于球碰撞的小游戏
    运用smali自己主动注入技术分析android应用程序行为
    《Pro Android Graphics》读书笔记之第二节
    单播、多播(组播)和广播的差别
    Swift闭包(Closure)
  • 原文地址:https://www.cnblogs.com/dsds/p/11792064.html
Copyright © 2020-2023  润新知