下面是四个表,只是为了练习用,不考虑表的结构,
Student(S_ID,S_S#,S_Name,S_Age,S_Sex)学生表
Course(C_ID,C_C#,C_T#,C_Name)课程表
SC(SC_ID,SC_S#,SC_C#,SC_Score)成绩表
Teacher(T_ID,T_T#,T_Name)教师表
--1、查询‘01’课程比‘02’课程成绩高的所有学生的学号;
--2、查询平均成绩大于80分的同学的学号和平均成绩
--3、查询所有同学的学号、姓名、选课数、总成绩
select Student .S_S# ,Student .S_Name ,COUNT (SC.SC_C# )AS SCamount,
SUM (SC.SC_Score )AS Score from Student left outer join
SC on Student .S_S# =SC.SC_S# group by Student .S_S# ,Student .S_Name
--4、查询姓农的老师的个数
--5、查询没有学过"冯瑞涛"老师课的同学的学号、姓名;
select Student .S_S# ,Student .S_Name from Student where Student .S_S# in (select Student .S_S# from SC,Course ,Teacher where
SC.SC_C# =Course .C_C# and Teacher .T_T# =Course .C_T# and Teacher .T_Name ='农凤新'group by SC.SC_S# having COUNT (SC.SC_C# )
=(select COUNT (Course.C_C#) from Course ,Teacher where
Teacher .T_T# =Course .C_T# and T_Name ='农凤新'));
--6、查询学过‘01’并且学过编号‘02’课程的同学的学号和姓名
select Student .S_S# ,Student .S_Name from Student ,SC
where SC.SC_S# =Student .S_S# and SC.SC_C# ='01' and
exists(select *from SC as SC_2 where SC_2.SC_S# =SC.SC_S# and SC_2.SC_C# ='02');
--7、查询学过‘农凤新’老师所教的所有课的学生的学号和姓名;
select Student .S_S# ,Student .S_Name from Student where Student .S_S# in (select Student .S_S# from SC,Course ,Teacher where
SC.SC_C# =Course .C_C# and Teacher .T_T# =Course .C_T# and Teacher .T_Name ='农凤新'group by SC.SC_S# having COUNT (SC.SC_C# )
=(select COUNT (Course.C_C#) from Course ,Teacher where
Teacher .T_T# =Course .C_T# and T_Name ='农凤新'));
--8、查询所有课程成绩小于60分的同学的学号和姓名
--9、查询没有学全所有课的同学的学号和姓名.