日期:2014-05-16  浏览次数:20556 次

一道经典的数据库sql语句题目

SQL查询题目

<!--列表视图页显示缩略图和文章摘要--><!--文章视图页显示全部内容--><!--如果有内容,显示内容-->

?

SQL查询题目

?

条件:

Student(Sno,Sname,Sage,Ssex) 学生表

Course(Cno,Cname,Tno) 课程表

SC(Sno,Cno,score) 成绩表(选课表)

Teacher(Tno,Tname) 教师表

?

问题:


1、查询“001”课程比“002”课程成绩高的所有学生的学号;

? select a.sno from (select sno,score from SC where cno='001') a,(select sno,score

? from SC where cno='002') b

? where a.score>b.score and a.sno=b.sno;

?

2、查询平均成绩大于60分的同学的学号和平均成绩;

?select SNO,avg(score) ?from sc ?group by SNO having avg(score) >60;


3、查询所有同学的学号、姓名、选课数、总成绩;

?select Student.SNO,Student.Sname,count(SC.CNO),sum(score)

?from Student left Outer join SC on Student.SNO=SC.SNO

?group by Student.SNO,Sname


4、查询姓“李”的老师的个数;

?select count(distinct(Tname))

? from Teacher where Tname like '李%';


5、查询没学过“叶平”老师课的同学的学号、姓名;

?select Student.SNO,Student.Sname ?from Student?

?where SNO not in (select distinct(SC.SNO) from SC,Course,Teacher where? SC.CNO=Course.CNO and Teacher.Tno=Course.Tno and Teacher.Tname='叶平');


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

select Student.SNO,Student.Sname from Student,SC where Student.SNO=SC.SNO and SC.CNO='001'and exists( Select * from SC as SC_2 where SC_2.SNO=SC.SNO and SC_2.CNO='002');


7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select SNO,Sname

? from Student

? where SNO in (select SNO from SC ,Course ,Teacher where SC.CNO=Course.CNO and Teacher.TNO=Course.TNO and Teacher.Tname='叶平' group by SNO having count(SC.CNO)=(select count(CNO) from Course,Teacher? where Teacher.TNO=Course.TNO and Tname='叶平'));


8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;?

Select SNO,Sname from (select Student.SNO,Student.Sname,score ,(select score from SC SC_2 where SC_2.SNO=Student.SNO and SC_2.CNO='002') score2

? from Student,SC where Student.SNO=SC.SNO and CNO='001') S_2 where score2 <score;


9、查询所有课程成绩小于60分的同学的学号、姓名;?

select SNO,Sname

? from Student

? where SNO not in (select Student.SNO from Student,SC where S.SNO=SC.SNO and score>60);


10、查询没有学全所有课的同学的学号、姓名;? ?

select Student.SNO,Student.Sname

??? from Student,SC

??? where Student.SNO=SC.SNO group by? Student.SNO,Student.Sname having count(CNO) <(select count(CNO) from Course);

?

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;??

select SNO,Sname from Student,SC where Student.SNO=SC.SNO and CNO in select CNO from SC where SNO='1001';


12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;?

select distinct SC.SNO,Sname

??? from Student,SC

??? where Student.SNO=SC.SNO and CNO in (select CNO from SC where SNO='001');


13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

update SC set score=(select avg(SC_2.score)

??? from SC SC_2

??? where SC_2.CNO=SC.CNO) from Course,Teacher where Course.CNO=SC.CNO and Course.TNO=Teacher.TNO and Teacher.Tname='叶平');

? ?
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

select SNO from SC where CNO in (select CNO from SC where SNO='1002')

??? group by SNO having count(*)=(select count(*) from SC where SNO='1002');

???
15、删除学习“叶平”老师课的SC表记录;?

DelectSC

??? from course ,Teacher?

??? where Course.CNO=SC.CNO and Course.TNO= Teacher.TNO and Tname='叶平';


16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩;? ?

Insert SC select SNO,'002',(Select avg(score)

??? from SC where CNO='002') from Student where SNO not in (Select SNO from SC where CNO='002');


17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分?

SELECT SNO as 学生ID

??????? ,(SELECT score FROM SC WHERE SC.SNO=t.SNO AND CNO='004') AS 数据库

??????? ,(SELECT score FROM SC WHERE SC.SNO=t.SNO AND CNO='001') AS 企业管理

??????? ,(SELECT sc