面试中经常遇到的SQL
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 12:51 点击:次
1、首先给出表结构,表结构需要大家去分析一下他们之间的逻辑关系。
Student(Sno,Sname,Sage,Ssex) 学生表
Course(Cno,Cname,Tno) 课程表
SC(Sno,Cno,score) 成绩表
Teacher(Tno,Tname) 教师表
2、问题
(1)查询“c001”课程比“c002”课程成绩高的所有学生的学号;
(2)查询平均成绩大于60分的同学的学号和平均成绩;
(3)查询所有同学的学号、姓名、选课数、总成绩;
(4)查询姓“李”的老师的个数;
(5)查询没学过“叶平”老师课的同学的学号、姓名;
(6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;
(8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
(9)查询所有课程成绩小于60分的同学的学号、姓名;
(10)查询没有学全所有课的同学的学号、姓名;
要求:
(1)请严格用Oracle对以上四张表进行建表。
(2)请严格按照Oracle SQL语法完成以上10道题
(1)查询“c001”课程比“c002”课程成绩高的所有学生的学号
答案:
select sno from sc where cno = 'c001' and sc.score >any (select score from sc where cno = 'c002');
(2)查询平均成绩大于60分的同学的学号和平均成绩;
答案:
select sno, avg(score) from sc group by sno having avg(score) > 60;
(3)查询所有同学的学号、姓名、选课数、总成绩;
答案:
select sc.sno,sname,sum(score),count(cno) from sc, student where sc.sno = student.sno group by sc.sno,sname;
(4)查询姓“李”的老师的个数;
答案:
select count(tno) from teacher where tname like '李%';
(5)查询没学过“叶平”老师课的同学的学号、姓名;
答案:
select sc.sno,sname from student, teacher, course, sc where teacher.tname <> '叶平' and
teacher.tno = course.tno and course.cno = sc.cno and sc.sno = student.sno;
(6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
答案:
select sc.sno,sname from sc,student where sc.cno = 'c001' and student.sno = sc.sno
and sc.sno in (select sno from sc where sc.cno = 'c002');
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;
答案:
select sc.sno,sname from student, teacher, course, sc where teacher.tname = '叶平' and
teacher.tno = course.tno and course.cno = sc.cno and sc.sno = student.sno;
(8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
答案:
select sno from sc where cno = 'c002' and sc.score 60;
(10)查询没有学全所有课的同学的学号、姓名;
答案:
select sc.sno,student.sname from sc,student where sc.sno = student.sno
group by sc.sno,student.sname having count(cno) < (select count(cno) from course);
摘自 easonwang的博客
Student(Sno,Sname,Sage,Ssex) 学生表
Course(Cno,Cname,Tno) 课程表
SC(Sno,Cno,score) 成绩表
Teacher(Tno,Tname) 教师表
2、问题
(1)查询“c001”课程比“c002”课程成绩高的所有学生的学号;
(2)查询平均成绩大于60分的同学的学号和平均成绩;
(3)查询所有同学的学号、姓名、选课数、总成绩;
(4)查询姓“李”的老师的个数;
(5)查询没学过“叶平”老师课的同学的学号、姓名;
(6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;
(8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
(9)查询所有课程成绩小于60分的同学的学号、姓名;
(10)查询没有学全所有课的同学的学号、姓名;
要求:
(1)请严格用Oracle对以上四张表进行建表。
(2)请严格按照Oracle SQL语法完成以上10道题
(1)查询“c001”课程比“c002”课程成绩高的所有学生的学号
答案:
select sno from sc where cno = 'c001' and sc.score >any (select score from sc where cno = 'c002');
(2)查询平均成绩大于60分的同学的学号和平均成绩;
答案:
select sno, avg(score) from sc group by sno having avg(score) > 60;
(3)查询所有同学的学号、姓名、选课数、总成绩;
答案:
select sc.sno,sname,sum(score),count(cno) from sc, student where sc.sno = student.sno group by sc.sno,sname;
(4)查询姓“李”的老师的个数;
答案:
select count(tno) from teacher where tname like '李%';
(5)查询没学过“叶平”老师课的同学的学号、姓名;
答案:
select sc.sno,sname from student, teacher, course, sc where teacher.tname <> '叶平' and
teacher.tno = course.tno and course.cno = sc.cno and sc.sno = student.sno;
(6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
答案:
select sc.sno,sname from sc,student where sc.cno = 'c001' and student.sno = sc.sno
and sc.sno in (select sno from sc where sc.cno = 'c002');
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;
答案:
select sc.sno,sname from student, teacher, course, sc where teacher.tname = '叶平' and
teacher.tno = course.tno and course.cno = sc.cno and sc.sno = student.sno;
(8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
答案:
select sno from sc where cno = 'c002' and sc.score 60;
(10)查询没有学全所有课的同学的学号、姓名;
答案:
select sc.sno,student.sname from sc,student where sc.sno = student.sno
group by sc.sno,student.sname having count(cno) < (select count(cno) from course);
摘自 easonwang的博客
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>