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 = '马化腾');

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值