- Student(Sno,Sname,Sage,Ssex) 学生表 S1:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别
- Course(Cno,Cname,T1) 课程表 C1,课程编号;Cname:课程名字;T1:教师编号
- SC(Sno,Cno,score) 成绩表 S1:学号;C1,课程编号;score:成绩
- Teacher(Tno,Tname) 教师表 T1:教师编号; Tname:教师名字
- --建表
- create table student(
- sno int primary key,
- sname varchar2(10) not null,
- sage int not null,
- sses char(1) ,
- constraint check_age check((sage)>0 and (sage)<120)
- );
-
- create table course(
- cno int primary key,
- cname varchar2(10) not null,
- tno int not null,
- constraint fk_course_teacher foreign key(tno) references teacher(tno)
- );
-
- create table teacher(
- tno int primary key,
- tname varchar2(10) not null
- );
-
- create table sc (
- sno int ,
- cno int ,
- score number,
- constraint fk_cno_sc foreign key (cno) references course(cno),
- constraint fk_cno_student foreign key (sno) references student(sno)
- );
- --修改表
- alter table student rename column sses to ssex;
- alter table student drop column ssex;
- alter table student add ( ssex varchar2(2)) ;
- --插数据
- insert into student values('001','zhang三',22,'男');
- insert into student values('002','wangwu2',22,'男');
- insert into student values('003','史蒂夫',23,'男');
- insert into student values('004','艾德',20,'女');
- insert into student values('005','猪八戒',32,'男');
- insert into student values(6,'合家福',12,'女');
- select * from student;
-
- insert into teacher values('1','徐大大');
- insert into teacher values('2','王富人');
- insert into teacher values('3','大大');
- insert into teacher values('5','李大大');
- insert into teacher values('6','陈大大');
- insert into teacher values('89','刘在石');
- insert into teacher values('9','何达大');
- insert into teacher values('10','彭建军');
- select * from teacher;
-
- insert into course values('1','Oracle数据','1');
- insert into course values('2','计算机原理','1');
- insert into course values('3','数据结构','3');
- insert into course values('4','算法概论','3');
- insert into course values('5','数据库原理','3');
- insert into course values('6','软件工程','6');
- insert into course values('7','UML','5');
- insert into course values('8','Java语言','89');
- insert into course values('9','嵌入式系统','10');
- select * from course order by cno desc;
- delete from sc s where rowid = (select rowid from sc where sno =3 and rownum =1 and cno =8);
- delete from sc s where rowid !=(select max(rowid) from sc b where s.cno=b.cno and b.cno =2)
- delete from sc s where cno =2;
- select rowid from sc where cno = 8;
-
- select cno,count(cno) from sc group by cno having count(cno) !=5 ;/2 4 3
-
- insert into sc(sno,cno,score) values(6,1,48);
- insert into sc(sno,cno,score) values(6,2,55);
- insert into sc(sno,cno,score) values(6,3,46);
- insert into sc(sno,cno,score) values(6,4,35);
- insert into sc(sno,cno,score) values(6,5,52);
- select * from sc where sno =6;
- select count(cno),cno from sc group by cno;
- select * from sc order by cno;
- select * from sc where cno =4 or cno =3 order by cno ,sno;
- select * from sc order by cno asc,sno asc,score desc;
-
- select s.sno,s.Sname,count(sc.sno),sum(sc.score) from student s
- left join sc on sc.sno = s.sno group by sc.sno ;
- --Question and Exercise
- 1、查询“001”课程比“002”课程成绩高的所有学生的学号;
- select a.sno from
- (select sno,score from sc where cno =1) a,
- (select sno,score from sc where cno =2) t
- where a.score >t.score and a.sno =t.sno;
- //sno=1 2 3 4 5
- select * from sc where sno =5
- 2、查询平均成绩大于60分的同学的学号和平均成绩;
- select sno,round(avg(score),1) from sc group by sno having avg(score)> 60;
- //round(数值,小数位数) Math.round()四舍五入取整原理:先+0.5 然后再向下取整(floor ),ceil
- //decimal()
- 3、查询所有同学的学号、姓名、选课数、总成绩;
- select s.sno,s.sname,count(c.rowid),sum(c.score) from student s ,sc c group by c.sno; 错误!!! 连接查询 和 分组
- select s.sno,s.sname,count(c.cno), sum(c.score) from student s left join sc c on s.sno = c.sno group by s.sno ,s.sname order by s.sno;
- select s.sno,s.sname,s.ssex,count(c.cno),sum(c.score) from student s,sc c where s.sno = c.sno(+) group by s.sno,s.sname,s.ssex;
- /*但是现在一旦分组之后,实际上对于语法上就会出现了新的限制,对于分组有以下要求:
- 1分组函数可以在没有分组的时候单独用使用,可是却不能出现其他的查询字段;
- 分组函数单独使用:
- SELECT COUNT(empno) FROM emp;
- 错误的使用,出现了其他字段:
- SELECT empno,COUNT(empno) FROM emp;
- 2如果现在要进行分组的话,则SELECT子句之后,只能出现分组的字段和统计函数,其他的字段不能出现:
- 正确做法:
- SELECT job,COUNT(empno),AVG(sal)
- FROM emp
- GROUP BY job;
- 错误的做法:
- SELECT deptno,job,COUNT(empno),AVG(sal)
- FROM emp
- GROUP BY job;
- */
- 4、查询姓“李”的老师的个数;
- select count(s.tno),s.tname from teacher s where s.tname like '李%' group by tname;
-
- 5、查询没学过“叶平”老师课的同学的学号、姓名;
- select * from teacher;
- insert into teacher values(66,'叶平');
- insert into course values(0,'C语言基础',66);
- insert into sc values(1,0,98)
- 嵌套
- select t.sno,t.sname from student t where not exists (
- select sno from sc where t.sno =sc.sno and cno in (
- select cno from course where tno in(
- select tno from teacher where tname='叶平'
- )));
- 内连接
- select t.sno,t.sname from student t where t.sno not in(
- select sno from sc s,course c,teacher b where
- s.cno = c.cno and
- c.tno = b.tno and
- b.tname = '叶平'
- );
-
- 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
- //select * from sc
- //select s.sno 学号,sname 姓名 from student s,sc c where s.sno=c.sno(+) and c.cno =1 and c.cno =2;
- //select s.sno ,s.sname from student s left join sc c on s.sno = c.sno where c.cno between 1 and 2;
- select distinct s.sno , s.sname from student s ,sc c where s.sno = c.sno and c.cno between 1 and 2 group by s.sno,s.sname having count(s.sno)=2;
- //select s.sno , s.sname from student where s.sno in ()
-
- 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
- select s.sno,s.sname from student s,sc c,course co ,teacher t
- where
- s.sno =c.sno and
- c.cno =co.cno and
- co.tno = t.tno and
- t.tname ='叶平';
-
- 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
- select sno,sname from student s where s.sno in(
- select a.sno from (select score,sno from sc where cno=1) a
- left join (select score,sno from sc where cno=2) b
- on a.sno = b.sno where a.score>b.score
- );
- update sc set score =99 where cno =2 and sno=1;
- select * from sc where cno between 1 and 2 ;
- 9、查询所有课程成绩小于60分的同学的学号、姓名;--没有一门成绩>60
- 采用 any some all 类的比较量词
- select distinct s.sno,sname from student s left join sc b on s.sno= b.sno where 60> all(select score from sc z where z.sno=b.sno);
-
- select s.sno,s.sname from student s where s.sno not in
- (select sno from sc b where b.score >60 and s.sno=b.sno);
-
- 10、查询没有学全所有课的同学的学号、姓名;
- select * from sc order by sno,cno,score;
- select s.sno,s.sname from student s where s.sno in (
- select sno from (select sno,count(rowid) from sc group by sno
- having count(rowid)<10 order by sc.sno asc)
- );
- select s.sno,s.sname from student s,sc b where
- s.sno = b.sno group by s.sno,s.sname having count(cno) < (select count(cno) from course);
-
- 11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
- select s.sno ,s.sname from student s where s.sno in
- (
- select sno from sc b where exists (select cno from sc c where sno =6 and b.cno =cno)
- )
- select distinct s.sno ,s.sname from student s,sc b where
- s.sno =b.sno and b.cno in (select cno from sc t where t.sno=6);
- 测试 一个没有跟sno=1相同学好的同学
- select * from sc where sno =6
- insert into student values(7,'刘备',23,'男')
- insert into sc values(7,9,'89')
-
- 12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
- select distinct s.sno,s.sname from student s,sc b where s.sno = b.sno and b.cno in (select cno from sc where sno = 6);
-
- 13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
- --oracle产生随机数
- select round(dbms_random.value(0,100),1) from dual;
- dbms_random.string (opt char, len NUMBER);
- --opt:表示返回值可能包含的东西
- ----'u','U' : 任何大写字母
- ----'l','L' : 任何小写字母
- ----'a','A' : 大小写混合字母
- ----'x','X' : 大写字母和数字混合
- ----'p','P' : 任何可打印字符
- ----其他参数 : 任何大写字母
- --len:表示要返回的字符串长度
- select dbms_random.string('x', 19) from dual
-
-
- begin
- for cr in (select rowid from sc )
- loop --循环
- update sc b set score =round(dbms_random.value(0,100),1) --更新语句(根据查询出来的结果集合)
- where rowid = b.rowid;
- end loop; --结束循环
- end;
- commit;
- /*sql循环更新模板
- BEGIN
- FOR L_RECORD IN (select id from customer_config where salt is null)
- LOOP
- UPDATE customer_config
- SET passwd=dbms_random.string('x',8)
- WHERE id=L_RECORD.id;
- END LOOP;
- commit;
- END;*/
- update sc so set score = (
- select distinct round(avg(score),1) from sc b where b.cno = (
- select distinct s.cno from sc s,course c,teacher t where s.cno = c.cno and c.tno = t.tno and t.tname = '叶平'
- )) where so.cno =
- ( select distinct s.cno from sc s,course c,teacher t where s.cno = c.cno and c.tno = t.tno and t.tname = '叶平')
-
- update sc s1 set score=(
- select avg(sc2.score) from sc s2 where s1.sno = s2.sno
- ) from sc sc2,course c,teacher t where sc2.cno =c.cno and t.tno = c.tno and t.tname ='叶平';
-
- insert into sc values(4,0,round(dbms_random.value(0,100),1));
- select * from sc b where b.cno = 0;
- 14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
- select * from sc order by sno,cno,score;
- select distinct s.sno,s.sname from student s,sc b where s.sno = b.sno and
- b.cno in(select cno from sc where sno = 1)
-
-
- 15、删除学习“叶平”老师课的SC表记录;
- select * from sc b,course c,teacher t where b.cno = c.cno and c.tno = t.tno and t.tname ='叶平';
- select * from sc where cno = 2;
- delete from sc b,course c,teacher t where b.cno = c.cno and c.tno =t.tno and t.tname = '叶平';
- select * from course c,teacher t where c.tno = t.tno and t.tname = '叶平';
-
- delete from sc b where b.sno in (select bb.sno from sc bb,course c,teacher t
- where b.cno = bb.cno and bb.cno= c.cno and c.tno = t.tno and t.tname ='叶平')
-
- 16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2、
- 号课的平均成绩;
- begin
- for sn in(select distinct sno from sc b where b.sno not in (select sno from sc bb where bb.cno=3) )
- loop
- insert into sc values(sn.sno,2,(select round(avg(a.score),1) from sc a group by a.cno having a.cno = 2));
- end loop;
- end;
-
- insert into sc select s.sno,2,(select round(avg(score),1) from sc b where b.cno=2)
- from student s where s.sno not in (select sno from sc where cno =3)
-
- select * from sc where sno =7;
- 17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、
- “英语”三门的课程成绩,按如下形式显示: 学生ID,数据库,企业管理,英语,有效课程数,有效平均分
- select sno,b.cno,(select cname from course c where cno =b.cno),count(*),avg(score)
- from sc b
- group by sno,cno
-
- select s1.sno 学生ID,
- (select b.score from sc b where b.cno =1 and b.sno =s1.sno) as 数据库,
- (select b.score from sc b where b.cno =8 and b.sno = s1.sno) Java,
- (select b.score from sc b where b.cno =3 and b.sno = s1.sno) 数据结构,
- count(*) 有效课程数,
- round(avg(score),1)有效平均分
- from sc s1
- group by s1.sno
- order by round(avg(score),1)
-
- 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
- select b.cno 课程ID,
- (select cname from course where b.cno = cno) 课程,
- max(score) 最高分,
- min(score) 最低分,
- round(avg(score),1) 平均分
- from sc b group by b.cno
-
-
- 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
- select b.cno 课程编号,
- (select cname from course where cno =b.cno) 课程名,
- round(avg(b.score),1) 平均分,
- concat(round(sum(case when b.score>60 then 1 else 0 end)/count(*)*100,1),'%') 及格率
- from sc b group by b.cno order by avg(b.score);
- --验证 cno 的及格率
- select sum(score) ,round(
- sum(
- case
- when b.score>=60
- then 1
- else 0
- end
- )/count(*),2) 及格率,count(*) from sc b where cno =2;
- select * from sc where cno = 2 3个及格的,共5个,及格率60%
-
- 20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)
- select b.cno 课程编号,
- (select cname from course where cno =b.cno) "课程名",
- round(avg(b.score),1) 平均分,
- concat(round(sum(case when b.score>60 then 1 else 0 end)/count(*)*100,1),'%') 及格率
- from sc b group by b.cno having b.cno in(1,2,3,4) order by avg(b.score);
-
- 21、查询不同老师所教不同课程平均分从高到低显示
- select t.tname, round(avg(s.score),1) from sc s,course c,teacher t
- where s.cno = c.cno and c.tno =t.tno
- group by t.tname;
-
-
- 22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)
- [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩
- select * from(
- select rownum rn, s1.sno,s1.sname,c.cname,s.score from student s1,sc s,course c
- where rownum<=6 and c.cno =s.cno and s.cno=1 and s1.sno =s.sno
- group by rownum ,s1.sno,s1.sname,c.cname,s.score
- order by s.score desc ) t where t.rownum<=6 and t.rownum>=3
-
- select s.sname,round(avg(b.score),1) ,
- (select score from sc t where cno =1 and c.cno=t.sno ) 计算机原理,
- (select score from sc t where cno =3 and c.cno=t.sno ) 数据结构,
- (select score from sc t where cno =5 and c.cno=t.sno ) 数据库原理,
- (select score from sc t where cno =6 and c.cno=t.sno ) Java语言
- from student s,sc b,course c
- where s.sno = b.sno and b.cno = b.cno
- group by s.sname
-
- select * from course;
-
- 23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
- select c.cno "课程编码",
- c.cname "课程名称",
- sum(case when s.score between 85 and 100 then 1 else 0 end) "100-85",--别名如果是数字开头的话,就要双引号
- sum(case when s.score between 70 and 85 then 1 else 0 end) "85-70",
- sum(case when s.score between 60 and 70 then 1 else 0 end) "70-60",
- sum(case when s.score <60 then 1 else 0 end) "<60 "
- from sc s,course c
- where s.cno = c.cno
- group by c.cno,c.cname;
-
- select * from sc where cno=6 and score between 85 and 100;--between 较小数 and 较大数
- update sc set score = 85 where sno =5 and cno =6;
-
- 24、查询学生平均成绩及其名次
-
- select
- s.sname 姓名,
- round(avg(t.score),1) 平均成绩
- from student s,sc t
- where s.sno = t.sno
- group by s.sname
- order by round(avg(t.score),1) desc
-
- 25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
- select s.sno,s.sname ,
- ( select top 3 score from sc where sno = s.sno )
- from student s,sc t
- where t.cno = c.cno
-
- --使用 row_number() over(partition by order by ),rank(), derank();函数
- select * from (select sno,
- cno,
- score,
- row_number() over(partition by cno order by score desc) rn
- from sc s
- )
- where rn <= 3
-
- 26、查询每门课程被选修的学生数
- select cno,count(cno) from sc group by cno
-
- select * from sc where cno = 1;
- 27、查询出只选修了一门课程的全部学生的学号和姓名
-
-
- 28、查询男生、女生人数
- select (select count(*) from student where ssex='女') 男生数, (select count(*) from student where ssex='男') 女生数 from dual;
- select * from student;
- 29、查询姓“张”的学生名单
- select * from student t where t.sname like '张%';
-
- 30、查询同名同性学生名单,并统计同名人数
- select s.*,count(*) from student s (select * from student) a where s.sname =a.sanme
- select sname,count(*) from student s group by s.sname having count(*)>1;
- 31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
- select * from student where sage between to_date('1981-01-01','yyyy-mm-dd') and to_date('1981-12-31','yyyy-mm-dd');
- select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age
- from student
- where CONVERT(char(11),DATEPART(year,Sage))='1981';
- 32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
- select s.cno ,t.cname ,round( avg(s.score),1)
- from sc s,course t
- where s.cno = t.cno
- group by s.cno,t.cname
- order by round( avg(s.score),1) asc,cno desc
-
- select avg(score) from sc where cno =7
- 33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
-
- select s.sno 学号,s.sname 姓名,round(avg(t.score),1) 平均成绩
- from student s,sc t
- where s.sno = t.sno
- group by s.sno,s.sname
- having round(avg(t.score),1)>60;
-
- 34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
- select s.sname,t.score
- from student s,sc t,course c
- where
- s.sno =t.sno
- and t.cno = c.cno
- and c.cname like '%数据库%'
- and t.score <60;
-
- select * from course;
- 35、查询所有学生的选课情况;
- select s.sno,s.sname,t.cname
- from student s,course t,sc c
- where s.sno = c.sno and c.cno = t.cno
- order by sno
-
- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
- select s.sname,c.cname,t.score
- from student s,sc t,course c
- where s.sno =t.sno
- and t.cno = c.cno
- and exists (select * from sc a where t.score >70 and a.sno= t.sno)
-
- 37、查询不及格的课程,并按课程号从大到小排列
- select distinct t.cno, c.cname
- from sc t,course c
- where t.cno = c.cno
- and t.score <60
- order by t.cno;
- 38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
- select s.sno,s.sname from student s,sc t
- where s.sno = t.sno
- and t.cno = 3
- group by s.sno,s.sname
- having min(t.score)>80;
-
- select * from sc where cno =3
- and not exists ( select * from sc a where a.score <80 and a.sno = s.sno)
- select * from sc t where not exists( select * from sc t where t.score <80)
-
- 39、求选了课程的学生人数
- select count(*) from sc where
- 40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
- select s.sname,a.score
- from sc a,course c,student s,teacher t
- where a.sno = s.sno
- and a.cno = c.cno
- and t.tno = c.tno
- and t.tname like '叶平'
- and a.score =(select max(score) from sc where cno = a.cno)
- group by s.sname;
-
-
- select * from teacher
- select * from teacher where tname='叶平'
- select * from sc where cno =1
- select * from course where tno =1;
-
- select * from sc a,teacher t,student s
- where a.sno = s.sno
- and t.no = a.cno
- and t.tname ='叶平'
- 41、查询各个课程及相应的选修人数
- select c.cname,t.cno,count(*) from sc t,course c
- where c.cno = t.cno
- group by c.cname,t.cno
- 42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
- select s.sno,a.cno, a.score,b.score from sc a,sc b,student s
- where a.score= b.score
- and a.cno <>b.cno and a.sno = s.sno
-
- 43、查询每门功成绩最好的前两名
- --!!
- --如果不考虑分数重复,用RANK()
- select m.学号 , m.课程号 , m.成绩 from
- (
- select t.* , RANK() over(partition by 课程号 order by 成绩 desc) px from aa t
- ) m
- where px <= 2
- order by m.课程号 , m.成绩 desc
-
- select * from
- (
- select t.*,rank() over(partition by t.cno order by score desc) px from sc t
- ) m
- where px<=2
- order by m.cno,m.score desc
-
- --如果考虑分数重复,用DENSE_RANK()
- select m.学号 , m.课程号 , m.成绩 from
- (
- select t.* , DENSE_RANK() over(partition by 课程号 order by 成绩 desc) px from aa t
- ) m
- where px <= 2
- order by m.课程号 , m.成绩 desc
-
- select m.* from
- (
- select t.*,RANK() over(partition by t.cno order by t.score desc) px from sc t
- )m
- where px <=2
- order by
- 44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
- select t.cno,count(t.sno)
- from sc t
- /*where count(t.sno)>10*/
- group by t.cno
- having count(t.sno)>10
- order by count(t.sno) desc,t.cno asc
-
- select t.cno,count(cno) from sc t group by t.cno order by cno
- select * from sc where cno = 5
- 45、检索至少选修两门课程的学生学号
- select sno
- from sc
- group by sno
- having count(cno)>2
- select * from sc where sno = 7
- 46、查询全部学生都选修的课程的课程号和课程名
- select *
- from course c
- where c.cno in (select cno from sc group by cno)
-
- 47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
- select Sname from Student where Sno not in (select Sno from Course,Teacher,SC where Course.Tno=Teacher.Tno and SC.Cno=course.Cno and Tname='叶平');
-
- 48、查询两门以上不及格课程的同学的学号及其平均成绩
- select sno,avg(nvl(score,0)) from SC where sno in (select sno from SC where score <60 group by sno having count(*)>2)group by sno;
-
- 49、检索“004”课程分数小于60,按分数降序排列的同学学号
- select sno from SC where cno='004'and score <60 order by score desc;
- 50、删除“002”同学的“001”课程的成绩
-
- delete from Sc where S#='002'and C#='001';
- --sysdba 登录
- grant select on v$statname to scott;
- grant select on v$sesstat to scott;
- grant select on v$mystat to scott;
- grant select on v$mystat to scott;
数据库面试必知必会
[https://segmentfault.com/a/1190000013517914?utm_source=channel-hottest]https://segmentfault.com/a/1190000013517914?utm_source=channel-hottest