MySQL查詢練習


表關系

查詢練習

1、自行創建測試數據;
2、查詢學生總人數;
3、查詢“生物”課程和“物理”課程成績都及格的學生id和姓名;
4、查詢每個年級的班級數,取出班級數最多的前三個年級;
5、查詢平均成績最高和最低的學生的id和姓名以及平均成績;
6、查詢每個年級的學生人數;
7、查詢每位學生的學號,姓名,選課數,平均成績;
8、查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;
9、查詢姓“李”的老師的個數和所帶班級數;
10、查詢班級數小於5的年級id和年級名;
11、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12為低年級,34為中年級,56為高年級),示例結果如下;
    班級id   班級名稱   年級 年級級別
    1  一年一班   一年級    低
12、查詢學過“張三”老師2門課以上的同學的學號、姓名;
13、查詢教授課程超過2門的老師的id和姓名;
14、查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名;
15、查詢沒有帶過高年級的老師id和姓名;
16、查詢學過“張三”老師所教的所有課的同學的學號、姓名;
17、查詢帶過超過2個班級的老師的id和姓名;
18、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;
19、查詢所帶班級數最多的老師id和姓名;
20、查詢有課程成績小於60分的同學的學號、姓名;
21、查詢沒有學全所有課的同學的學號、姓名;
22、查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名;
23、查詢至少學過學號為“1”同學所選課程中任意一門課的其他同學學號和姓名;
24、查詢和“2”號同學學習的課程完全相同的其他同學的學號和姓名;
25、刪除學習“張三”老師課的score表記錄;
26、向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績;
27、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
28、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
29、按各科平均成績從低到高和及格率的百分數從高到低順序;
30、課程平均分從高到低顯示(顯示任課老師);
31、查詢各科成績前三名的記錄(不考慮成績並列情況)
32、查詢每門課程被選修的學生數;
33、查詢選修了2門以上課程的全部學生的學號和姓名;
34、查詢男生、女生的人數,按倒序排列;
35、查詢姓“張”的學生名單;
36、查詢同名同姓學生名單,並統計同名人數;
37、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
38、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;
39、查詢課程編號為“3”且課程成績在80分以上的學生的學號和姓名;
40、求選修了課程的學生人數
41、查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
42、查詢各個課程及相應的選修人數;
43、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
44、查詢每門課程成績最好的前兩名學生id和姓名;
45、檢索至少選修兩門課程的學生學號;
46、查詢沒有學生選修的課程的課程號和課程名;
47、查詢沒帶過任何班級的老師id和姓名;
48、查詢有兩門以上課程超過80分的學生id及其平均成績;
49、檢索“3”課程分數小於60,按分數降序排列的同學學號;
50、刪除編號為“2”的同學的“1”課程的成績;
51、查詢同時選修了物理課和生物課的學生id和姓名;
查詢練習題

建立相關表

create table class_grade(
    gid int primary key auto_increment,
    gname varchar(16) not null unique
);

create table class(
    cid int primary key auto_increment,
    caption varchar(16) not null,
    grade_id int not null,
    foreign key(grade_id) references class_grade(gid)
);

create table student(
    sid int primary key auto_increment,
    sname varchar(16) not null,
    gender enum('女','男') not null default '女',
    class_id int not null,
    foreign key(class_id) references class(cid)
);

create table teacher(
    tid int primary key auto_increment,
    tname varchar(16) not null
);

create table course(
    cid int primary key auto_increment,
    cname varchar(16) not null,
    teacher_id int not null,
    foreign key(teacher_id) references teacher(tid)
);

create table score(
    sid int not null unique auto_increment,
    student_id int not null,
    course_id int not null,
    score int not null,
    primary key(student_id,course_id),
    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 not null unique auto_increment,
    tid int not null,
    cid int not null,
    primary key(tid,cid),
    foreign key(tid) references teacher(tid)
    on delete cascade
    on update cascade,
    foreign key(cid) references class(cid)
    on delete cascade
    on update cascade
);

