2021-03-02

Mysql 综合练习




1、查询男生、女生的人数;

mysql> select gender, count(sid) from student group by gender;


2、查询姓“张”的学生名单;


3、课程平均分从高到低显示

mysql> select course_id, cname,avg_num from course inner join

(select course_id,avg(num) as avg_num from score group by course_id) as t1

where cid=t1.course_id

group by avg_num desc;


4、查询有课程成绩小于60分的同学的学号、姓名;

mysql> select sname, sid from student where sid in (select distinct student_id from score where num < 60);


5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;

select sid,sname from student where sid in

(select distinct student_id from score where course_id in

(select course_id from score where student_id  = 1)) ;


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


select * from student inner join (select student_id, count(course_id) from score group by student_id having count(course_id)=1) as bb on sid = bb.student_id ;



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

select course_id, max(num),min(num) from score group by course_id ;


8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

# 先找到每个学生的课程编号“1”的和课程编号“2”的成绩组成一张表

select t1.student_id from(select num num2,student_id from score where course_id = 2) t2 inner join(select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id

# 再找到课程编号“2”的成绩比课程编号“1”课程低的所有学生的学号

select t1.student_id from(select num num2,student_id from score where course_id = 2) t2 inner join(select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1

# 再找到所有学生的学号、姓名

select sid,sname from student where sid in(select t1.student_id from(select num num2,student_id from score where course_id = 2) t2 inner join(select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1);


9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;

select student_id,shengwu_score,wuli_score from (

(select student_id, num as shengwu_score from score where course_id in

(select cid from course where cname="生物")) as t1

inner join

(select student_id as student_id2, num as wuli_score from score where course_id in

(select cid from course where cname="物理")) as t2 

on t1.student_id = t2.student_id2)

where shengwu_score > wuli_score;




10、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id, avg(num) from score group by student_id having avg(num) > 60;



11、查询所有同学的学号、姓名、选课数、总成绩;

select student_id,sname, sum(num), count(course_id) from score

inner join  student on student.sid = student_id group by student_id;


12、查询姓“李”的老师的个数;

select count(tid ) as li_ciunter from ( select * from teacher where tname like "李%") as teacher;


13、查询没学过“张磊老师”课的同学的学号、姓名;


select distinct sname, student_id from student inner join score on student.sid =student_id where

course_id not in

(select tid from teacher where tname like "张磊老师");


14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;

select sname, sid from student where sid in (

select t1.1_id  from

(select student_id as 1_id from score where course_id = 1 ) t1

inner join

(select student_id as 2_id from score where course_id = 2)t2

on t1.1_id = t2.2_id);



15、查询学过“李平老师”所教的所有课的同学的学号、姓名;

select * from student where sid in (

select distinct student_id from score where course_id in

(select cid from course where teacher_id  = (

select tid from teacher where tname = "李平老师")));


你可能感兴趣的:(2021-03-02)