日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java经典sql笔试题

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java经典sql笔试题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、 數據準備

-- 學生表 create table student(id varchar(50) not null comment '學號',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 '課程號',course_name varchar(50) not null comment '課程名稱',teacher_id varchar(50) not null comment '教師號',primary key (id) );-- 成績表 create table score(student_id varchar(50) not null comment '學號',course_id varchar(50) not null comment '課程號',score float not null comment '成績',foreign key (student_id) references student(id),foreign key (course_id) references course(id) );-- 教師表 create table teacher(id varchar(50) not null comment '教師號',teacher_name varchar(50) comment '教師姓名',primary key (id) );-- 學生表:添加數據 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' , '男');-- 課程表:添加數據 insert into course(id,course_name,teacher_id) values('0001' , '語文' , '0002');insert into course(id,course_name,teacher_id) values('0002' , '數學' , '0001');insert into course(id,course_name,teacher_id) values('0003' , '英語' , '0003');-- 成績表:添加數據 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);-- 教師表:添加數據 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題目

-- 查詢課程編號為“0002”的總成績 select sum(score) from score where course_id='0002'; -- 查詢選了課程的學生人數 select count(distinct student_id) from score; -- 查詢各科成績最高和最低的分, 以如下的形式顯示:課程號,最高分,最低分 select course_id, max(score) max_score, min(score) min_score from score group by course_id; -- 查詢每門課程被選修的學生數 select course_id, count(student_id) from score group by course_id; -- 查詢男生、女生人數 select sex, count(id) from student group by sex; -- 查詢平均成績大于60分學生的學號和平均成績 select student_id, avg(score) scores from score group by student_id having scores > 60; -- 查詢至少選修兩門課程的學生學號 select student_id, count(1) courses from score group by student_id having courses > 2; -- 查詢同名同姓學生名單并統計同名人數 select name,count(1) from student group by name having count(1) > 1; -- 查詢不及格的課程并按課程號從大到小排列 select course_id from score where score<=60 order by course_id desc ; -- 查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列 select course_id, avg(score) scores from score group by course_id order by scores asc,course_id desc ; -- 檢索課程編號為“0003”且分數小于60的學生學號,結果按按分數降序排列 select student_id from score where course_id='0003' and score < 60 order by score desc ; -- 查詢兩門以上不及格課程的同學的學號及其平均成績 select student_id, avg(score) avg_score from score where score < 60 group by student_id having count(course_id) > 1; -- 查詢學生的總成績并進行排名 select student_id, sum(score) sum_score from score group by student_id order by sum_score desc ; -- 查詢所有課程成績小于60分學生的學號、姓名 select id, name from student where id not in (select student_id from score where score >= 60 group by student_id); -- 查詢沒有學全所有課的學生的學號、姓名 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)); -- 查詢所有學生的學號、姓名、選課數、總成績 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; -- 查詢平均成績大于85的所有學生的學號、姓名和平均成績 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; -- 查詢學生的選課情況:學號,姓名,課程號,課程名稱 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; -- 查詢出每門課程的及格人數和不及格人數 select course_id, sum(IF(score >= 60, 1, 0)) 及格人數, sum(IF(score < 60, 1, 0)) 不及格人數 from score group by course_id; -- 使用分段[100-85],[85-70],[70-60],[<60]來統計各科成績,分別統計:各分數段人數,課程號和課程名稱 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;-- 查詢課程編號為0003且課程成績在80分以上的學生的學號和姓名 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"課程分數小于60,按分數降序排列的學生信息 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 ; -- 查詢課程名稱為"數學",且分數低于60的學生姓名和分數 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='數學' and s.score < 60; -- 查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績 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; -- 查詢不同課程成績相同的學生的學生編號、課程編號、學生成績 select student_id, course_id, score from score st group by score having count(course_id) > 1; -- 查詢學過編號為“0001”的課程并且也學過編號為“0002”的課程的學生的學號、姓名 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; -- 查詢學過“馬化騰”老師所教的所有課的同學的學號、姓名 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='馬化騰'; -- 查詢至少有一門課與學號為“0001”的學生所學課程相同的學生的學號和姓名 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'); -- 按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績 select s.student_id, avg(s.score) avg_score, max(case when c.course_name ='語文' then s.score else 0 end ) '語文',max(case when c.course_name ='數學' then s.score else 0 end ) '數學',max(case when c.course_name ='英語' then s.score else 0 end ) '英語' from score s left join course c on s.course_id = c.id group by s.student_id order by avg_score desc ;-- 查詢學生平均成績及其名次 -- 申明了一個變量 @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”課程成績高的所有學生的學號 -- 方法一 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;-- 查詢所有同學的學號,姓名,選課數,總成績 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;-- 查詢沒有學過葉平老師課的同學的學號,姓名 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 = '馬化騰');

總結

以上是生活随笔為你收集整理的java经典sql笔试题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。