建表
建表

添加測試數據

# 5 個年級
insert into class_grade(gname) values
('一年級'),
('二年級'),
('三年級'),
('四年級'),
('五年級');


# 5 個老師
insert into teacher(tname) values
('張三'),
('李四'),
('王五'),
('李傑'),
('趙六');

# 12 個班級
insert into class(caption,grade_id) values
('一年一班',1),
('一年二班',1),
('一年三班',1),
('一年四班',1),
('一年五班',1),
('二年一班',2),
('二年二班',2),
('三年一班',3),
('三年二班',3),
('四年一班',4),
('四年二班',4),
('五年一班',5);

# 10 個課程
insert into course(cname,teacher_id) values
('生物',1),
('體育',1),
('物理',2),
('化學',3),
('美術',4),
('音樂',2),
('語文',3),
('數學',4),
('地理',2),
('研究',1);

# 18 個學生
insert into student(sname,gender,class_id) values  # 12個學生
('孫尚香','',1),
('貂蟬','',1),
('劉備','',2),
('孫二娘','',2),
('張飛','',3),
('關羽','',4),
('林黛玉','',5),
('薛寶釵','',6),
('宋江','',6),
('白骨精','',7),
('豬八戒','',8),
('王熙鳳','',1),
('李師師','',2),
('金翠蓮','',9),
('如花','',1),
('沙僧','',2),
('李美麗','',3),
('金角大王','',4);


insert into score(student_id,course_id,score) values
(1,1,60),
(1,2,59),
(1,3,58),
(1,4,22),
(1,5,59),
(1,6,60),
(1,7,99),
(1,8,100),
(1,9,88),
(2,1,99),
(2,2,99),
(2,3,89),
(2,4,60),
(2,5,59),
(2,6,33),
(2,7,56),
(2,8,59),
(2,9,60),
(3,1,59),
(3,3,30),
(3,5,28),
(3,7,70),
(3,9,60),
(4,2,59),
(4,4,100),
(4,6,90),
(4,8,80),
(4,10,88),
(5,1,59),
(5,2,33),
(5,3,12),
(5,4,88),
(6,1,60),
(6,3,99),
(6,5,100),
(6,6,60),
(6,7,59),
(6,8,100),
(6,9,88),
(7,9,20),
(7,1,36),
(7,3,57),
(7,5,60),
(7,8,60),
(7,10,60),
(8,2,61),
(8,4,59),
(8,6,62),
(9,8,59),
(9,1,60),
(9,2,61),
(9,3,21),
(10,1,70),
(10,3,88),
(10,5,68),
(10,9,99),
(11,1,89),
(11,7,99),
(12,3,100),
(12,8,60);


insert into teach2cls(tid,cid) values
(1,1),
(1,2),
(1,3),
(1,5),
(2,4),
(2,6),
(2,8),
(2,9),
(2,1),
(2,5),
(3,7),
(3,1),
(3,3),
(3,5),
(3,9),
(4,7),
(4,2),
(4,4),
(4,6),
(4,8),
(4,1),
(1,12),
(2,12);
添加測試數據

參考SQL

1、自行創建測試數據;
詳見"db5_結構+數據.sql"
2、查詢學生總人數;
select count(sid) from student ;
3、查詢“生物”課程和“物理”課程成績都及格的學生id和姓名;
select
  sid,
  sname
from
  student
where
  sid in(
    select
      score.student_id
    from
      score
    inner join course on score.course_id=course.cid
    where
      course.cname in(
        '生物',
        '物理'
        )
        and score.score >= 60
        group by
            score.student_id
        having
            count(course_id)=2
      );


4、查詢每個年級的班級數,取出班級數最多的前三個年級;

select
    gname
from
    class_grade
