# --1.学生表
# Student(S#,Sname,Sage,Ssex) --
#
# --2.课程表
# Course(C#,Cname,T#) --
#
# --3.教师表
# Teacher(T#,Tname) --
#
# --4.成绩表
# SC(S#,C#,score) --
# 完成下列查询需求:
#查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s.*,b.score 课程1,c.score 课程2 from student s, sc b,sc c
where s.s_id=b.s_id and b.s_id=c.s_id and b.c_id=158 and c.c_id=55 and b.score>c.score;
#查询平均成绩大于等于60分的同学的学生编号、学生姓名和平均成绩
select s.s_id 编号,s.sname 姓名,avg(sc.score) 平均成绩 from student s ,sc
where s.s_id=sc.s_id
group by s.s_id ,s.sname
having avg(sc.score)>=60;
#查询平均成绩小于60分的同学的学生编号、学生姓名和平均成绩
select s.s_id 编号,s.sname 姓名,avg(sc.score) 平均成绩 from student s ,sc
where s.s_id=sc.s_id
group by s.s_id ,s.sname
having avg(sc.score)<60;
#查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select s.s_id 编号,s.sname 姓名,count(sc.c_id) 选课总数,SUM(sc.score) 总成绩 from student s ,sc
where s.s_id=sc.s_id
group by s.s_id ,s.sname;
#查询"李"姓老师的数量
select COUNT(*) FROM teacher
where tname like '李%';
#查询学过"张三"老师所授课程的同学的信息
SELECT * FROM student WHERE s_id IN (SELECT s_id FROM sc WHERE c_id IN
(SELECT c_id FROM course WHERE t_id IN
(SELECT t_id FROM teacher WHERE tname LIKE '蓝老师')));
#查询没学过"张三"老师所授课程的同学的信息
select * from student where s_id not in
(select distinct s_id FROM sc
where c_id=(select c_id from course where t_id=
(select t_id from teacher where tname like '蓝老师')));
#查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
# 方法一:
select * from student
where s_id in(select s.s_id from sc s,sc c
where s.s_id=c.s_id and s.c_id=158 and c.c_id=155);
# 方法二:
select * from student s
where s.s_id in(select s_id from sc
where c_id=2) and s.s_id in(select s_id from sc
where c_id=1);
#查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
# 方法一:
select * from student s
where s.s_id not in(select s_id from sc
where c_id=155) and s.s_id in(select s_id from sc
where c_id=158);
#查询没有学全所有课程的同学的信息
select * from student
where s_id in (select s_id from sc
group by s_id
having count(c_id)<(select COUNT(c_id) from course));
#查询至少有一门课与学号为"01"的同学所学的课程相同的同学的信息
select * from student
where s_id in (
select DISTINCT(s_id) from sc
where c_id in (select c_id from sc
where s_id=1) and s_id<>1);
#查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT avg(score)FROM sc
GROUP BY c_id
ORDER BY avg(score) DESC ,c_id ASC ;
#查询平均成绩大于等于70分的所有学生的学号、姓名和平均成绩
SELECT s.s_id ,s.sname, avg(sc.score)FROM student s,sc
WHERE sc.s_id=s.s_id
GROUP BY s.s_id
HAVING avg(score)>70;
#查询课程名称为"数学",且分数低于60分的学生姓名和分数
SELECT s.sname, sc.score FROM student s,sc,course c
WHERE s.s_id=sc.s_id
AND c.cname LIKE 'java'
AND sc.c_id=c.c_id
AND sc.score>60;
#查询所有学生的课程及分数情况
SELECT s.sname ,c.cname,sc.score FROM student s, sc,course c
WHERE s.s_id=sc.s_id AND sc.c_id=c.c_id;
#查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT s.sname 姓名,c.cname 课程名称,sc.score 分数 FROM student s,sc,course c
WHERE c.c_id=sc.c_id AND sc.s_id=s.s_id AND sc.score>70;
#查询不及格的学生信息
SELECT * FROM student s,sc WHERE s.s_id=sc.s_id AND sc.score<60;
#查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT * FROM student s,sc WHERE s.s_id=sc.s_id AND c_id=158 AND sc.score>80;
#求每门课程的学生人数
select s.c_id,cou.cname,count(s.s_id) 人数 from SC s,course cou
where s.c_id=cou.c_id
GROUP BY s.c_id,cou.cname;
#检索"01"课程分数小于60分的同学的信息,结果按分数降序排列
select s.s_id,s.sname,sc.score
from STUDENT s ,sc
where sc.s_id=s.s_id
and sc.c_id=155
and sc.score<60
order by sc.score desc;