参考别人学习,有些代码为借鉴
创建表添加数据
学生表
--创建表
create table student (
s_id int,
s_name varchar(20),
s_birth date,
s_sex varchar(4))
--插入数据
insert into student values
(1,'赵雷','1990-01-01','男'),
(2,'钱电','1990-12-21','男'),
(3,'孙风','1990-05-20','男'),
(4,'李云','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吴兰','1992-03-01','女'),
(7,'郑竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');
成绩表
--创建表
create table score (
s_id int,
c_id int,
s_score int)
--插入数据
insert into score values
(1,1,80),
(1,2,90),
(1,3,99),
(2,1,70),
(2,2,60),
(2,3,65),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,40),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);
课程表
--创建表
CREATE TABLE course(c_id int,c_name VARCHAR(20),t_id int);
--插入数据
insert into course values
(1,'语文',2),
(2,'数学',1),
(3,'英语',3);
教师表
--创建表
create table teacher (
t_id int,
t_name varchar(20))
--插入数据
insert into teacher values
(1,'张三'),
(2,'李四'),
(3,'王五');
50道题详细代码
1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT student.*,c1.s_score as 课程1分数,c2.s_score 课程2分数 from student
join (
SELECT s_id,s_score from score where c_id=1)c1 on student.s_id=c1.s_id
join (
SELECT s_id,s_score from score where c_id=2)c2 on c1.s_id=c2.s_id
WHERE c1.s_score>c2.s_score
2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT student.*,c1.s_score as 课程1分数,c2.s_score 课程2分数 from student
join (
SELECT s_id,s_score from score where c_id=1)c1 on student.s_id=c1.s_id
join (
SELECT s_id,s_score from score where c_id=2)c2 on c1.s_id=c2.s_id
WHERE c1.s_score<c2.s_score
3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT score.s_id,s_name,avg(s_score)平均成绩 from score join student
on score.s_id=student.s_id
GROUP BY score.s_id,s_name HAVING 平均成绩>=60
4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
SELECT student.s_id,s_name,ifnull(avg(s_score),0)平均成绩 from student
left join score on score.s_id=student.s_id
GROUP BY student.s_id,s_name HAVING 平均成绩 is null or 平均成绩<60
5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT student.s_id,s_name,count(*)课程数,ifnull(sum(s_score),0)总成绩
from student LEFT JOIN score on student.s_id=score.s_id
GROUP BY student.s_id,s_name
6.查询"李"姓老师的数量
SELECT count(*) 李姓数量
from teacher WHERE t_name like '李%'
7.询学过"张三"老师授课的同学的信息
SELECT student.* from student
join score on student.s_id=score.s_id
join course on score.c_id=course.c_id
join teacher on course.t_id=teacher.t_id
where t_name='张三'
8.查询没学过"张三"老师授课的同学的信息
SELECT s.* from student s WHERE s_id not in(
SELECT DISTINCT s_id from score sc join(
SELECT c_id from course c join teacher t on c.t_id=t.t_id
where t_name='张三')t1 on sc.c_id=t1.c_id);
9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT s.* from student s where s_id in (
SELECT s_id from score where c_id in(1,2) GROUP BY s_id HAVING count(*)=2)
10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select s.*from student s
where s_id in (
select s_id
from score
where c_id = 1
and s_id not in (
select s_id from score where c_id = 2
))
11.查询没有学全所有课程的同学的信息
select s.*
from student s
where s_id not in
(select s_id
from score
where c_id in (1,2,3)
GROUP BY s_id having count(*)=3);
12、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名
SELECT * from student where s_id in(
SELECT DISTINCT s_id from score s join (
SELECT c_id from score s where s_id=1)t1 on s.c_id=t1.c_id)
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT * from student where s_id in (
SELECT s_id from score s join (
SELECT c_id from score s where s_id=1)t1 on s.c_id=t1.c_id
where s_id!=1 GROUP BY s_id HAVING count(*)=(SELECT count(*) from score where s_id=1))
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name
from student
where s_id not in (
select s_id
from score sc
inner join (
select c_id
from course c
inner join teacher t on c.t_id = t.t_id
where t_name = '张三'
) t1
on sc.c_id = t1.c_id
);
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT student.s_id,s_name,round(avg(s_score),2) 平均成绩 from student
JOIN score on student.s_id=score.s_id
WHERE s_score<60 GROUP BY student.s_id,s_name having count(*)>=2
16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT s.*,s_score from student s join
(SELECT s_id,s_score from score where c_id=1 and s_score<60 )t1 on s.s_id= t1.s_id
order by s_score desc
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.s_id,sum(case c_id when 1 then s_score else 0 end) as 语文,
sum(case c_id when 2 then s_score else 0 end) as 数学,
sum(case c_id when 3 then s_score else 0 end) as 英语,
ifnull(round(avg(s_score), 2), 0) as 平均成绩
from score a GROUP BY a.s_id ORDER BY 平均成绩 DESC;
18、查询各科成绩最高分、最低分和平均分,以如下形式显示:
课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select
score.c_id as 课程ID,course.c_name as 课程名称,
max(s_score) as 最高分,
min(s_score) as 最低分,
round(avg(s_score), 2) as 平均分, concat(round(sum(case when s_score >= 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 及格率,
concat(round(sum(case when s_score between 70 and 80 then 1 else 0 end) / count(*) * 100, 2), '%') as 中等率,
concat(round(sum(case when s_score between 80 and 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优良率,
concat(round(sum(case when s_score >= 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优秀率
from score left join course on score.c_id=course.c_id
group by score.c_id,course.c_name
19、按各科成绩进行排序,并显示排名
select c.c_id,c.c_name,s.s_id,s.s_name,s_score,row_number() over (partition by c.c_id order by s_score desc) as 'm_rank'
from score sc
inner join student s on sc.s_id = s.s_id
inner join course c on sc.c_id = c.c_id
group by c.c_id,c.c_name,s.s_id,s.s_name,s_score
order by c.c_id,s_score desc
20、查询学生的总成绩并进行排名
SELECT student.s_id,s_name,sum(s_score)总成绩, rank() over (order by sum(s_score) desc) as 排名
from student join score on student.s_id=score.s_id GROUP BY student.s_id,s_name ORDER BY 总成绩 DESC
21、查询不同老师所教不同课程平均分从高到低显示
SELECT teacher.t_id,t_name,course.c_id,round(avg(s_score),2) as avg_score
from score join course on score.c_id=course.c_id join teacher on teacher.t_id=course.t_id GROUP BY t_id,t_name,course.c_id ORDER BY teacher.t_id,avg_score
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT * from (
select score.c_id,c_name,score.s_id,s_name,s_birth,s_sex,s_score,row_number() over (partition by score.c_id order by s_score desc) as 'm_rank'
from score join course on score.c_id=course.c_id join student on student.s_id=score.s_id
group by score.c_id,c_name,score.s_id,s_name,s_birth,s_sex,s_score
order by score.c_id,s_score desc)t1 WHERE m_rank in(2,3)
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select
c.c_id as 课程编号,c.c_name as 课程名称,
sum(case when s_score between 85 and 100 then 1 else 0 end) as '[100-85]',
concat(round(sum(case when s_score between 85 and 100 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
sum(case when s_score between 70 and 85 then 1 else 0 end) as '[85-70]',
concat(round(sum(case when s_score between 70 and 85 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
sum(case when s_score between 60 and 70 then 1 else 0 end) as '[70-60]',
concat(round(sum(case when s_score between 60 and 70 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
sum(case when s_score between 0 and 60 then 1 else 0 end) as '[0-60]',
concat(round(sum(case when s_score between 0 and 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比
from course c
inner join score sc
on c.c_id = sc.c_id
group by c.c_id,c.c_name
24、查询学生平均成绩及其名次
SELECT row_number() over (order by avg(s_score) desc ) 排名,score.s_id,s_name,avg(s_score) as avg_score
from score join student on score.s_id=student.s_id
GROUP BY score.s_id,s_name ORDER BY avg_score desc
25、查询各科成绩前三名的记录
SELECT * FROM (
SELECT score.c_id,c_name,score.s_id,s_name,s_score,row_number() over(PARTITION by score.c_id ORDER BY s_score desc) 排名
from score join course on score.c_id=course.c_id join student on student.s_id=score.s_id GROUP BY score.c_id,c_name,score.s_id,s_name,s_score ORDER BY score.c_id,s_score desc) t1 where 排名<=3
26、查询每门课程被选修的学生数
SELECT course.c_id,c_name,count(*) as 人数
from course join score on course.c_id=score.c_id
GROUP BY course.c_id,c_name order by course.c_id
27、查询出只有两门课程的全部学生的学号和姓名
SELECT score.s_id,s_name from student join score on student.s_id=score.s_id
GROUP BY score.s_id,s_name having count(*)=2
28、查询男生、女生人数
SELECT s_sex,count(*)as 人数 from student GROUP BY s_sex
29、查询名字中含有"风"字的学生信息
SELECT * from student WHERE s_name like '%风%'
30、查询同名同性学生名单,并统计同名人数
SELECT s_name,s_sex,count(*) from student GROUP BY s_name,s_sex HAVING count(*)>1
31、查询1990年出生的学生名单
SELECT * from student where YEAR(s_birth)='1990'
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT score.c_id,c_name,avg(s_score) as 平均成绩
from score join course on score.c_id=course.c_id
GROUP BY score.c_id,c_name ORDER BY 平均成绩 desc,course.c_id
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT student.s_id,s_name,ROUND(avg(s_score),2) 平均成绩
from student join score on student.s_id=score.s_id
GROUP BY student.s_id,s_name HAVING 平均成绩>=85
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT s_name,s_score from student join score on student.s_id=score.s_id
join course on course.c_id =score.c_id where c_name='数学' and s_score<60
35、查询所有学生的课程及分数情况
select a.s_id,a.s_name,
SUM(case c.c_name when '语文' then b.s_score else 0 end) as '语文',
SUM(case c.c_name when '数学' then b.s_score else 0 end) as '数学',
SUM(case c.c_name when '英语' then b.s_score else 0 end) as '英语',
IFNULL(SUM(b.s_score),0) as '总分'
from student a left join score b on a.s_id = b.s_id
left join course c on b.c_id = c.c_id
GROUP BY a.s_id,a.s_name
36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
--第一种
SELECT s_name,c_name,s_score from student join score on student.s_id=score.s_id join course on score.c_id=course.c_id where s_score>70 GROUP BY student.s_id,s_name,c_name,s_score
--第二种
select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id
left join student a on a.s_id=c.s_id where c.s_score>=70
37、查询课程不及格的学生
SELECT student.s_id,s_name,score.c_id,c_name,s_score
from student join score on student.s_id=score.s_id
join course on score.c_id=course.c_id
where s_score<60 GROUP BY student.s_id,s_name,score.c_id,c_name,s_score
ORDER BY student.s_id,score.c_id
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT student.s_id,s_name from student join score on student.s_id=score.s_id
where c_id=1 and s_score>=80
39、求每门课程的学生人数
SELECT score.c_id,c_name,count(*) 人数 from score join course on score.c_id=course.c_id GROUP BY score.c_id,c_name
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT * from(
SELECT student.s_id,s_name,s_birth,s_sex,s_score,row_number() over(ORDER BY s_score desc) 排名 from student
join score on student.s_id=score.s_id
join course on score.c_id=course.c_id
join teacher on course.t_id=teacher.t_id
where t_name='张三') t1 where 排名=1
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT * from score where s_score in(
SELECT s_score from score GROUP BY s_score HAVING count(*)>1)
select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score
42、查询每门课程成绩最好的前三名
SELECT * from(
SELECT score.c_id,c_name,score.s_id,s_name,s_score,row_number() over( PARTITION by score.c_id ORDER BY s_score desc) 排名 from student
join score on student.s_id=score.s_id
join course on score.c_id=course.c_id
GROUP BY score.c_id,c_name,score.s_id,s_name,s_score ) t1
where 排名<=3
43、统计每门课程的学生选修人数(超过5人的课程才统计)
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id,count(*) 人数 from score
GROUP BY c_id HAVING 人数>5 ORDER BY 人数,c_id asc
44、检索至少选修两门课程的学生学号
SELECT s_id from score GROUP BY s_id HAVING count(*)>=2
45、查询选修了全部课程的学生信息
SELECT * from student where s_id in(
SELECT s_id from score
GROUP BY s_id HAVING count(*)=3)
46、查询各学生的年龄(周岁)
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select
s_id,s_name,s_birth,
if (
month(current_date()) < month(s_birth) or (month(current_date()) = month(s_birth) and day(current_date()) < day(s_birth)),
year(current_date()) - year(s_birth) -1,
year(current_date()) - year(s_birth)
)
as s_age
from student;
47、查询本周过生日的学生
select *
from student
where
datediff(concat(year(current_date()), date_format(s_birth, '-%m-%d')), current_date())
between 0 and 7
or
datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date())
between 0 and 7;
48、查询下周过生日的学生
select *
from student
where
datediff(concat(year(current_date()), date_format(s_birth, '-%m-%d')), current_date())
between 7 and 14
or
datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date())
between 7 and 14;
49、查询本月过生日的学生
select *
from student
where month(s_birth) = month(current_date());
50、查询12月份过生日的学生
select *
from student
where month(s_birth) = 12;