SQL笔试题:经典50题 创建表
create table student(sno varchar2(4) primary key,sname varchar2(4),birthday date,sex varchar2(2)); create table scores(sno varchar2(4),cno varchar2(4),result float,primary key(sno,cno)); create table course(cno varchar2(4),cname varchar2(4),tno varchar2(4),primary key(cno,cname); create table teacher(tno varchar2(4) primary key,tname varchar2(10));添加数据
insert into student values('0001' , '猴子' , to_date('1989-01-01','yyyy-mm-dd'), '男'); insert into student values('0002' , '猴子' , to_date('1990-12-21','yyyy-mm-dd'), '女'); insert into student values('0003' , '马云' , to_date('1991-12-21','yyyy-mm-dd'), '男'); insert into student values('0004' , '马云' , to_date('1990-05-20','yyyy-mm-dd'), '男'); insert into scores values('0001' , '0001' , 80); insert into scores values('0001' , '0002' , 90); insert into scores values('0001' , '0003' , 99); insert into scores values('0002' , '0002' , 60); insert into scores values('0002' , '0003' , 80); insert into scores values('0003' , '0001' , 80); insert into scores values('0003' , '0002' , 80); insert into scores values('0003' , '0003' , 80); insert into course values('0001' , '语文' , '0002'); insert into course values('0002' , '数学' , '0001'); insert into course values('0003' , '英语' , '0003'); insert into teacher values('0001' , '孟扎扎'); insert into teacher values('0002' , '马化腾'); insert into teacher values('0003' , 'null'); insert into teacher values('0004' , ' ');为了方便学习,我将50道面试题进行了分类 一.简单查询 1.查询姓“猴”的学生名单
select * from student where sname like '猴%';2.查询姓名中最后一个字是“猴”的学生名单
select * from student where sname like '%猴';3.查询姓名中带“猴”的学生名单
select * from student where sname like '%猴%';4.查询姓“孟”老师的个数
select count(tname) from teacher where tname like '孟%'二.汇总分析 5.查询课程编号为“0002”的总成绩
select sum(result) from scores where cno = '0002';6.查询选了课程的学生人数
select count(distinct sno) from scores;7.查询各科成绩最高和最低的分, 以如下的形式显示:课程号,最高分,最低分
select cno,max(result)"最高分",min(result)"最低分" from scores group by cno;8.查询每门课程被选修的学生数
select cno,count(sno) from scores group by cno;9.查询男生、女生人数
select sex,count(sno) from student group by sex;
10.查询平均成绩大于60分学生的学号和平均成绩
select sno,avg(result) from scores group by sno having avg(result) > '60';11.查询至少选修两门课程的学生学号
select sno from scores group by sno having count(cno) >=2;12.查询同名同姓学生名单并统计同名人数
select sname,count(sname) from student group by sname having count(sname)>1;13.查询不及格的课程并按课程号从大到小排列
select cno from scores where result <60 order by result desc;14.查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
select cno,avg(result) from scores group by cno order by avg(result) asc,cno desc;15.检索课程编号为“0004”且分数小于60的学生学号,结果按按分数降序排列
select sno from scores where cno = '0004' and result <60 order by result desc;16.统计每门课程的学生选修人数(超过2人的课程才统计) 要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序
select cno ,count(sno) from scores group by cno having count(sno)>2 order by count(sno) desc,cno asc;17.查询两门以上不及格课程的同学的学号及其平均成绩
select sno,avg(result) from scores where result <60 group by sno having count(sno)>2;18.查询学生的总成绩并进行排名
select sno,sum(result) from scores group by sno order by sum(result) desc;19.查询平均成绩大于60分的学生的学号和平均成绩
select sno,avg(result) from scores group by sno having avg(result)>60;三.复杂查询 汇总分析
20.查询所有课程成绩小于60分学生的学号、姓名
select sno,sname from student where sno in (select sno from scores group by sno having max(result)<60);21.查询没有学全所有课的学生的学号、姓名
select sno, sname from student where sno in (select sno from scores group by sno having count(cno) < (select count(cno) from course));22.查询出只选修了两门课程的全部学生的学号和姓名
select sno, sname from student where sno in (select sno from scores group by sno having count(cno) = 2);四.日期函数 23.1990年出生的学生名单
select * from student where to_char(birthday,'yyyy')=199024.查询各科成绩前两名的记录
select * from (select sno, cno, result, row_number() over(partition by cno order by result) t from scores) where t in (1, 2);25.查询各学生的年龄(精确到月份) 方法一
select sno,sname,(to_char(sysdate, 'yyyy') - to_char(birthday, 'yyyy')) || '岁' || (to_char(sysdate, 'mm') - to_char(birthday, 'mm')) || '个月' age from student方法二
select sno,sname,round(((months_between(sysdate,birthday))/12),1) age from student26查询本月过生日的学生
select sno,sname from student where to_char(sysdate,'mm')=to_char(birthday,'mm')五.多表查询 27.查询所有学生的学号、姓名、选课数、总成绩
select a.sno, a.sname, "选课数", "总成绩" from student a left join (select sno, count(cno) "选课数", sum(result) "总成绩" from scores group by sno) b on a.sno = b.sno;28.查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select a.sno,a.sname,"平均成绩" from student a inner join (select sno, avg(result) "平均成绩" from scores group by sno having avg(result) >85 ) b on a.sno=b.sno29.查询学生的选课情况:学号,姓名,课程号,课程名称
select a.sno,a.sname,c.cno,c.cname from student a inner join scores b on a.sno= b.sno inner join course c on b.cno=c.cno order by a.sno30.查询出每门课程的及格人数和不及格人数 方法一
select a.cno,"及格人数","不及格人数" from (select cno,count(sno)"及格人数" from scores where result >=60 group by cno) a full join (select cno,count(sno)"不及格人数" from scores where result <60 group by cno) b on a.cno=b.cno方法2
select cno, sum(case when result >=60 then 1 else 0 end) "及格人数", sum(case when result<60 then 1 else 0 end ) "不及格人数" from scores group by cno;31.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计:各分数段人数,课程号和课程名称
select a.cno,b.cname, sum(case when result between 85 and 100 then 1 else 0 end) "100-85人数", sum(case when result between 70 and 84 then 1 else 0 end) "70-84人数", sum(case when result between 60 and 69 then 1 else 0 end) "60-69人数", sum(case when result <60 then 1 else 0 end) "小于60人数" from scores a right join course b on a.cno = b.cno group by a.cno,b.cname32.查询课程编号为0003且课程成绩在80分以上的学生的学号和姓名
select sno,sname from student where sno in (select sno from scores where cno=0003 and result>80);33.下面是学生的成绩表(表名score,列名:学号、课程号、成绩)
select sno, max(case cno when '0001' then result else 0 end) as "课程号0001", max(case cno when '0002' then result else 0 end) as "课程号0002", max(case cno when '0003' then result else 0 end) as "课程号0003" from scores group by sno;34.检索"0001"课程分数小于60,按分数降序排列的学生信息
select a.*,b.cno,b.result from student a inner join (select * from scores where cno=0001 and result <60) b on a.sno = b.sno order by result35.查询不同老师所教不同课程平均分从高到低显示
select c.tno, c.tname, "平均分" from course a inner join (select cno, avg(result) "平均分" from scores group by cno order by avg(result) desc) b on a.cno = b.cno inner join teacher c on c.tno = a.tno36.查询课程名称为"数学",且分数低于60的学生姓名和分数
select a.sname, b.result from student a inner join (select sno, result from scores where cno = (select cno from course where cname = '数学') and result < 60) b on a.sno = b.sno37.查询任何一门课程成绩在70分以上的姓名、课程名称和分数(与上题类似)
select a.sname,c.cname,b.result from student a inner join scores b on a.sno=b.sno inner join course c on b.cno=c.cno where b.result >7038.查询两门及其以上及格课程的同学的学号,姓名及其平均成绩
select a.sno, a.sname, "平均成绩" from student a inner join (select sno, count(cno) "及格课程数", avg(result) "平均成绩" from scores where result > 60 group by sno having count(cno) >= 2) b on a.sno = b.sno39.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select a.* from scores a inner join scores b on a.sno=b.sno where a.cno!=b.cno and a.result = b.result40.-查询课程编号为“0001”的课程比“0002”的课程成绩低的所有学生的学号
select a.sno from (select * from scores where cno=0001 ) a inner join (select * from scores where cno=0002)b on a.sno=b.sno where a.result <b.result41.查询学过编号为“0001”的课程并且也学过编号为“0002”的课程的学生的学号、姓名
select c.sno,c.sname from (select * from scores where cno=0001 ) a inner join (select * from scores where cno=0002)b on a.sno=b.sno inner join student c on c.sno=b.sno42.查询学过“孟扎扎”老师所教的所有课的同学的学号、姓名
select d.sno,d.sname from (select * from teacher where tname='孟扎扎' )a inner join course b on a.tno=b.tno inner join scores c on c.cno=b.cno inner join student d on d.sno=c.sno43-查询没学过"孟扎扎"老师讲授的任一门课程的学生姓名(与上题类似,"没学过"用not in来实现)
select sname from student where sno not in ( select a.sno from student a inner join scores b on a.sno=b.sno inner join course c on b.cno =c.cno inner join teacher d on c.tno =d.tno where d.tname ='孟扎扎');44.-查询没学过“孟扎扎”老师课的学生的学号、姓名(与上题类似)
select sno, sname from student where sno not in (select sno from scores where cno= (select cno from course where tno = (select tno from teacher where tname ='孟扎扎')));45查询选修“孟扎扎”老师所授课程的学生中成绩最高的学生姓名及其成绩(与上题类似,用成绩排名,用 limit 1得出最高一个)
select * from (select d.sname,c.result from (select * from teacher where tname='孟扎扎' )a inner join course b on a.tno=b.tno inner join scores c on c.cno=b.cno inner join student d on d.sno=c.sno order by c.result desc) where rownum=146.查询至少有一门课与学号为“0001”的学生所学课程相同的学生的学号和姓名
select sno,sname from student where sno in (select sno from scores where cno in (select cno from scores where sno=0001) and sno!=0001);47.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.sno,avg(a.result ), max(case when b.cname = '数学' then a.result else null end ) "数学", max(case when b.cname = '语文' then a.result else null end ) "语文", max(case when b.cname = '英语' then a.result else null end ) "英语" from scores a inner join course b on a.cno =b.cno group by a.sno;六.SQL高级功能:窗口函数 48.查询学生平均成绩及其名次
select sno, avg(result), row_number() over(order by avg(result) desc) from scores group by sno49.按各科成绩进行排序,并显示排名
select cno, sno, result, row_number()over(partition by cno order by result desc) from scores50.查询每门功成绩最好的前两名学生姓名
select b.cno,a.sname ,b.rank from student a inner join (select cno, sno, result, row_number()over(partition by cno order by result desc) rank from scores) b on a.sno=b.sno where b.rank=1 or b.rank=2 ;--不知道为什么这里用 where b.rank <=2结果不对)