where
    gid in (
        select
            grade_id
        from
            class
        group by
            grade_id
        having
            count(cid) in (
                select
                    t1.count_cid
                from
                    (
                        select distinct
                            count(cid) as count_cid
                        from
                            class
                        group by
                            grade_id
                        order by
                            count_cid desc
                        limit 3
                    ) as t1
                )
            );


5、查詢平均成績最高和最低的學生的id和姓名以及平均成績;

select
    student.sid,
    student.sname,
    t1.avg_score
from
    student
inner join (
    select
        student_id,
        avg(score) as avg_score
    from
        score
    group by
        student_id
    having
        avg(score) in (
            (
                select
                    avg(score) as max_avg_score
                from
                    score
                group by
                    student_id
                order by
                    avg(score) desc
                limit 1
            ),
            (
                select
                    avg(score) as min_avg_score
                from
                    score
                group by
                    student_id
                order by
                    avg(score) asc
                limit 1
            )
        )
) as t1 on student.sid = t1.student_id;

6、查詢每個年級的學生人數;
select
    t1.grade_id,
    count(t1.sid) as count_student
from
    (
        select
            student.sid,
            class.grade_id
        from
            student
        inner join class on student.class_id=class.cid
    ) as t1
group by
    t1.grade_id;

7、查詢每位學生的學號,姓名,選課數,平均成績;
select
    student.sid,
    student.sname,
    t1.count_course,
    t1.avg_score
from
    student
left join (
    select
        student_id,
        count(course_id) as count_course,
        avg(score) as avg_score
    from
        score
    group by
        student_id
) as t1 on student.sid = t1.student_id;


8、查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;

select
    student.sname,
    course.cname,
    t1.score
from(

        select
            student_id,
            course_id,
            score
        from
            score
        where
            student_id=2
            and score in(
                (

                    select
                        min(score)
                    from
                        score
                    where
                        student_id=2
                ),
                (
                    select
                        max(score)
                    from
                        score
                    where
                        student_id=2
                )
            )
    ) as t1
    inner join student on t1.student_id=student.sid
    inner join course on t1.course_id=course.cid;

9、查詢姓“李”的老師的個數和所帶班級數;

select
    teacher.tid,
    teacher.tname,
    t1.count_cid
from
    teacher
left join (
    select
        tid,
        count(cid) as count_cid
    from
        teach2cls
    where
        tid in (
            select
                tid
            from
                teacher
            where
                tname like '李%'
        )
    group by
        tid
) as t1 on teacher.tid = t1.tid
where
    teacher.tname like '李%';


10、查詢班級數小於5的年級id和年級名;
select
    gid,
    gname
from
    class_grade
where
    gid in (
        select
            grade_id
        from
            class
        group by
            grade_id
        having
            count(caption)<5
    );


11、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12為低年級,34為中年級,56為高年級),示例結果如下;
    班級id   班級名稱   年級 年級級別
    1  一年一班   一年級    低
# case when ... then

select
    class.cid 班級id,
    class.caption 班級名稱,
    class_grade.gname 年級,
    case when class_grade.gid between 1 and 2  then ''
    when class_grade.gid between 3 and 4  then ''
    when class_grade.gid between 5 and 6  then '' else 0 end as '年級級別'
from
    class,
    class_grade
where
    class.grade_id = class_grade.gid;


12、查詢學過“張三”老師2門課以上的同學的學號、姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        where
            course_id in (
                select
                    course.cid
                from
                    teacher,
                    course
                where
                    teacher.tid = course.teacher_id
                    and teacher.tname = '張三'
            )
        group by
            student_id
        having
            count(course_id) > 2
    );

13、查詢教授課程超過2門的老師的id和姓名;

select
    tid,
    tname
from
    teacher
where
    tid in (
        select
            teacher_id
        from
            course
        group by
            teacher_id
        having
            count(cid) > 2
    );

14、查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select distinct
            student_id
        from
            score
        where
            course_id in (
                1,
                2
            )
    );

