查询语句
1、查询所有的课程的名称以及对应的任课老师姓名
SELECT cname,tname from course inner join teacher ON course.teacher_id = teacher.tid;
2、查询学生表中男女生各有多少人
select gender,COUNT(sid) from student GROUP BY gender;
3、查询物理成绩等于100的学生的姓名
SELECT sname from student where sid in (
SELECT student_id from score where course_id = (SELECT cid from course where cname = '物理') and num = 100
);
4、查询平均成绩大于八十分的同学的姓名和平均成绩
SELECT student.sname,t1.avg_num from student inner join
(SELECT student_id,AVG(num) avg_num from score GROUP BY student_id
HAVING avg(num) > 80) as t1
on student.sid = t1.student_id;
5、查询所有学生的学号,姓名,选课数,总成绩
SELECT student.sid,student.sname,t1.course_num,t1.total_num from student inner JOIN
(SELECT
student_id,
count(course_id) course_num,
sum(num) total_num
FROM
score
GROUP BY
student_id) as t1
on student.sid = t1.student_id;
6、 查询姓李老师的个数
SELECT COUNT(1) from teacher where tname like '李%';
7、 查询没有报李平老师课的学生姓名
SELECT
sname
FROM
student
WHERE
sid NOT IN (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
teacher_id = (
SELECT
tid
FROM
teacher
WHERE
tname = '李平老师'
)
)
);
8、 查询物理课程比生物课程高的学生的学号
SELECT t1.student_id from
(SELECT student_id,num from score where course_id = (
SELECT cid from course where cname = '物理'
)) as t1
inner join
(SELECT student_id,num from score where course_id = (
SELECT cid from course where cname = '生物'
)) as t2
on t1.student_id = t2.student_id
where t1.num > t2.num;
9、 查询没有同时选修物理课程和体育课程的学生姓名
SELECT sname from student where sid in (
SELECT student_id from score LEFT JOIN course
on score.course_id = course.cid
WHERE course.cname in ('物理','体育')
GROUP BY student_id
HAVING count(sid) < 2
);
10、查询挂科超过两门(包括两门)的学生姓名和班级
SELECT sname,caption from student LEFT JOIN class
on student.class_id = class.cid
where student.sid in (
SELECT student_id from score where num < 60 GROUP BY student_id
HAVING COUNT(course_id) >= 2
)
;
11 、查询选修了所有课程的学生姓名
SELECT student_id from score GROUP BY student_id
HAVING count(course_id) = (
SELECT count(1) from course
);
12、查询李平老师教的课程的所有成绩记录
SELECT * from score where course_id in (
SELECT cid from course inner JOIN teacher
on course.teacher_id = teacher.tid
WHERE tname = '李平老师'
);
13、查询全部学生都选修了的课程号和课程名
SELECT course_id from score group by course_id
HAVING count(student_id) = (
SELECT count(1) from student
);
14、查询每门课程被选修的次数
SELECT course.cname,t1.count_student FROM course
INNER JOIN
(
SELECT course_id,count(student_id) count_student from score GROUP BY course_id
) as t1
ON course.cid = t1.course_id;
15、查询只选修了一门课程的学生姓名和学号
SELECT student_id from score GROUP BY student_id
HAVING COUNT(course_id) = 1;
16、查询所有学生考出的总成绩并按从高到低排序(成绩去重)
SELECT DISTINCT sum(num) sum_num from score group by student_id
ORDER BY sum_num desc;
17、查询平均成绩大于85的学生姓名和平均成绩
SELECT student.sname,t1.avg_num from student inner join
(
SELECT student_id,avg(num) avg_num from score GROUP BY student_id having avg(num) > 85
) as t1
on student.sid = t1.student_id;
18、查询生物成绩不及格的学生姓名和对应生物分数
SELECT sname,t1.num from student
INNER JOIN
(
SELECT student_id,num from score LEFT JOIN course
on score.course_id = course.cid
where course.cname = '生物' and score.num < 60
) as t1
on student.sid = t1.student_id;
19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
20、查询每门课程成绩最好的前两名学生姓名
SELECT * from score ORDER BY course_id,num desc;
#取得课程编号与第一高的成绩:course_id,first_num
SELECT course_id,max(num) first_num from score GROUP BY course_id;
#取得课程编号与第二高的成绩:course_id,second_num
SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id
) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id
;
#链表得到一张新表,新表包含课程编号与这门课程前两名的成绩分数
select t1.course_id,t1.first_num,t2.second_num from
(SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
inner join
(SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id
) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2
on t1.course_id = t2.course_id;
#取前两名学生的编号
SELECT score.course_id,score.student_id from score LEFT JOIN (
select t1.course_id,t1.first_num,t2.second_num from
(SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
inner join
(SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id
) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2
on t1.course_id = t2.course_id
) as t3
on score.course_id = t3.course_id
where score.num >= t3.second_num and score.num <= t3.first_num
;
SELECT t4.course_id,student.sname from student inner join
(
SELECT score.course_id,score.student_id from score LEFT JOIN (
select t1.course_id,t1.first_num,t2.second_num from
(SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
inner join
(SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id
) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2
on t1.course_id = t2.course_id
) as t3
on score.course_id = t3.course_id
where score.num >= t3.second_num and score.num <= t3.first_num
) as t4
on student.sid = t4.student_id
ORDER BY t4.course_id
;
select student.sname,t.course_id,t.num from student INNER JOIN
(
select
s1.student_id,s1.course_id,s1.num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
from
score as s1
) as t
on student.sid = t.student_id
where t.num in (t.first_num,t.second_num)
ORDER BY t.course_id
;
SELECT sid from score as s1 ;