数据库sql基础题
2010-03-24 16:29
190 查看
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;
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;
相关文章推荐
- 【数据库基础】SQL中的基本数据类型
- SQL 必知必会-- 第1课:数据库基础和什么是SQL
- sql server 2008 之利用SQL Server Management Studio创建数据库(最基础)
- 数据库基础--SQL 语言(DQL、DDL、DML、DCL)
- 走向面试之数据库基础:三、SQL进阶之变量、事务、存储过程与触发器
- 数据库SQL 基础(总结)
- 今天说说数据库——SQL语言基础
- php 安全基础 第三章 数据库及SQL
- 数据库基础-sql_(一)
- 《Microsoft SQL Server 2005: 数据库基础由入门到精通》书评
- 【SQL重温】面试之数据库基础练习
- SQL_数据库基础试题(培训/已完结)
- ADO.NET基础01(ADO.NET组成,数据库的方式,SqlCommand,SqlDataReader)
- sql_设置权限_数据库基础
- 数据库基础(sql基本指令)
- Day30 Oracle基础1 数据库安装与入门 基本SQL语句
- 走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 1
- 走向面试之数据库基础:二、SQL进阶之case、子查询、分页、join与视图
- Oracle 数据库基础学习 (八) PL/SQL综合练习
- SQL 数据库基础操作