• 【mysql】排序方操作50题练习及其答案


    1.创建数据库、相关表,并插入数据
    create database homework;
    use homework;

    create table class_grade(gid int primary key auto_increment, gname char(16));
    create table class(cid int primary key auto_increment, caption char(16), grade_id int, foreign key(grade_id) references class_grade(gid) on delete cascade on update cascade);
    create table student(sid int primary key auto_increment, sname char(16), gender char(6) not null default '男', class_id int,foreign key(class_id) references class(cid) on delete cascade on update cascade);
    create table teacher(tid int primary key auto_increment, tname char(16));
    create table course(cid int primary key auto_increment, cname char(16), teacher_id int, foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade);
    create table score(sid int primary key auto_increment, student_id int,course_id int,score int unsigned, foreign key(student_id) references student(sid) on delete cascade on update cascade,foreign key(course_id) references course(cid) on delete cascade on update cascade);
    create table teach2cls(tcid int primary key auto_increment, tid int, cid int, foreign key(tid) references teacher(tid), foreign key(cid) references class(cid) on delete cascade on update cascade);

    insert into class_grade(gname) values('一年级'),('二年级'),('三年级');
    insert into class(caption, grade_id) values('一年一班',1),('二年一班',2),('三年一班',3);
    insert into student(sname, gender, class_id) values('乔丹','女',1),('艾弗森', '女', 1), ('科比', '男', 2);
    insert into teacher(tname) values('张三'),('李四'),('王五');
    insert into course(cname, teacher_id) values('生物',1), ('体育',1), ('物理',2);
    insert into score(student_id, course_id, score) values(1,1,60),(1,2,59),(2,2,99);
    insert into teach2cls(tid, cid) values(1,1),(1,2),(2,1),(3,2);

    2. 查询学生总人数
    select count(sid) from student;

    3.查询'生物' 和 '物理' 课程成绩都及格的学生
    select sid,sname from student where sid in(select student_id from score as t1 right join (select cid from course where cname in('生物', '物理')) as t2 on t1.course_id= t2.cid where score >= 60 group by student_id);

    4.查询每个年级的班级数,取出班级数最多的三个年级
    select * from class_grade as t1 right join(select grade_id,count(cid) as count from class group by(grade_id))as t2 on t1.gid=t2.grade_id order by count desc limit 3;

    5.查询平均成绩最高和最低的学生ID和姓名以及成绩
    select sid,sname,avg from student as t1 right join (
    select student_id,avg(score) as avg from score group by student_id
    ) as t2 on t1.sid=t2.student_id
    where avg in(
    (select max(avg) from (select avg(score) as avg from score group by student_id) as tmax),
    (select min(avg) from (select avg(score) as avg from score group by student_id) as tmin)
    );

    6.查询每个年级的学生人数
    select gname,count(sid) from (select * from student right join class on student.class_id=class.cid) as t1
    left join
    (select * from class left join class_grade on class.grade_id=class_grade.gid) as t2
    on t1.cid=t2.cid group by gid;

    7.每位学生的学号, 姓名, 选课数, 平均成绩
    select sid,sname,count(course_id),avg(score) from (select student.sid,sname,course_id,score from student left join score on student.sid=score.student_id) as t1 group by sid;

    8.查询学生编号为2的学生姓名,该学生最高成绩的课程名, 成绩最低的课程名及分数
    set @ssid =1;
    select sname,cname,score from
    (select student.sid,sname,course_id,score from score left join student on score.student_id=student.sid
    where student_id=@ssid and score in(
    (select max(score) from score where student_id=@ssid),
    (select min(score) from score where student_id=@ssid))
    )as t1 left join course as t2 on t1.course_id=t2.cid
    order by score;

    9.查询姓'李'的老师个数和所带班级数
    set @first_name='李%';
    select GROUP_CONCAT(tname),count(tname),count(cid) from teacher as t1 left join teach2cls as t2 on t1.tid=t2.tid where tname like @first_name ;

    10.查询班级数小于5的年级id和年级名
    select gid,gname from class_grade left join class on class_grade.gid=class.grade_id group by gid having count(cid)<5;

    11.输出班级信息并增加一列显示年级的文字级别(1,2为低年级, 3,4为中年级, 5,6为高年级)
    select
    cid,
    caption,
    gname,
    if((select grade_id from class where cid=t1.cid)<3,'低年级',
    if((select grade_id from class where cid=t1.cid)<5,'中年级','高年级')
    ) as grade_level
    from class as t1 left join class_grade as t2 on t1.grade_id=t2.gid;

    12.张三老师2门课以上的粉丝id,名字
    select student.sid,sname,count(student.sid) from student
    left join (select * from (select * from teacher
    right join course on teacher.tid=course.teacher_id where tname='张三') as t1
    right join score on t1.cid=score.course_id) as t2 on student.sid=t2.student_id
    group by student.sid having count(student.sid)>=2;

    13.查询教授超过两门课的老师的ID 及 名字
    select tid,tname from teacher left join course on teacher.tid=course.teacher_id group by tid having count(cid)>=2;

    14.查询学过编号1 课程 和编号2课程的同学的学号与姓名
    select student_id,sname from score left join student on score.student_id=student.sid group by student_id having count(course_id)>=2;

    15.查询没有带过高年级的老师id和姓名
    select tid,tname from (select teacher.tid,tname,cid from teacher left join teach2cls on teacher.tid=teach2cls.tid) as t1 left join class as t2 on t1.cid=t2.cid group by tid having max(grade_id)<4;

    16.查询学过'张三'老师所教的所有课的同学学号、姓名
    select sid,sname from (select * from teacher left join course on teacher.tid=course.teacher_id) as t1
    left join (select student.sid,sname,course_id from student left join score on student.sid=score.student_id) as t2 on t1.cid=t2.course_id where tname='张三';

    17.查询带超过2个班级的老师id和姓名
    select teacher.tid,teacher.tname from teacher left join teach2cls on teacher.tid=teach2cls.tid group by tid having count(cid)>=2;

    18.查询课程编号2的成绩比课程编号1课程低的所有同学的学号,姓名
    select sid,sname from student as t1 right join (select student_id,score as score_2,(select score from score where course_id=1 and student_id=a.student_id) as score_1 from score a where course_id=2 having score_1>score) as t2 on t1.sid=t2.student_id;

    19.查询所带班级数最多的老师ID和姓名
    select teacher.tid,tname,count(cid) from teacher left join teach2cls on teacher.tid=teach2cls.tid group by tid order by count(cid) desc limit 1;

    20.查询有成绩不及格的同学学号、姓名
    select distinct student.sid,sname from student left join score on student.sid=score.student_id where score<60;

    21.没有学全所有课的同学学号、姓名
    select student.sid,sname,count(course_id) from student left join score on student.sid=score.student_id group by student.sid having count(course_id)<(select count(cid) from course);

    22.至少有一门课与学号为 1 的同学所学相同的学生学号及姓名
    select distinct student.sid,sname from student left join score on student.sid=score.student_id where course_id in (select course_id from score where student_id=1);

    23.22题,其他同学
    select distinct student.sid,sname from student left join score on student.sid=score.student_id where student.sid != 1 and course_id in (select course_id from score where student_id=1);

    24.查询与2号同学所学习课程完全相同的同学学号及姓名 用group_concat()拼接
    select student.sid,sname,group_concat(course_id) as courses from student left join score on student.sid=score.student_id where student.sid!=2 group by student.sid having courses = (select group_concat(course_id) from score group by student_id having student_id=2);

    25.删除张三老师的课的score记录; (课程1,课程2的成绩记录全被删了。。。)
    delete from score where course_id in(select cid from course left join teacher on course.teacher_id=teacher.tid where tname='张三');

    26.向score插入一些记录,这些记录要求符合以下条件:1.没有上过编号2课程同学学号,2.插入2号课程的平均成绩
    ## 插入内容为: student_id=3, course_id=2,score=(avg(score))
    insert into score(student_id,course_id,score) select sid,2,(select avg(score) from score group by course_id having course_id=2) from student where sid not in(SELECT student_id from score where course_id=2);

    27.按平均成绩从低到高显示所有学生的“语文”“数学”“英语”三门的课程成绩,按如下内容显示:
    学生ID,语文,数学,英语,有效课程数,有效平均分
    select student_id as 学生ID,
    (select score from score left join course on score.course_id=course.cid where cname='语文' and student_id=t1.student_id) as '语文',
    (select score from score left join course on score.course_id=course.cid where cname='数学' and student_id=t1.student_id) as '数学',
    (select score from score left join course on score.course_id=course.cid where cname='英语' and student_id=t1.student_id) as '英语',
    count(course_id) as 有效课程数,
    avg(score) as 有效平均分
    from score as t1 left join course on t1.course_id=course.cid where cname in('语文','数学','英语') group by student_id order by 有效平均分 asc;

    28.查询各科成绩最高分与最低分显示形式: 课程ID 最高课程 最低成绩
    select
    course.cid as 课程ID,
    (select max(score) from score group by course_id having course_id=t.course_id) as 最高成绩,
    (select min(score) from score group by course_id having course_id=t.course_id) as 最低成绩
    from score as t right join course on t.course_id=course.cid group by course.cid;

    29.按各科平均成绩从低到高和及格率的百分数从高到低排序
    select
    course.cid as 课程ID,
    avg(score) as 平均成绩,
    concat(ifnull((select count(student_id) from score where score > 60 group by course_id having course_id=t.course_id),0)/
    (select count(student_id) from score group by course_id having course_id=t.course_id)*100
    , '%') as 及格率
    from score as t right join course on t.course_id=course.cid
    group by course.cid
    order by 平均成绩 asc ,及格率 desc;

    30.课程平均分从高到低显示(现实任课老师)
    select cid,cname,avg(score),tname
    from teacher as t1
    right join (select * from score right join course on score.course_id=course.cid) as t2
    on t1.tid=t2.teacher_id
    group by cid
    order by avg(score) desc;

    31.查询各科成绩前三名的记录(),不考虑并列的情况;
    select
    a.course_id as 课程ID,
    a.score as 成绩,
    count(a.course_id) as 排名
    from score a left join score b on a.course_id=b.course_id and a.score<=b.score
    group by a.course_id,a.score
    having count(a.course_id)<4
    order by a.course_id,a.score desc;

    32.查询每门课程被选修的学生数
    select cid,cname,count(student_id) as 选修学生数 from score right join course on score.course_id=course.cid group by cid;

    33.查询选修了2门以上课程的全部学生的学号和姓名
    select student.sid,sname from student left join score on student.sid=score.student_id group by student.sid having count(course_id)>=2;

    34.查询男生女生人数,按倒序排列
    select gender,count(sid) from student group by gender order by count(sid) desc;

    35.查询姓张的学生名单
    select * from student where sname like '张%';

    36.查询同名同姓学生名单,并统计同名人数
    select sname,group_concat(sid) as 学号,count(sid) as 重名人数 from student group by sname having count(sid)>1;

    37.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
    select cid as 课程号,cname as 课程名,ifnull(avg(score),0) as 平均成绩 from score right join course on score.course_id=course.cid group by cid order by 平均成绩 asc,课程号 desc;

    38.查询课程名称为数学且分数低于60的学生姓名与分数
    select sname,score from student as t1 right join (select * from score right join course on score.course_id=course.cid where cname = '数学' and score<60) as t2 on t1.sid=t2.student_id;

    39.查询课程编号为3 且课程成绩在80分以上的学生学号及姓名
    select student.sid,sname from student left join score on student.sid=score.student_id where course_id=3 and score>=80;

    40.求选修了课程的学生人数
    select count(distinct sid) from score;

    41.查询选修王五老师所教授课程的学生中成绩最高和最低的学生姓名及其成绩
    select * from (select '最低成绩',sname,score from score left join student on score.student_id=student.sid where course_id in(
    select cid from course left join teacher on course.teacher_id=teacher.tid where tname='王五') order by score limit 1) as a
    union
    select * from (select '最高成绩',sname,score from score left join student on score.student_id=student.sid where course_id in(
    select cid from course left join teacher on course.teacher_id=teacher.tid where tname='王五') order by score desc limit 1) as b;

    42.查询各个课程及其选修人数
    select cname,count(student_id) from score right join course on score.course_id=course.cid group by cname;

    43.查询课程不同但成绩相同的学生的学号、课程号、学生成绩
    select distinct a.student_id as 学号,a.course_id as 课程号, a.score as 成绩
    from score a left join score b on a.score = b.score
    where a.course_id != b.course_id;

    44.查询每门课程成绩最好的前两名学生id 和姓名
    select course_id as 课程编号,
    sid as 学号,
    sname as 姓名
    from student right join (select a.course_id,a.student_id,a.score,count(a.course_id) from score a left join score b on a.course_id=b.course_id and a.score<b.score group by a.course_id,a.student_id,a.score having count(a.course_id)<2 order by course_id) as t on student.sid=t.student_id order by course_id;

    45.检索至少选修2门课程的学生学号
    select student_id from score group by student_id having count(course_id)>=2;

    46.查询没有学生选修的课程号及课程名
    select cid,cname from score right join course on score.course_id=course.cid group by cid having count(student_id)<1;

    47.查询没有带过任何班级的老师id 和姓名
    select teacher.tid,tname from teacher left join teach2cls on teacher.tid=teach2cls.tid group by teacher.tid having count(cid)<1;

    48.查询2门以上课程超过80分的同学id 及其平均成绩
    select student_id,avg(score) from score where student_id in (select student_id from score where score > 80 group by student_id having count(course_id)>=2) group by student_id;

    49 检索 3 号课程分数小于60的同学学号,按分数降序排列
    select student_id,score from score where course_id=3 and score<60 order by score desc;

    50 删除编号为2的同学的1课程成绩
    delete from score where student_id=2 and course_id=1;

    51 查询同时修了物理课和生物课的学生id和姓名
    select student.sid,sname from student right join score on student.sid=score.student_id where course_id in(select cid from course where cname in('物理','生物')) group by student.sid having count(course_id)=2;

  • 相关阅读:
    自定义组件要加@click方法
    绑定样式
    647. Palindromic Substrings
    215. Kth Largest Element in an Array
    448. Find All Numbers Disappeared in an Array
    287. Find the Duplicate Number
    283. Move Zeroes
    234. Palindrome Linked List
    202. Happy Number
    217. Contains Duplicate
  • 原文地址:https://www.cnblogs.com/caya-yuan/p/9307551.html
Copyright © 2020-2023  润新知