标签:
一、表说明同“学生——成绩表2.1”
二、目录
选课情况
1. 查询学过"张三"老师授课的同学的信息
2. 查询没学过"张三"老师授课的同学的信息
3. 查询选修了全部课程的学生信息
4. 查询没有学全所有课程的同学的信息
5. 查询出只选有两门课程的全部学生的学号和姓名
6. 检索至少选修两门课程的学生学号
7. 查询每门课程被选修的学生数
8. 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
9. 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
10. 查询学过编号为"01"并且也学过编号为"02"的但是没有学过课程"03"的同学的信息
11. 查询至少有一门课与学号为"01"的同学所学相同的同学的信息
12. 查询和"01"号的同学学习的课程完全相同的其他同学的信息
三、查询
1.查询学过"张三"老师授课的同学的信息
select distinct Student.* from Student , SCore , Course , Teacher where Student.s_id= SCore.s_id and SCore.C_id = Course.C_id and Course.T_id = Teacher.T_id and Teacher.Tname = ‘张三‘ order by Student.S_id
2.查询没学过"张三"老师授课的同学的信息
select student.* from student where student.S_id not in (select distinct score.S_id from score , course , teacher where score.C_id = course.C_id and course.T_id = teacher.T_id and teacher.tname = N‘张三‘) order by student.S_id
3.查询选修了全部课程的学生信息
方法1
select student.* from student where S_id in (select S_id from score group by S_id having count(1) = (select count(1) from course))
方法2 使用双重否定来完成
select t.* from student t where t.S_id not in( select distinct m.S_id from ( select S_id , C_id from student , course) m where not exists (select 1 from score n where n.S_id = m.S_id and n.C_id = m.C_id) )
方法3 使用双重否定来完成
select t.* from student t where not exists(select 1 from( select distinct m.S_id from( select S_id , C_id from student , course) m where not exists (select 1 from score n where n.S_id = m.S_id and n.C_id = m.C_id) ) k where k.S_id = t.S_id)
4. 查询没有学全所有课程的同学的信息
4.1不包括什么课都没选的同学
select Student.* from Student , SCore where Student.S_id = SCore.S_id group by Student.S_id , Student.Sname , Student.Sage , Student.Ssex having count(C_id) < (select count(C_id) from Course)
4.2包括什么课都没选的同学
select Student.* from Student left join SCore on Student.S_id = SCore.S_id group by Student.S_id , Student.Sname , Student.Sage , Student.Ssex having count(C_id) < (select count(C_id) from Course)
5.查询出只选有两门课程的全部学生的学号和姓名
select Student.S_id, Student.Sname from Student , SCore where Student.S_id = SCore.S_id group by Student.S_id , Student.Sname having count(SCore.S_id) = 2 order by Student.S_id
6. 检索至少选修两门课程的学生学号
select student.S_id, student.Sname from student , SCore where student.S_id = SCore.S_id group by student.S_id , student.Sname having count(1) >= 2 order by student.S_id
7. 查询每门课程被选修的学生数
7.1 只在score表中查询
select c_id , count(S_id) 学生数 from score group by c_id
7.2在score表、Course表中查询
select Course.C_id , Course.Cname , count(*) 学生人数 from Course , SCore where Course.C_id = SCore.C_id group by Course.C_id , Course.Cname order by Course.C_id , Course.Cname
7.3 统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select Course.C_id, Course.Cname , count(*) 学生人数 from Course , SCore where Course.C_id = SCore.C_id group by Course.C_id , Course.Cname having count(*) >= 5 order by 学生人数 desc , Course.C_id
8.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
方法1
select Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id= ‘01‘ and exists ( Select 1 from SCore SC_2 where SC_2.S_id = SCore.S_id and SC_2.C_id= ‘02‘) order by Student.S_id
方法2
select Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id = ‘02‘ and exists (Select 1 from SCore SC_2 where SC_2.S_id = SCore.S_id and SC_2.C_id = ‘01‘) order by Student.S_id
方法3
select m.* from Student m where S_id in( select S_id from( select distinct S_id from SCore where C_id = ‘01‘ union all select distinct S_id from SCore where C_id = ‘02‘ ) t group by S_id having count(1) = 2 ) order by m.S_id
9.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
方法1
select Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id = ‘01‘ and not exists ( Select 1 from SCore SC_2 where SC_2.S_id = SCore.S_id and SC_2.C_id = ‘02‘) order by Student.S_id
方法2
select Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id = ‘01‘ and Student.S_id not in ( Select SC_2.S_id from SCore SC_2 where SC_2.S_id = SCore.S_id and SC_2.C_id = ‘02‘) order by Student.S_id
10.查询学过编号为"01"并且也学过编号为"02"的但是没有学过课程"03"的同学的信息
select Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id= ‘01‘ and exists ( Select 1 from SCore SC_2 where SC_2.S_id = SCore.S_id and SC_2.C_id= ‘02‘) and not exists( Select 1 from SCore SC_3 where SC_3.S_id = SCore.S_id and SC_3.C_id= ‘03‘) order by Student.S_id
11.查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct Student.* from Student , SCore where Student.S_id = SCore.S_id and SCore.C_id in (select C_id from SCore where S_id = ‘01‘) and Student.S_id <> ‘01‘
12.查询和"01"号的同学学习的课程完全相同的其他同学的信息
select Student.* from Student where S_id in (select distinct SCore.S_id from SCore where S_id <> ‘01‘ and SCore.C_id in (select distinct C_id from SCore where S_id = ‘01‘) group by SCore.S_id having count(1) = (select count(1) from SCore where S_id=‘01‘))
标签:
原文地址:http://www.cnblogs.com/wql025/p/4957522.html