数据库sql基础题

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

1、查询“001”课程比“002”课程成绩高的所有学生的学号;
 
select a.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----

  select a.id from student a, sc b, sc c where b.cid=1 and c.cid=2 and a.id=b.sid and a.id=c.sid and b.score>c.score

点评:需构造两个集合,一个001课程的集合, 一个002课程的集合

 


2、查询平均成绩大于60分的同学的学号和平均成绩;
   
select S#,avg(score)
   
from sc
   
group by S# having avg(score) >60;

 

 

3、查询所有同学的学号、姓名、选课数、总成绩;
 
select Student.S#,Student.Sname,count(SC.C#),sum(score)
 
from Student left Outer join SC on Student.S#=SC.S#
 
group by Student.S#,Sname
点评:因为要查出所有学生的信息, 所以要用右外连接,否则如果一个学生没有选课,则会丢失该学生的信息

 

4、查询学过“叶平”老师课的同学的学号、姓名; 
   
select Student.S#,Student.Sname
   
from Student 
   
where S# not in (select distinct( SC.S#) from SC,Course,Teacher where  SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平');
点评:先找出选修过“叶平”的课程的学生

 

5、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

    select a.id from student a, sc b, sc c
    where a.id = b.sid and a.id = c.sid and b.cid='001' and c.cid='002'
点评:做一个sc的自连接是关键

 

6、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select a.id, count(b.cid) from student a, sc b, course c, teacher d
where a.id = b.sid
and b.cid = c.id
and c.tid = d.id
and d.name='叶平'
group by a.id
having count(b.cid)=(select count(a.id) from course a, teacher b where a.tid=b.id and b.name='叶平')

点评:查出叶平的课程数,查出学生的选了叶平老师的课程数。
7、查询所有课程成绩小于60分的同学的学号;

select id from student a
where a.id not in
(select a.id from student a, sc b where a.id=b.sid and b.score>60)

点评:先找出有选课大于60分的学生

8、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
   
select distinct SC.S#,Sname
   
from Student,SC
   
where Student.S#=SC.S# and C# in (select C# from SC where S#='001');

 

 

9、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
   
SELECT SC.C# as 课程ID, Cname as 课程名称
        ,
SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]
        ,
SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]
        ,
SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]
        ,
SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]
   
FROM SC,Course
   
where SC.C#=Course.C#
   
GROUP BY SC.C#,Cname; 

 

10、查询各科成绩的及格率
   
select a.name, (sum(case when b.score>70 then 1 else 0 end))/(count(b.cid)) as '及格率'
    from course a, sc b where a.id = b.cid group by a.name

11、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
    select a.name, avg(b.score) from course a, sc b where a.id = b.cid group by a.name order by avg(b.score) asc, a.name asc

12、查询至少选修了两门课程的学生学号
    select a.id from student a, sc b where a.id = b.sid  group by a.id having count(b.cid)>=2

13、怎么优化sql语句以提高查询速度

    1 减少数据库访问次数;

    2 select语句避免使用select *, 因为数据库把*转换成各自段名称需要查询数据字典, 需要耗费更多时间;

    3 如无必要不要使用distinct;

    4 不要使用cursor;

   

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值