15、查詢沒有帶過高年級的老師id和姓名;

select
    tid,
    tname
from
    teacher
where
    tid not in (
        select
            tid
        from
            teach2cls
        where
            cid in (
                select
                    cid
                from
                    class
                where
                    grade_id in (
                        5,
                        6
                    )
            )
    );

16、查詢學過“張三”老師所教的所有課的同學的學號、姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        inner join course on score.course_id=course.cid
        where teacher_id in(
            select
                sid
            from
                teacher
            where
                sname='張三'

        )


    )

17、查詢帶過超過2個班級的老師的id和姓名;

select
    tid,
    tname
from
    teacher
where
    tid in (
        select
            teacher_id
        from
            course
        group by
            teacher_id
        having
            count(cid) > 2
    );

18、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            t1.student_id
        from
            (
                select
                    student_id,
                    score
                from
                    score
                where
                    course_id = 2
                group by
                    student_id
            ) as t1,
            (
                select
                    student_id,
                    score
                from
                    score
                where
                    course_id = 1
                group by
                    student_id
            ) as t2
        where
            t1.student_id = t2.student_id
            and t1.score < t2.score
    );

19、查詢所帶班級數最多的老師id和姓名;

# 考慮班級數並列最多的情況

select
    tid,
    tname
from
    teacher
where
    tid in (
        select
            tid
        from
            teach2cls
        group by
            tid
        having
            count(cid) = (
                select
                    count(cid)
                from
                    teach2cls
                group by
                    tid
                order by
                    count(cid) desc
                limit 1
            )
    );

20、查詢有課程成績小於60分的同學的學號、姓名;
select
    sid,
    sname
from
    student
where
    sid in (
        select distinct
            student_id
        from
            score
        where
            score < 60
    );

21、查詢沒有學全所有課的同學的學號、姓名;
select
    sid,
    sname
from
    student
where
    sid not in (
        select
            student_id
        from
            score
        group by
            student_id
        having
            count(course_id) = (
                select
                    count(cid)
                from
                    course
            )
    );
22、查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        where
            course_id in (
                select
                    course_id
                from
                    score
                where
                    student_id = 1
            )
        group by
            student_id
    );

23、查詢至少學過學號為“1”同學所選課程中任意一門課的其他同學學號和姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        where
            course_id in (
                select
                    course_id
                from
                    score
                where
                    student_id = 1
            )
        group by
            student_id
    )
    and sid != 1;

24、查詢和“2”號同學學習的課程完全相同的其他同學的學號和姓名;
select
    sid,
    sname
from
    student
where
    sid in (
        select
            score.student_id
        from
            score,
            (
                select
                    course_id
                from
                    score
                where
                    student_id = 2
            ) as t1
        where
            score.course_id = t1.course_id
            and score.student_id != 2
        group by
            score.student_id
        having
            count(score.course_id) = (
                select
                    count(course_id)
                from
                    score
                where
                    student_id = 2
            )
    );


25、刪除學習“張三”老師課的score表記錄;
delete from
    score
where
    course_id in (
        select
            course.cid
        from
            course,
            teacher
        where
            course.teacher_id = teacher.tid
            and teacher.tname = '張三'
    );

26、向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績;

# 實際結果會和上面一題有沖突,張三老師所教的2號課程

insert into score(student_id,course_id,score)
select
    t1.sid,
    2,
    t2.avg_score
from
    (
        select
            sid
        from
            student
        where
            sid not in (
                select
                    student_id
                from
                    score
                where
                    course_id = 2
            )
    ) as t1,
    (
        select
            avg(score) as avg_score
        from
            score
        where
            course_id = 2
    ) as t2;

27、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;

# 有效成績含所有課程,包括語數英

