SQL50题
2021/7/8 19:06:37
本文主要是介绍SQL50题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
SQL50题
首先建表
–建表
–学生表
CREATE TABLE Student (
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT ‘’,
s_birth VARCHAR(20) NOT NULL DEFAULT ‘’,
s_sex VARCHAR(10) NOT NULL DEFAULT ‘’,
PRIMARY KEY( s_id )
);
–课程表
CREATE TABLE Course (
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT ‘’,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY( c_id )
);
–教师表
CREATE TABLE Teacher (
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT ‘’,
PRIMARY KEY( t_id )
);
–成绩表
CREATE TABLE Score (
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY( s_id , c_id )
);
–插入学生表测试数据
insert into Student values(‘01’ , ‘赵雷’ , ‘1990-01-01’ , ‘男’);
insert into Student values(‘02’ , ‘钱电’ , ‘1990-12-21’ , ‘男’);
insert into Student values(‘03’ , ‘孙风’ , ‘1990-05-20’ , ‘男’);
insert into Student values(‘04’ , ‘李云’ , ‘1990-08-06’ , ‘男’);
insert into Student values(‘05’ , ‘周梅’ , ‘1991-12-01’ , ‘女’);
insert into Student values(‘06’ , ‘吴兰’ , ‘1992-03-01’ , ‘女’);
insert into Student values(‘07’ , ‘郑竹’ , ‘1989-07-01’ , ‘女’);
insert into Student values(‘08’ , ‘王菊’ , ‘1990-01-20’ , ‘女’);
–课程表测试数据
insert into Course values(‘01’ , ‘语文’ , ‘02’);
insert into Course values(‘02’ , ‘数学’ , ‘01’);
insert into Course values(‘03’ , ‘英语’ , ‘03’);
–教师表测试数据
insert into Teacher values(‘01’ , ‘张三’);
insert into Teacher values(‘02’ , ‘李四’);
insert into Teacher values(‘03’ , ‘王五’);
–成绩表测试数据
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);
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT st.*,sc1.s_score AS "课程01",sc2.s_score AS "课程02" 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" WHERE sc1.s_score>sc2.s_score;
看要求,需要查询 学生的信息 和 课程分数
所以需要 学生表和成绩表进行关联查询 关联条件就是学生的id
又限制条件是 01课程 和 02课程,
所以 需要限制成绩表中的课程 id = 01 , 02
最后做一个 比较,01课程成绩 大于 02 课程成绩
相同类型
查询“01”课程比“02”课程成绩高的所有学生的学号;
#课程01的所有学生和成绩
(SELECT s_id,s_score from score sc where sc.c_id =‘01’) a,
#课程02的所有学生和成绩
(SELECT s_id,s_score from score sc where sc.c_id = ‘02’) b
#查询的条件(针对的是同一个学生)
where a.s_id = b.s_id;
#查询的是课程01比02分数高的所有学生的编号
SELECT a.sid
where a.s_score > b.s_score
#整个的查询语句
SELECT a.s_id from (SELECT s_id,s_score from score sc where sc.c_id ='01') a, (SELECT s_id,s_score from score sc where sc.c_id = '02') b where a.s_score > b.s_score AND a.s_id = b.s_id;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
同上
3 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.s_id,a.s_name,ROUND(AVG(b.s_score),2) as avg_score
from
student a join score b
on a.s_id = b.s_id
GROUP BY
a.s_id,a.s_name
HAVING avg_score >=60;
因为要查询的是 学生姓名,学号,以及平均成绩
所以是 查询 学生表 和 成绩表
根据学生进行分组(group by) ,并且 平均成绩 大于60
如果是 查询平均成绩大于60分的同学的学号和平均成绩; 不包括学生姓名,可以直接查询成绩表就行
#查询条件:平均成绩大于60分,但需要根据学号进行分组
GROUP BY s_id HAVING avg(s_score) > 60
#查询的是满足条件的所有学生的学号和平均成绩
s_id,avg(s_score) from score
#整个查询语句
SELECT s_id,avg(s_score) from score GROUP BY s_id HAVING avg(s_score) > 60
4 .查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from student a left join score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name;
.查询所有同学的学号、姓名、选课数、总成绩
涉及到的表有student,score 关联条件就是学生id(两表关联)
5. 查询"李"姓老师的数量 (模糊查询)
select count(t_id) from teacher where t_name like ‘李%’;
6、查询学过"张三"老师授课的同学的信息
student 表 和 teacher表 没有关联关系,
而 student表和成绩表有关联,
教师表和课程表有关联,课程表和成绩表有关联
所以可以根据题目,先找到 张三老师教的课程id,
查询张三老师教的课程id,
select c_id from course c join teacher t on c.t_id = t.t_id and t.t_name = ‘张三’
再查询有谁学过,没办法直接关联,所以和成绩表进行关联,成绩表中有课程id,限制课程id是张三老师教的,所以结果就是
select stu.* from student stu join score sc on stu.s_id=sc.s_id where sc.c_id in (select c_id from course c join teacher t on c.t_id = t.t_id and t.t_name = '张三')
8、查询没学过"张三"老师授课的同学的信息
可以用反向思维
先查询出学过张三老师教过的学生id
select stu.s_id from student stu join score sc on stu.s_id=sc.s_id where sc.c_id in( select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三')
然后查询学生表,看谁不在上面的结果里
select * from student stu where stu.s_id not in( select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三'));
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.* from
student a,score b,score c
where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id=‘01’ and c.c_id=‘02’;
方法二
分别查出来学过 01 和 02 的学生,最后将这两个查询结果做限制
select a.s_id,a.S_name from (select student.s_id,student.s_Name from student,course ,score where course.c_id='01' and student.s_id=score.s_id and course.c_id=score.c_id) a, (select student.s_id,student.s_Name from student,course ,score where course.c_id='02' and student.s_id=score.s_id and course.c_id=score.c_id) b where a.s_id=b.s_id
10 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
//查询学过课程01的学生id ,
select s_id from score where c_id=‘01’
//查询学过课程02的学生id ,
select s_id from score where c_id=‘02’
//查询学过01 并且没有学过02 的学生id
select a.* from student a where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02') +
11、查询没有学全所有课程的同学的信息
先查询成绩表中以学生分组 count(*) 等于 课程总数的学生
然后去掉这部分学生
select * from student where s_id not in( select s_id from score t1 group by s_id having count(*) =(select count(distinct c_id) from course))
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
查出来 01 同学都学过哪些课程
select a.c_id from score a where a.s_id=‘01’
查出来成绩表中谁 学习了 和01同学 所学的课程
select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id=‘01’)
得到学生id之后,就可以查询学生表,查询学生信息
select * from student where s_id in( select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id='01') );
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT Student.* FROM Student WHERE s_id IN (SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(s_id) = ( #下面的语句是找到'01'同学学习的课程总数 SELECT COUNT(c_id) FROM Score WHERE s_id = '01' ) ) # 上面这个条件是过滤和01同学学习课程总总数不一样的人 AND s_id NOT IN ( #下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们 SELECT s_id FROM Score WHERE c_id IN( #下面的语句是找到‘01’同学没学过的课程 SELECT DISTINCT c_id FROM Score WHERE c_id NOT IN ( #下面的语句是找出‘01’同学学习的课程 SELECT c_id FROM Score WHERE s_id = '01' ) ) GROUP BY s_id ) #下面的条件是排除01同学 AND s_id NOT IN ('01')
14 查询没学过"张三"老师讲授的任一门课程的学生姓名
select a.s_name from student a where a.s_id not in ( select s_id from score where c_id = # 查询学过这些课程的人 (select c_id from course where t_id =( # 查询张三教的课程id select t_id from teacher where t_name = '张三'))); #查询张三的ID
这篇关于SQL50题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27消息中间件底层原理资料详解
- 2024-11-27RocketMQ底层原理资料详解:新手入门教程
- 2024-11-27MQ底层原理资料详解:新手入门教程
- 2024-11-27MQ项目开发资料入门教程
- 2024-11-27RocketMQ源码资料详解:新手入门教程
- 2024-11-27本地多文件上传简易教程
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器