-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT a.*,b.s_score as '01-课程',c.s_score as '02-课程'
FROM student a
JOIN score b on a.s_id = b.s_id AND b.c_id = '01'
LEFT JOIN score c on a.s_id = c.s_id AND c.c_id='02' OR c.c_id=NULL
WHERE b.s_score > c.s_score
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT a.*,b.s_score as '01',c.s_score AS '02'
FROM student a
JOIN score b on a.s_id = b.s_id AND b.c_id = '01'
left JOIN score c on a.s_id = c.s_id AND c.c_id = '02'
where b.s_score<c.s_score
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT a.s_id AS '学号',a.s_name AS '名字',ROUND(AVG(b.s_score),2) AS '大于60平均成绩'
FROM student a
JOIN score b on a.s_id = b.s_id
GROUP BY a.s_name,a.s_id
HAVING ROUND(AVG(b.s_score),2) >= 60
ORDER BY a.s_id desc
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的)
SELECT a.s_id AS '学号',a.s_name AS '名字',ROUND(AVG(b.s_score),2) AS '小于等于60平均成绩'
FROM student a
JOIN score b on a.s_id = b.s_id
GROUP BY a.s_name,a.s_id
HAVING ROUND(AVG(b.s_score),2) <= 60 UNION
SELECT b.s_id AS '学号',b.s_name AS '名字',0 AS '小于等于60平均成绩'
FROM student b
WHERE b.s_id not in ( -- 获取没有成绩的人,并且将对应的
SELECT distinct s_id
FROM score
)
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s_id AS '学生编号',a.s_name AS "学生姓名",COUNT(b.c_id) AS '选课总数',SUM(b.s_score) AS '总成绩'
FROM student a
JOIN score b on a.s_id = b.s_id
GROUP BY a.s_id,a.s_name
-- 6、查询"李"姓老师的数量
SELECT COUNT(t_id)
FROM teacher
WHERE t_name LIKE '李%'
-- 7、查询学过"张三"老师授课的同学的信息
SELECT a.*
FROM student a
JOIN score b on a.s_id = b.s_id
WHERE b.c_id in (
SELECT c_id
FROM course
WHERE t_id =(
SELECT t_id
FROM teacher
WHERE t_name = '张三'
))
-- 8、查询没学过"张三"老师授课的同学的信息
SELECT *
FROM student s
WHERE s.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 c_id
FROM course
WHERE t_id = (
SELECT t_id
FROM teacher
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'
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
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、查询没有学全所有课程的同学的信息
select s.*
from student s
where s.s_id in(
select s_id from score where s_id not in(
select a.s_id
from score a
join score b on a.s_id = b.s_id and b.c_id='02'
join score c on a.s_id = c.s_id and c.c_id='03'
where a.c_id='01'))
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT s.*
FROM student s
WHERE s.s_id in(
SELECT DISTINCT s_id
FROM score
WHERE c_id in (
SELECT a.c_id
FROM score a
WHERE a.s_id = 01
)
)
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select a.*
from student a
where a.s_id in(
select distinct s_id
from score
where s_id!='01' and c_id in(
select c_id
from score
where s_id='01'
)
group by s_id
having count(1)=(select count(1) from score where s_id='01'));
-- 使用集合函数GROUP_CONCA
SELECT a.*
FROM student a
LEFT JOIN score sc ON a.s_id = sc.s_id
GROUP BY a.s_id
HAVING GROUP_CONCAT(sc.c_id) = (
SELECT GROUP_CONCAT(c.c_id)
FROM student b
LEFT JOIN score c ON c.s_id = b.s_id
WHERE b.s_id = '01'
) AND a.s_id != '01'
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT *
FROM student a
WHERE a.s_id NOT IN (
SELECT b.s_id
FROM score b
WHERE b.c_id = (
SELECT c.c_id
FROM course c
WHERE c.t_id in (
SELECT t_id
FROM teacher
WHERE t_name = '张三'
)
))
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT a.s_id,a.s_name,ROUND(AVG(b.s_score),2)
FROM student a
LEFT JOIN score b on a.s_id = b.s_id
WHERE a.s_id in (
SELECT s_id
FROM score
WHERE s_score <60
GROUP BY s_id
HAVING COUNT(1) >=2
)
GROUP BY a.s_id,a.s_name
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT a.*,b.c_id,b.s_score
FROM student a
INNER JOIN score b ON a.s_id = b.s_id AND b.s_score<60 AND b.c_id = 01
ORDER BY b.s_score DESC
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT a.s_id,(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id = 01) AS 语文,(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id = 02) AS 数学,(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id = 03) AS 英语,ROUND(AVG(s_score),2) AS '平均分'
FROM score a
GROUP BY a.s_id
ORDER BY 平均分 DESC
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name
SELECT a.c_id AS'课程ID',b.c_name as '课程name',MAX(s_score) AS '最高分',MIN(s_score) AS '最低分',ROUND(AVG(s_score),2) AS '平均分',
ROUND(100*(SUM(CASE WHEN a.s_score >=60 THEN 1 ELSE 0 END)/SUM(case when a.s_score THEN 1 ELSE 0 END)),2) AS '及格率',
ROUND(100*(SUM(CASE WHEN a.s_score >=70 and a.s_score <80 THEN 1 ELSE 0 END)/SUM(case when a.s_score THEN 1 ELSE 0 END)),2) AS '中等率',
ROUND(100*(SUM(CASE WHEN a.s_score >=80 AND a.s_score <90 THEN 1 ELSE 0 END)/SUM(case when a.s_score THEN 1 ELSE 0 END)),2) AS '优良率',
ROUND(100*(SUM(CASE WHEN a.s_score >=90 THEN 1 ELSE 0 END)/SUM(case when a.s_score THEN 1 ELSE 0 END)),2) AS '优秀率'
FROM score a
LEFT JOIN course b on a.c_id =b.c_id
GROUP BY a.c_id,b.c_name
-- 19、按各科成绩进行排序,并显示排名
-- #1
SELECT a.c_id AS '课程ID',a.s_id AS '学生ID',a.s_score AS '学生成绩', COUNT(b.s_score)+1 AS 'rank等级'
FROM score a
left JOIN score b on a.c_id = b.c_id AND (b.s_score > a.s_score or (b.s_score = a.s_score AND a.s_id >b.s_id))
-- 碰到分数相同,根据关联关系,or会新增n条数据,所以count计数的时候不会重复,如果分数相同那么学生ID号大的排前边,这种排序不一定符合要求
GROUP BY a.c_id,a.s_id,a.s_score
ORDER BY a.c_id,rank等级 ASC
-- #2 通过变量来实现 ???
SELECT a.s_id,a.c_id, -- ③
@i:=@i+1 as '去重排名', -- 循环自增1
@k:=(CASE WHEN @score = a.s_score THEN @k ELSE @i END) AS '不去重排名', -- 遇到成绩相同的保存对应的
@score:=a.s_score as score
FROM (SELECT s_id,c_id,s_score -- ①返回所需列名 表别名为
FROM score
GROUP BY s_id,c_id,s_score
ORDER BY s_score DESC
) a,
( -- ②初始化 i,k,score 变量 表别名为s
SELECT @k:=0, @i:=0, @score:=0
) s
-- 执行顺序 ① -> ② -> ③
-- #3 union 来实现
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank
FROM score t1 where t1.c_id='01'
order by t1.s_score desc) t1)
union
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank
FROM score t1 where t1.c_id='02'
order by t1.s_score desc) t2)
union
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank
FROM score t1 where t1.c_id='03'
order by t1.s_score desc) t3)
-- 20、查询学生的总成绩并进行排名
SELECT a.s_id,
@i:=@i+1 AS 不去重rank,
@k:=(case WHEN @score=a.sum_score THEN @k ELSE @i END) AS 去重rank,
@score:=a.sum_score as score
FROM (SELECT s.s_id,SUM(s.s_score) as sum_score
FROM score s
GROUP BY s.s_id
ORDER BY SUM(s.s_score) DESC) a,(SELECT @i:=0,@k:=0,@score:=0) b
-- 21、查询不同老师所教不同课程平均分从高到低显示
SELECT a.c_id AS '课程ID',a.t_id AS '老师ID',c.t_name AS '老师名称',ROUND(AVG(b.s_score),2) as avgscore
FROM course a
LEFT JOIN score b on a.c_id = b.c_id
LEFT JOIN teacher c on a.t_id = c.t_id
GROUP BY a.c_id,a.t_id,c.t_name
ORDER BY avgscore DESC
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT d.*,c.排名,c.s_score,c.c_id
FROM (
SELECT a.s_id,
a.s_score,
a.c_id,
@i:=@i+1 AS 排名
FROM score a,(SELECT @i:=0)s
WHERE a.s_id = '01'
ORDER BY a.s_score DESC
) c
LEFT JOIN student d on d.s_id=c.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT d.*,c.排名,c.s_score,c.c_id
FROM (
SELECT a.s_id,
a.s_score,
a.c_id,
@k:=@k+1 AS 排名
FROM score a,(SELECT @k:=0)s
WHERE a.s_id = '02'
ORDER BY a.s_score DESC
) c
LEFT JOIN student d on d.s_id=c.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT d.*,c.排名,c.s_score,c.c_id
FROM (
SELECT a.s_id,
a.s_score,
a.c_id,
@j:=@j+1 AS 排名
FROM score a,(SELECT @j:=0)s
WHERE a.s_id = '03'
ORDER BY a.s_score DESC
) c
LEFT JOIN student d on d.s_id=c.s_id
WHERE 排名 BETWEEN 2 AND 3
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比
from score a
left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`,
ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)b on a.c_id=b.c_id
left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`,
ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)c on a.c_id=c.c_id
left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`,
ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)d on a.c_id=d.c_id
left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`,
ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)e on a.c_id=e.c_id
left join course f on a.c_id = f.c_id
-- 24、查询学生平均成绩及其名次
SELECT AS ' 学生ID',
@i:=@i+1 AS '实际排名',
@k:=(case when @score = a.avg1 then @k ELSE @i END) AS '重复排名',
@score:=a.avg1 AS '平均分'
FROM (
SELECT s_id as sid,AVG(s_score) AS avg1
FROM score
GROUP BY sid
ORDER BY avg1 DESC
) a,(SELECT @i:=0,@k:=0,@score:=0) s
-- 25、查询各科成绩前三名的记录
-- #1 笨方法,分别排序最后结合在一起
(SELECT *
FROM score
WHERE c_id = '01'
ORDER BY s_score DESC
LIMIT 0,3)
UNION
(SELECT *
FROM score
WHERE c_id = '02'
ORDER BY s_score DESC
LIMIT 0,3)
UNION
(SELECT *
FROM score
WHERE c_id = '03'
ORDER BY s_score DESC
LIMIT 0,3)
-- 2# 常规
select a.s_id,a.c_id,a.s_score
from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC
-- 26、查询每门课程被选修的学生数
SELECT c_id,COUNT(s_id)
FROM score
GROUP BY c_id
-- 27、查询出只有两门课程的全部学生的学号和姓名
SELECT *
FROM student a
WHERE a.s_id in (
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(c_id) = 2)
-- 28、查询男生、女生人数
-- #1
(SELECT '男生' AS '性别',COUNT(s_id) AS '人数'
FROM student
WHERE s_sex = '男'
) UNION
(SELECT '女生' AS '性别',COUNT(s_id) AS '人数'
FROM student
WHERE s_sex = '女'
)
-- #2
SELECT s_sex,COUNT(s_sex)
FROM student
GROUP BY s_sex
-- 29、查询名字中含有"风"字的学生信息
SELECT *
FROM student
WHERE s_name LIKE '%风%'
-- 30、查询同名同性学生名单,并统计同名人数 7郑竹 8王菊
SELECT b.s_name,COUNT(b.s_name)
FROM student a
LEFT JOIN student b on a.s_id = b.s_id AND a.s_name = b.s_name
GROUP BY b.s_name,b.s_sex
HAVING COUNT(b.s_name) >1
select a.s_name,a.s_sex,count(*) from student a JOIN
student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
GROUP BY a.s_name,a.s_sex
-- 31、查询1990年出生的学生名单
select s_name from student where s_birth like '1990%'
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c_id,ROUND(AVG(s_score),2) AS avg_score
FROM score
GROUP BY c_id
ORDER BY avg_score DESC,c_id ASC
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT b.s_id as '学号',b.s_name as '名称',ROUND(AVG(a.s_score),2) AS avg_score_85
FROM score a
LEFT JOIN student b on a.s_id = b.s_id
GROUP BY a.s_id
HAVING avg_score_85 >=85
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT *
FROM score a
JOIN student b on a.s_id = b.s_id and a.c_id = (
SELECT c_id course
FROM course
WHERE c_name = "数学"
)
WHERE a.s_score <=60
-- 35、查询所有学生的课程及分数情况;
SELECT a.s_name as '学生名称',
SUM(case WHEN c.c_name = '语文' THEN b.s_score ELSE 0 END) AS '语文',
SUM(case WHEN c.c_name = '数学' THEN b.s_score ELSE 0 END) AS '数学',
SUM(case WHEN c.c_name = '英语' THEN b.s_score ELSE 0 END) AS '英语',
SUM(b.s_score)
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 a.s_name as '姓名',c.c_name as '课程名字',b.s_score 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
WHERE b.s_score > 70
-- 37、查询不及格的课程
SELECT b.c_name as '课程名称',a.s_score AS '分数'
FROM score a
LEFT JOIN course b on a.c_id = b.c_id
WHERE a.s_score <60
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT b.s_id as '学号',a.s_name as '姓名'
FROM student a
LEFT JOIN score b on a.s_id = b.s_id
WHERE b.s_score >80 AND b.c_id = 01
-- 39、求每门课程的学生人数
SELECT COUNT(*) as '课程人数'
FROM score
GROUP BY c_id
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
-- #1
SELECT *
FROM (
SELECT s_id,s_score,c_id
FROM score
WHERE c_id in (
SELECT c_id
FROM course
WHERE t_id =
(SELECT t_id
FROM teacher
WHERE t_name = '张三'
)
)
) as a
JOIN student b on a.s_id = b.s_id
ORDER BY a.s_score DESC
LIMIT 1
-- #2
select a.*,b.s_score,b.c_id,c.c_name 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
where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三')
and b.s_score in (select MAX(s_score) from score where c_id='02')