select
    t1.student_id as 學生ID,
    (select score.score from score left join course on score.course_id = course.cid where course.cname = '語文' and score.student_id = t1.student_id) as 語文,
    (select score.score from score left join course on score.course_id = course.cid where course.cname = '數學' and score.student_id = t1.student_id) as 數學,
    (select score.score from score left join course on score.course_id = course.cid where course.cname = '音樂' and score.student_id = t1.student_id) as 英語,
    count(t1.course_id) as 有效課程數,
    avg(t1.score) as 有效平均分
from
    score as t1
group by
    t1.student_id
order by
    avg(t1.score) asc;

28、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;

select
    course.cid as 課程ID,
    max(score.score) as 最高分,
    min(score.score) as 最低分
from
    course
left join score on course.cid = score.course_id
group by
    score.course_id;

29、按各科平均成績從低到高和及格率的百分數從高到低順序;

select
    course_id,
    avg(score) as avg_score,
    sum(case when score.score > 60 then 1 else 0 end) / count(1) * 100 as 及格率
from
    score
group by
    course_id
order by
    avg(score) desc,
    及格率 asc;

30、課程平均分從高到低顯示(顯示任課老師);
select
    course.cname,
    t1.avg_score,
    teacher.tname
from
    course,
    teacher,
    (
        select
            course_id,
            avg(score) as avg_score
        from
            score
        group by
            course_id
        order by
            avg_score desc
    ) as t1
where
    course.cid = t1.course_id
    and course.teacher_id = teacher.tid
order by
    t1.avg_score desc;

31、查詢各科成績前三名的記錄(不考慮成績並列情況)
select
    score.sid,
    score.student_id,
    score.course_id,
    score.sid,
    t1.first_score,
    t1.second_score,
    t1.third_score
from
    score
inner join (
    select
        s1.sid,
        (select score from score as s2 where s1.course_id = s2.course_id order by score desc limit 0,1) as first_score,
        (select score from score as s3 where s1.course_id = s3.course_id order by score desc limit 1,1) as second_score,
        (select score from score as s4 where s1.course_id = s4.course_id order by score desc limit 2,1) as third_score
    from
        score as s1
) as t1 on score.sid = t1.sid
where
    score.score in (
        t1.first_score,
        t1.second_score,
        t1.third_score
    );

32、查詢每門課程被選修的學生數;

# 包含了沒有被選修的課程顯示0
select
    course.cid,
    ifnull(t1.count_students,0) as count_student
from
    course
left join (
    select
        course_id,
        count(student_id) as count_students
    from
        score
    group by
        course_id
) as t1 on course.cid = t1.course_id;

33、查詢選修了2門以上課程的全部學生的學號和姓名;

select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        group by
            student_id
        having
            count(course_id) > 2
    );

34、查詢男生、女生的人數,按倒序排列;

select
    gender,
    count(sid) as count_student
from
    student
group by
    gender
order by
    count_student desc;

35、查詢姓“張”的學生名單;

select
    student.sid,
    student.sname,
    student.gender,
    class.caption
from
    student
inner join class on student.class_id = class.cid
where
    student.sname like '張%';

36、查詢同名同姓學生名單,並統計同名人數;
select
    sname,
    count(sname) as count_sname
from
    student
group by
    sname
having
    count(sname) > 1;

37、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;

select
    course_id,
    avg(score) as avg_score
from
    score
group by
    course_id
order by
    avg_score,
    course_id desc;

38、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;
select
    student.sname,
    t1.score
from
    student
inner join (
    select
        score.student_id,
        score.score
    from
        score
    inner join course on score.course_id = course.cid
    where
        course.cname = '數學'
        and score.score < 60
) as t1 on student.sid = t1.student_id;
39、查詢課程編號為“3”且課程成績在80分以上的學生的學號和姓名;
select
    sid,
    sname
from
    student
where sid in (
    select
        student_id
    from
        score
    where
        course_id = 3
        and score > 80
);
40、求選修了課程的學生人數
select
    course_id,
    count(student_id) as count_student
from
    score
group by
    course_id;

