SQL笔试50题
Posted unuliha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL笔试50题相关的知识,希望对你有一定的参考价值。
工具:Navicat Premium 封装的mysql。
1.表的创建
-- 创建数据库 create database school; use school; -- 建表 -- 学生表:学生编号,学生姓名, 出生年月,学生性别 create table Student(s_id varchar(10),s_name nvarchar(10),s_birth datetime,s_sex nvarchar(10)); insert into Student values(\'01\' , N\'赵雷\' , \'1990-01-01\' , N\'男\'); insert into Student values(\'02\' , N\'钱电\' , \'1990-12-21\' , N\'男\'); insert into Student values(\'03\' , N\'孙风\' , \'1990-05-20\' , N\'男\'); insert into Student values(\'04\' , N\'李云\' , \'1990-08-06\' , N\'男\'); insert into Student values(\'05\' , N\'周梅\' , \'1991-12-01\' , N\'女\'); insert into Student values(\'06\' , N\'吴兰\' , \'1992-03-01\' , N\'女\'); insert into Student values(\'07\' , N\'郑竹\' , \'1989-07-01\' , N\'女\'); insert into Student values(\'08\' , N\'王菊\' , \'1990-01-20\' , N\'女\'); -- 课程表:课程编号, 课程名称, 教师编号 create table Course(c_id varchar(10),c_name nvarchar(10),t_id varchar(10)); insert into Course values(\'01\' , N\'语文\' , \'02\'); insert into Course values(\'02\' , N\'数学\' , \'01\'); insert into Course values(\'03\' , N\'英语\' , \'03\'); -- 教师表:教师编号,教师姓名 create table Teacher(t_id varchar(10),t_name nvarchar(10)); insert into Teacher values(\'01\' , N\'张三\'); insert into Teacher values(\'02\' , N\'李四\'); insert into Teacher values(\'03\' , N\'王五\'); -- 成绩表:学生编号,课程编号,分数 create table Score(s_id varchar(10),c_id varchar(10),s_score decimal(18,1)); insert into Score values(\'01\' , \'01\' , 80); insert into Score values(\'01\' , \'02\' , 90); insert into Score values(\'01\' , \'03\' , 99); insert into Score values(\'02\' , \'01\' , 70); insert into Score values(\'02\' , \'02\' , 60); insert into Score values(\'02\' , \'03\' , 80); insert into Score values(\'03\' , \'01\' , 80); insert into Score values(\'03\' , \'02\' , 80); insert into Score values(\'03\' , \'03\' , 80); insert into Score values(\'04\' , \'01\' , 50); insert into Score values(\'04\' , \'02\' , 30); insert into Score values(\'04\' , \'03\' , 20); insert into Score values(\'05\' , \'01\' , 76); insert into Score values(\'05\' , \'02\' , 87); insert into Score values(\'06\' , \'01\' , 31); insert into Score values(\'06\' , \'03\' , 34); insert into Score values(\'07\' , \'02\' , 89); insert into Score values(\'07\' , \'03\' , 98);
2.表的结构
3.笔试50题
-- 1.查询“01”课程比“02”课程成绩高的所有学生的学号 SELECT st.*, sc1.s_score as "课程1", sc2.s_score as "课程2", sc3.s_score as "课程3" From student st JOIN score sc1 on st.s_id=sc1.S_id AND sc1.c_id = "01" JOIN score sc2 on st.s_id=sc2.S_id AND sc2.c_id = "02" JOIN score sc3 on st.s_id=sc3.S_id AND sc3.c_id ="03" WHERE sc1.s_score>sc2.s_score -- 2.查询平均成绩大于60分的同学的学号和平均成绩 SELECT st.s_id,s_name,ROUND(AVG(s_score),2) as avg_score FROM student st JOIN score on st.s_id = score.s_id GROUP BY s_id HAVING avg(s_score)>=60 #HAVING子句给出了选择组的条件;where作用于基本表或视图,having作用于组;WHERE子句中不能用聚集函数做条件表达式 -- 3.查询所有同学的学号、姓名、选课数、总成绩 SELECT st.s_id,st.s_name,count(c_id) as "选课总数",sum(s_score) as total_score from student st left join score on st.s_id=score.s_id GROUP BY st.s_id #有学生未出现在成绩表上,用左连接,保证出现在学生表的学生都被输出 -- 4.查询姓“李”的老师的个数 SELECT count(t_id) from teacher where t_name like \'李%\' -- 5.查询没学过“张三”老师课的同学的学号、姓名 SELECT st1.* from student st1 where s_id not in (SELECT st.s_id from student st,score sc,course c,teacher te WHERE st.s_id = sc.s_id and sc.c_id = c.c_id and c.t_id = te.t_id and te.t_name = "张三") -- 6.查询学过“张三”老师所教的课的同学的学号、姓名; SELECT st.* from student st join score on st.s_id = score.s_id join course on score.c_id = course.c_id join teacher on course.t_id = teacher.t_id AND teacher.t_name = "张三" SELECT st.* from student st,score sc,course c,teacher te WHERE st.s_id = sc.s_id and sc.c_id = c.c_id and c.t_id = te.t_id and te.t_name = "张三" -- 7.查询学过编号“01”并且也学过编号“02”课程的同学的学号、姓名; SELECT st.* from student st join score sc1 on st.s_id = sc1.s_id and sc1.c_id = 01 join score sc2 on st.s_id = sc2.s_id and sc2.c_id = 02 -- 8.查询学过01但是没有学过02的同学的信息 SELECT st.* from student st join score s1 on st.s_id = s1.s_id and s1.c_id = 01 where st.s_id not in (SELECT s_id from score WHERE c_id = 02) #字段前最好都跟上表名 -- 9.查询所有课程成绩小于60分的同学的学号、姓名; SELECT st.s_id,s_name from student st WHERE s_id in( SELECT s_id from score GROUP BY s_id HAVING max(s_score)<60 ) -- 10.查询没有学全所有课的同学的学号、姓名 select * from student where s_id not in( SELECT s_id from score GROUP BY s_id HAVING count(c_id) = ( select count(c_id) from course ) ) -- 11.查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名 SELECT st.* from student st where s_id in ( SELECT s_id from score where c_id in( SELECT c_id from score WHERE s_id = 01 ) and s_id not in ("01") ) #思路类似,但将所有表放在一起可以精简过程 SELECT DISTINCT st.* FROM student st JOIN score sc ON st.s_id=sc.s_id AND sc.c_id IN ( SELECT sc.c_id FROM score sc WHERE sc.s_id="01" ) AND sc.s_id NOT IN ("01"); -- 12.查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名 SELECT st.* from student st join score sc on st.s_id = sc.s_id WHERE st.s_id not in ( SELECT s_id from student WHERE c_id not in( SELECT c_id from score where s_id = "01" ) ) GROUP BY st.s_id HAVING COUNT(c_id)=(SELECT count(c_id) from score WHERE s_id = "01") #虚拟表的名称为局部变量 -- 14.查询没学过"张三"老师讲授的任一门课程的学生姓名 SELECT s_name from student WHERE s_id not in( SELECT st.s_id FROM student st join score sc on st.s_id = sc.s_id join course c on sc.c_id = c.c_id join teacher te on te.t_id = c.t_id and t_name = "张三" ) -- 15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 SELECT st.s_id,s_name,ROUND(avg(sc.s_score),2) as avg_score from student st join score sc on st.s_id = sc.s_id WHERE st.s_id in ( SELECT s_id from score WHERE s_score < "60" GROUP BY s_id HAVING COUNT(*)> 1 ) GROUP BY st.s_id #有分组汇总函数就要有group by,除非只有一个组 SELECT st.s_id,st.s_name,ROUND(avg(sc.s_score),2) as avg_score from student st join score sc on st.s_id = sc.s_id and sc.s_score<\'60\' GROUP BY s_id HAVING count(s_score) > 1 -- 16.检索"01"课程分数小于60,按分数降序排列的学生信息 SELECT st.* from student st JOIN score sc on st.s_id = sc.s_id WHERE sc.s_score < 60 and sc.c_id = "01" ORDER BY sc.s_score DESC -- 17.按平均成绩从高到低显示所有学生的平均成绩 SELECT st.s_id, st.s_name,sc1.s_score as "01",sc2.s_score as "02",sc3.s_score as "03",ROUND(avg(sc.s_score),2) as "avg_score" from student st left join score sc on st.s_id = sc.s_id left join score sc1 on st.s_id = sc1.s_id and sc1.c_id = "01" left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = "02" left join score sc3 on st.s_id = sc3.s_id and sc3.c_id = "03" GROUP BY st.s_id ORDER BY avg(sc.s_score) DESC; -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率 SELECT a.c_id "课程ID",a.c_name "课程名", max(b.s_score) "最高分", min(b.s_score) "最低分", ROUND(avg(b.s_score),2) "平均分", sum(case when b.s_score>60 then 1 else 0 end)/count(1) "及格率", sum(case when b.s_score>=70 and b.s_score < 80 then 1 else 0 end)/count(1) "中等率", sum(case when b.s_score>=80 and b.s_score < 90 then 1 else 0 end)/count(1) "优良率", sum(case when b.s_score>=90 then 1 else 0 end)/count(1) "优秀率" from course a join score b on a.c_id = b.c_id GROUP BY 1 -- 19.按各科平均成绩从低到高和及格率的百分数从高到低顺序 SELECT sc.c_id,c_name,round(avg(s_score),2) \'avg_score\', concat(round(sum(case when s_score >= 60 then 1 else 0 end)/count(1)*100,2),\'%\') "及格率" from score sc join course c on sc.c_id = c.c_id GROUP BY sc.c_id ORDER BY avg_score,"及格率" desc -- 20.查询学生的总成绩并进行排名 --有rank函数时 SELECT sc.s_id,st.s_name,sum(s_score) as sum_score, rank() over(ORDER BY sum(sc.s_score) desc) as score_rank FROM score sc join student st on sc.s_id = st.s_id GROUP BY sc.s_id; --无rank函数时 SELECT a.s_id,a.s_name, @i := @i +1 as 序号, @k := (case when @score = a.total_score then @k else @i end) as 排名, @score := a.total_score as total_score from( SELECT st.s_id ,s_name ,sum(sc.s_score) as total_score from student st join score sc on st.s_id = sc.s_id GROUP BY st.s_id ORDER BY total_score desc )a, (SELECT @i:=0,@k:=0,@score:=0)b -- 21.查询不同老师所教不同课程平均分从高到低显示 SELECT t.t_id,t_name,c.c_id,c.c_name, round(avg(s_score),2) avg_score from teacher t join course c on t.t_id =某公司笔试题解读