数据库要做
Posted 莫轩ASL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库要做相关的知识,希望对你有一定的参考价值。
drop table if exists Student; create table Student ( Sid varchar(5) primary key, Sname varchar(10), Sage int(3), Ssex varchar(10) ); drop table if exists Course; create table Course( Cid varchar(5) primary key, Cname varchar(10), Tid int(5) ); drop table if exists SC; create table SC( Sid varchar(5) primary key, Cid varchar(5), score double(4,1) ); drop table if exists Teacher; create table Teacher( Tid varchar(5) primary key, Tname varchar(10) ); 十几道sql语句面试题第1部分: Student(Sid, Sname, Sage, Ssex)学生表 Course(Cid, Cname, Tid)课程表 SC(Sid, Cid, score)成绩表 Teacher(Tid, Tname)教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.Sid from (select Sid, score from SC where Cid = ’001′) a, (select Sid, score from SC where Cid = ’002′) b where a.score > b.score and a.Sid = b.Sid; 2、查询平均成绩大于60分的同学的学号和平均成绩; select Sid, avg(score) from sc group by Sidhavingavg(score) > 60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.Sid, Student.Sname, count(SC.Cid), sum(score) from Student left Outer join SConStudent.Sid = SC.Sid group by Student.Sid, Sname4、查询姓“李”的老师的个数;select count(distinct(Tname)) from Teacher where Tnamelike‘李 % ’; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.Sid, Student.Sname from Student where Sid not in (select distinct(SC.Sid) from SC, Course, Teacher where SC.Cid = Course.Cid and Teacher.Tid = Course.Tid and Teacher.Tname = ’叶平’); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.Sid, Student.Sname from Student,SC where Student.Sid = SC.Sid and SC.Cid = ’001′ and exists (select * from SC as SC_2 where SC_2.Sid = SC.Sid and SC_2.Cid = ’002′); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select Sid,Sname from Student 8、查询所有课程成绩小于60分的同学的学号、姓名; select Sid,Sname from Student where Sid notin (select Student.Sid from Student, SC where S.Sid = SC.Sid and score > 60); 9、查询没有学全所有课的同学的学号、姓名; select Student.Sid,Student.Sname from Student,SC where Student.Sid = SC.Sid group by Student.Sid,Student.Sname having count(Cid) < (select count(Cid) from Course); 10、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名; select Sid, Sname from Student, SC where Student.Sid = SC.Sid and Cid in (select Cid from SC where Sid = ‘1001‘); 11、删除学习“叶平”老师课的SC表记录; delect SC from course, Teacher where Course.Cid = SC.Cid and Course.Tid = Teacher.Tid and Tname = ‘叶平‘; 12、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 select L.Cid课程ID, L.score最高分, R.score最低分 from SCL,SCR where L.Cid = R.Cid and L.score = (select MAX(IL.score) from SCIL, StudentIM where IL.Cid = L.Cid and IM.Sid = IL.Sid group by IL.Cid) and R.Score = (select MIN(IR.score) from SCIR where IR.Cid = R.Cid group by IR.Cid); 13、查询学生平均成绩及其名次 select 1 + (select COUNT(distinct平均成绩) from (select Sid, AVG(score)平均成绩 from SC group by Sid) T1 where 平均成绩 > T2.平均成绩)名次,Sid学生学号,平均成绩 from (select Sid, AVG(score)平均成绩 from SC group by Sid) T2 order by 平均成绩desc; 14、查询各科成绩前三名的记录: (不考虑成绩并列情况) select t1.Sid as 学生ID,t1.Cid as 课程ID,Score as 分数 from SCt1 where scoreIN (select TOP3score from SC where t1.Cid = Cid order by scoreDESC) order by t1.Cid; 15、查询每门功成绩最好的前两名 select t1.Sid as 学生ID,t1.Cid as 课程ID,Score as 分数 from SCt1 where scoreIN (select TOP2score from SC where t1.Cid = Cid order by scoreDESC) order by t1.Cid;
以上是关于数据库要做的主要内容,如果未能解决你的问题,请参考以下文章