41、查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
# 教的課可能包含多門,按課程id來分的!包含了最高,最低成績相等的情況

select
    student.sname,
    t2.course_id,
    t2.score,
    t2.max_score,
    t2.min_score
from
    student
inner join (
    select
        score.student_id,
        score.course_id,
        score.score,
        t1.max_score,
        t1.min_score
    from
        score,
        (
            select
                course_id,
                max(score) as max_score ,
                min(score) as min_score
            from
                score
            where
                course_id in (
                    select
                        cid
                    from
                        course
                    inner join teacher on course.teacher_id = teacher.tid
                    where
                        teacher.tname = '王五'
                )
            group by
                course_id
        ) as t1
    where
        score.course_id = t1.course_id
        and score.score in (
            max_score,
            min_score
        )
) as t2 on student.sid = t2.student_id;

42、查詢各個課程及相應的選修人數;
# 包含了沒有被選修的課程顯示0

select
    course.cname,
    ifnull(t1.count_student,0) as count_student
from
    course
left join (
    select
        course_id,
        count(student_id) as count_student
    from
        score
    group by
        course_id
) as t1 on course.cid = t1.course_id;
43、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
# 不同的學生之間,成績相同 這里有問題!!
select distinct
    s1.student_id,
    s2.student_id,
    s1.course_id as s1_course_id,
    s2.course_id as s2_course_id,
    s1.score,
    s2.score
from
    score as s1,
    score as s2
where
    s1.course_id != s2.course_id
    and s1.score = s2.score;

select distinct             # 同一個學生,成績相同
    s1.student_id,
    s2.student_id,
    s1.course_id as s1_course_id,
    s2.course_id as s2_course_id,
    s1.score,
    s2.score
from
    score as s1,
    score as s2
where
    s1.student_id = s2.student_id
    and s1.course_id != s2.course_id
    and s1.score = s2.score;
44、查詢每門課程成績最好的前兩名學生id和姓名;

# 這個有排名 包括了成績相同的!
select
    c.sname,
    d.cname,
    a.score
from
    score a
inner join (
    select course_id, score, rank
        from (
             select a.course_id, a.score, count(*) as rank
                from (select course_id, score
                       from score
                        group by course_id, score
                        order by course_id, score desc) a
                inner join (select course_id, score
                            from score
                            group by course_id, score
                            order by course_id, score desc) b
                on a.course_id = b.course_id
                    and a.score <= b.score
             group by course_id, score
        ) t1
        where rank in (1, 2)
        order by course_id, rank
   ) b
on a.course_id = b.course_id
    and a.score = b.score
inner join student c
    on a.student_id = c.sid
inner join course d
    on a.course_id = d.cid;

45、檢索至少選修兩門課程的學生學號;
select
    student_id
from
    score
group by
    student_id
having
    count(course_id) >= 2;
46、查詢沒有學生選修的課程的課程號和課程名;

select
    cid,
    cname
from
    course
where
    cid not in (
        select
            course_id
        from
            score
        group by
            course_id
    );

47、查詢沒帶過任何班級的老師id和姓名;
select
    tid,
    tname
from
    teacher
where
    tid not in (
        select
            tid
        from
            teach2cls
        group by
            tid
    );

48、查詢有兩門以上課程超過80分的學生id及其平均成績;
select
    student_id,
    avg(score) as 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
    sid = (
        select
            t1.sid
        from
            (
                select
                    sid
                from
                    score
                where
                    student_id = 2
                    and course_id = 1
            ) as t1
    );

51、查詢同時選修了物理課和生物課的學生id和姓名;
select
    sid,
    sname
from
    student
where
    sid in (
        select
            student_id
        from
            score
        where
            course_id in (
                select
                    cid
                from
                    course
                where
                    course.cname in (
                        '物理',
                        '生物'
                    )
            )
        group by
            student_id
        having
            count(course_id) = 2
    );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM