数据库SQL语言--SELECT查询操作
1、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。
(1)--检索年龄大于23的男学生的学号和姓名--
    select sno,sn from s where sex='男' and age > 23
(2)--检索至少选修一门课程的女学生姓名--
  select sn from S,SC 
    where sex='女' AND  S.Sno=SC.Sno
    group by S.Sn having count(*)>=1;
(3)--检索王同学没有选修的课程的课程号--
      select cno from c
      where c.cno not in
      (select cno from sc,s
      where sc.sno=s.sno and sn like '王%')
(4)--检索至少选修两门课程的学生学号--
      select distinct s.sno from s,sc
      where sc.sno=s.sno
      group by s.sno
      having count(*)>=2;
(5)--检索全部学生都选修的课程的课程号与课程名--
      select cno,cn from c
      where not exists
      (select * from s
      where not exists
      (select * from sc
      where s.sno=sc.sno and c.cno=sc.cno))
(6)--检索选修了所有3学分课程的学生学号和姓名--
    select distinct s.sno,s.sn from s,sc
  where exists
(select * from c
where ct='3' and s.sno=sc.sno and c.cno=sc.cno)
2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。
  (1)--统计有学生选修的课程门数--
    select count(distinct sc.cno) from sc;
(2)--查询选修4号课程的学生的平均年龄--
    select avg(s.age)
    from s,sc
  where s.sno=sc.sno and cno='4';
(3)--查询学分为3的每门课程的学生平均成绩--
    select avg(sc.score)
    from c,sc,s
    where s.sno=sc.sno and c.ct='3';
(4)--统计每门课程的学生选修人数(超过3人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,则按课程号升序排列
select cno,count(sno)from sc
group by cno
having count(sno)>3
order by count(sno)desc,(cno)asc;
(5)--检索学号比钱横同学大,而年龄比他小的学生姓名--
selectselect中distinct sn from s
where sno>(select sno from s where sn='钱横')
and age<(select age from s where sn='钱横');
(6)--检索姓名以王打头的所有学生的姓名和年龄--
select sn,age from s
where s.sn like('%');
(7)--在SC中检索成绩为空值的学生学号和年龄--
select s.sno,s.age from s,sc
where score is null;
注意:is null 那里不能用等号“=
(8)--查询年龄大于女同学平均年龄的男同学姓名和年龄--
select s.sn,s.age from s
where age>(select avg(age)from s where sex='')
  and sex='';
(9)--查询年龄大于所有女同学年龄的男同学姓名和年龄--
select s.sn,s.age from s
where age>(select max(age)from s where sex='')
  and sex='';
(10)--检索所有比‘赵四’年龄大的学生姓名、年龄和性别--
select s.sn,s.age,s.sex from s
where age>(select max(age)from s where sn='赵四');
(11)--检索选修“”课程的学生中成绩最高的学生的学号--
select sno,score from sc
where score=(select max(score)from sc where cno='2');
(12)--检索学生姓名及其所选修课程的课程号和成绩--
select sn,cno,score from s,sc
where s.sno=sc.sno;
(13)--检索学生姓名及其所选修课程的课程号和成绩--
    select sno,sum(score) from sc
    where score>=60
    and sno in(select sno from sc
    group by sno
    having count(sno)>4)
    group by sno
    order by sum(score)desc;
  (14)--查询“c001”课程比“c002”课程成绩高的所有学生的学号--
    select * from SC a
    where ao='c001'
    and  exists(select * from sc b where bo='c002' and a.score>b.score
    and a.sno = b.sno)
  (15)--查询平均成绩大于60 分的同学的学号和平均成绩--
    select sno,avg(score)
    from sc 
    group by sno
    having avg(score)>60;
(16)--查询姓“刘”的老师的个数--
      select count(*)
      from teacher
      where tn like '刘%';
  (17)--查询没学过“谌燕”老师课的同学的学号、姓名--
      select * from s st where st.sno not in
      (select distinct sno from sc s join course c on so=co
      join teacher t =t.tno where tname='谌燕');
(18)--查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名--
    select st.* from sc a
    join sc b on a.sno=b.sno
    join s st
    on st.sno=a.sno
    where ao='c001' and bo='c002' and st.sno=a.sno;
(19)--查询学过“谌燕”老师所教的所有课的同学的学号、姓名--
    select st.* from s st join sc s on st.sno=s.sno
    join course c on so=co
    join teacher t =t.tno
    ame='谌燕'
(19)--查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名--
  select * from s st
  join sc a on st.sno=a.sno
  join sc b on st.sno=b.sno
  where ao='c002' and bo='c001' and a.score < b.score
(20)--查询所有课程成绩小于60 分的同学的学号、姓名--
    select st.*,s.score from s st
    join sc s on st.sno=s.sno
    join course c on so=co
    where s.score <60
(21)--查询没有学全所有课的同学的学号、姓名--
    select * from s where sno in
    (select sno from
          (select st.sno,co from s st
          cross join course c
          minus
          select sno,cno from sc))
(22)--查询每门课程被选修的学生数--
select cno,count(sno)
from sc
group by cno;
(23)--查询出只选修了一门课程的全部学生的学号和姓名--
    select sc.sno,st.sn,count(cno) from s st
    left join sc
    on sc.sno=st.sno
    group by st.sn,sc.sno having count(cno)=1;