吹静静

欢迎QQ交流:592590682

Student(S#,Sname,Sage,Ssex) --学生表 
Course(C#,Cname,T#)         --课程表 
SC(S#,C#,score)             --成绩表 
Teacher(T#,Tname)            --教师表

-- 1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S#   -- 是表限定符,S.S#意思是取S表中的S#列的值
from (select S#,score from SC where C#='001')a,(select S#,score from SC where C#='002')b 
where a.score>b.score and a.S#=b.S#;

-- 2、查询平均成绩大于60分的同学的学号和平均成绩;
select S#,avg(score)
from SC
group by S# having avg(score)>60; -- HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集。

-- 3、查询所有同学的学号、姓名、选课数、总成绩;
select Student.S#,Student.Sname,count(SC.C#),sum(SC.score)
from Student left Outer join SC on Student.S#=SC.S# -- 左外连接(left join)
group by Student.S#,Sname;

-- 4、查询姓“李”的老师的个数;
select count(distinct(Tname))
from Teacher
where Tname like '李%';

-- 5、查询没学过“叶平”老师课的同学的学号、姓名;
select Student.S#,Stufent.Sname
from Student
where S# not in (
    select distinct(SC.S#) -- 关键词 DISTINCT 用于返回唯一不同的值
    from SC,Course,Teacher 
    where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'
);

-- 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 
select Student.S#,Student.Sname 
from Student,SC 
where Student.S#=SC.S# and SC.C#='001' and exists(
    select *
    from SC as SC_2 
    where SC_2.S#=SC.S# and SC_2.C#='002'
);

 

posted on 2019-05-30 08:07  吹静静  阅读(153)  评论(0编辑  收藏  举报