java经典sql笔试题
生活随笔
收集整理的這篇文章主要介紹了
java经典sql笔试题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、 數(shù)據(jù)準(zhǔn)備
-- 學(xué)生表 create table student(id varchar(50) not null comment '學(xué)號(hào)',name varchar(50) not null comment '姓名',birthday date not null comment '生日',sex varchar(20) not null comment '性別',primary key (id) );-- 課程表 create table course(id varchar(50) not null comment '課程號(hào)',course_name varchar(50) not null comment '課程名稱',teacher_id varchar(50) not null comment '教師號(hào)',primary key (id) );-- 成績(jī)表 create table score(student_id varchar(50) not null comment '學(xué)號(hào)',course_id varchar(50) not null comment '課程號(hào)',score float not null comment '成績(jī)',foreign key (student_id) references student(id),foreign key (course_id) references course(id) );-- 教師表 create table teacher(id varchar(50) not null comment '教師號(hào)',teacher_name varchar(50) comment '教師姓名',primary key (id) );-- 學(xué)生表:添加數(shù)據(jù) insert into student(id,name,birthday,sex) values('0001' , '猴子' , '1989-01-01' , '男');insert into student(id,name,birthday,sex) values('0002' , '猴子' , '1990-12-21' , '女');insert into student(id,name,birthday,sex) values('0003' , '馬云' , '1991-12-21' , '男');insert into student(id,name,birthday,sex) values('0004' , '王思聰' , '1990-05-20' , '男');-- 課程表:添加數(shù)據(jù) insert into course(id,course_name,teacher_id) values('0001' , '語(yǔ)文' , '0002');insert into course(id,course_name,teacher_id) values('0002' , '數(shù)學(xué)' , '0001');insert into course(id,course_name,teacher_id) values('0003' , '英語(yǔ)' , '0003');-- 成績(jī)表:添加數(shù)據(jù) insert into score(student_id,course_id,score) values('0001' , '0001' , 80);insert into score(student_id,course_id,score) values('0001' , '0002' , 90);insert into score(student_id,course_id,score) values('0001' , '0003' , 99);insert into score(student_id,course_id,score) values('0002' , '0002' , 60);insert into score(student_id,course_id,score) values('0002' , '0003' , 80);insert into score(student_id,course_id,score) values('0003' , '0001' , 80);insert into score(student_id,course_id,score) values('0003' , '0002' , 80);insert into score(student_id,course_id,score) values('0003' , '0003' , 80);-- 教師表:添加數(shù)據(jù) insert into teacher(id,teacher_name) values('0001' , '孟扎扎');insert into teacher(id,teacher_name) values('0002' , '馬化騰');-- 這里的教師姓名是空值(null) insert into teacher(id,teacher_name) values('0003' , null);-- 這里的教師姓名是空字符串('') insert into teacher(id,teacher_name) values('0004' , '');二、 sql題目
-- 查詢課程編號(hào)為“0002”的總成績(jī) select sum(score) from score where course_id='0002'; -- 查詢選了課程的學(xué)生人數(shù) select count(distinct student_id) from score; -- 查詢各科成績(jī)最高和最低的分, 以如下的形式顯示:課程號(hào),最高分,最低分 select course_id, max(score) max_score, min(score) min_score from score group by course_id; -- 查詢每門課程被選修的學(xué)生數(shù) select course_id, count(student_id) from score group by course_id; -- 查詢男生、女生人數(shù) select sex, count(id) from student group by sex; -- 查詢平均成績(jī)大于60分學(xué)生的學(xué)號(hào)和平均成績(jī) select student_id, avg(score) scores from score group by student_id having scores > 60; -- 查詢至少選修兩門課程的學(xué)生學(xué)號(hào) select student_id, count(1) courses from score group by student_id having courses > 2; -- 查詢同名同姓學(xué)生名單并統(tǒng)計(jì)同名人數(shù) select name,count(1) from student group by name having count(1) > 1; -- 查詢不及格的課程并按課程號(hào)從大到小排列 select course_id from score where score<=60 order by course_id desc ; -- 查詢每門課程的平均成績(jī),結(jié)果按平均成績(jī)升序排序,平均成績(jī)相同時(shí),按課程號(hào)降序排列 select course_id, avg(score) scores from score group by course_id order by scores asc,course_id desc ; -- 檢索課程編號(hào)為“0003”且分?jǐn)?shù)小于60的學(xué)生學(xué)號(hào),結(jié)果按按分?jǐn)?shù)降序排列 select student_id from score where course_id='0003' and score < 60 order by score desc ; -- 查詢兩門以上不及格課程的同學(xué)的學(xué)號(hào)及其平均成績(jī) select student_id, avg(score) avg_score from score where score < 60 group by student_id having count(course_id) > 1; -- 查詢學(xué)生的總成績(jī)并進(jìn)行排名 select student_id, sum(score) sum_score from score group by student_id order by sum_score desc ; -- 查詢所有課程成績(jī)小于60分學(xué)生的學(xué)號(hào)、姓名 select id, name from student where id not in (select student_id from score where score >= 60 group by student_id); -- 查詢沒(méi)有學(xué)全所有課的學(xué)生的學(xué)號(hào)、姓名 select id, name from student where id not in (select student_id from score group by student_id having count(course_id) = (select count(id) from course)); -- 查詢所有學(xué)生的學(xué)號(hào)、姓名、選課數(shù)、總成績(jī) select st.id, st.name, count(s.course_id), sum(s.score) from student st left join score s on st.id = s.student_id group by s.student_id; -- 查詢平均成績(jī)大于85的所有學(xué)生的學(xué)號(hào)、姓名和平均成績(jī) select st.id, st.name, avg(s.score) avg_score from student st left join score s on st.id = s.student_id group by s.student_id having avg_score > 85; -- 查詢學(xué)生的選課情況:學(xué)號(hào),姓名,課程號(hào),課程名稱 select st.id, st.name, c.id, c.course_name from student st inner join score s on st.id = s.student_id inner join course c on s.course_id = c.id; -- 查詢出每門課程的及格人數(shù)和不及格人數(shù) select course_id, sum(IF(score >= 60, 1, 0)) 及格人數(shù), sum(IF(score < 60, 1, 0)) 不及格人數(shù) from score group by course_id; -- 使用分段[100-85],[85-70],[70-60],[<60]來(lái)統(tǒng)計(jì)各科成績(jī),分別統(tǒng)計(jì):各分?jǐn)?shù)段人數(shù),課程號(hào)和課程名稱 select course_id, sum(case when score between 85 and 100 then 1 else 0 end ) '[100-85]',sum(case when score between 70 and 85 then 1 else 0 end ) '[85-70]',sum(case when score between 60 and 70 then 1 else 0 end ) '[70-60]',sum(case when score < 60 then 1 else 0 end ) '[<60]' from score group by course_id;-- 查詢課程編號(hào)為0003且課程成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名 select st.id, st.name from student st left join score s on st.id = s.student_id where s.course_id='0003' and s.score > 80;-- 檢索"0003"課程分?jǐn)?shù)小于60,按分?jǐn)?shù)降序排列的學(xué)生信息 select st.id, st.name from student st left join score s on st.id = s.student_id where s.course_id='0003' and s.score < 60 order by s.score desc; -- 查詢不同老師所教不同課程平均分從高到低顯示 select t.id, t.teacher_name, avg(s.score) avg_score from teacher t left join course c on t.id = c.teacher_id left join score s on c.id = s.course_id group by t.id order by avg_score desc ; -- 查詢課程名稱為"數(shù)學(xué)",且分?jǐn)?shù)低于60的學(xué)生姓名和分?jǐn)?shù) select st.name, c.id, s.score from student st inner join score s on st.id = s.student_id inner join course c on s.course_id = c.id where c.course_name='數(shù)學(xué)' and s.score < 60; -- 查詢兩門及其以上不及格課程的同學(xué)的學(xué)號(hào),姓名及其平均成績(jī) select student_id, st.name, avg(score) avg_score from student st left join score s on st.id = s.student_id where score < 60 group by student_id having count(course_id) > 1; -- 查詢不同課程成績(jī)相同的學(xué)生的學(xué)生編號(hào)、課程編號(hào)、學(xué)生成績(jī) select student_id, course_id, score from score st group by score having count(course_id) > 1; -- 查詢學(xué)過(guò)編號(hào)為“0001”的課程并且也學(xué)過(guò)編號(hào)為“0002”的課程的學(xué)生的學(xué)號(hào)、姓名 select st.id, st.name from student st left join score s on st.id = s.student_id where s.course_id in ('0001','0002') group by s.student_id having count(course_id) > 1; -- 查詢學(xué)過(guò)“馬化騰”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名 select s2.id, s2.name from teacher t left join course c on t.id = c.teacher_id left join score s on c.id = s.course_id left join student s2 on s.student_id = s2.id where t.teacher_name='馬化騰'; -- 查詢至少有一門課與學(xué)號(hào)為“0001”的學(xué)生所學(xué)課程相同的學(xué)生的學(xué)號(hào)和姓名 select * from student where id in (select s2.student_id from score s2 where s2.course_id in (select s.course_id from score s where s.student_id='0001') and s2.student_id !='0001'); -- 按平均成績(jī)從高到低顯示所有學(xué)生的所有課程的成績(jī)以及平均成績(jī) select s.student_id, avg(s.score) avg_score, max(case when c.course_name ='語(yǔ)文' then s.score else 0 end ) '語(yǔ)文',max(case when c.course_name ='數(shù)學(xué)' then s.score else 0 end ) '數(shù)學(xué)',max(case when c.course_name ='英語(yǔ)' then s.score else 0 end ) '英語(yǔ)' from score s left join course c on s.course_id = c.id group by s.student_id order by avg_score desc ;-- 查詢學(xué)生平均成績(jī)及其名次 -- 申明了一個(gè)變量 @curRank ,并將此變量初始化為0,查得一行將此變量加一 select student_id, avg(score) avg_score, @curRank := @curRank + 1 cc from score, (select @curRank:=0) r group by student_id order by avg_score desc ;-- 查詢“0001”課程比“0002”課程成績(jī)高的所有學(xué)生的學(xué)號(hào) -- 方法一 select * from student st where (select s1.score score1 from score s1 where s1.course_id='0001' and st.id=s1.student_id)> (select s2.score score2 from score s2 where s2.course_id='0002' and st.id=s2.student_id); -- 方法二 select st.id, st.name from student st left join score s on st.id = s.student_id left join score s1 on st.id = s1.student_id where s.course_id='0001' and s1.course_id='0002' and s.score > s1.score;-- 查詢所有同學(xué)的學(xué)號(hào),姓名,選課數(shù),總成績(jī) select st.id, st.name, count(s.course_id), sum(s.score) from student st left join score s on st.id = s.student_id group by s.student_id;-- 查詢沒(méi)有學(xué)過(guò)葉平老師課的同學(xué)的學(xué)號(hào),姓名 select st.id, st.name from student st where st.id not in( select s.student_id from score s inner join course c on s.course_id = c.id inner join teacher t on c.teacher_id = t.id where t.teacher_name = '馬化騰');總結(jié)
以上是生活随笔為你收集整理的java经典sql笔试题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java异常处理原则与技巧总结
- 下一篇: OpenCV学习笔记(三)——Mat,图