sql mark

# 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select  student.*,a.score'C01',b.score'C02'
       from (select score ,SId from sc where sc.CId='01') as a,(select score ,SId from sc where sc.CId='02') as b,student
where a.sid=b.sid and a.sid=student.SId and a.score>b.score;

# 查询同时存在" 01 "课程和" 02 "课程的情况
select * from student
where SId in (select a.sid from (select SId from sc where sc.CId='01') as a,(select SId from sc where sc.CId='02') as b
where a.sid=b.sid);

select a.sid,a.score,b.score
from (select sid,score from sc where sc.cid='01')as a
inner join (select sid,score from sc where sc.cid='02')as b
on a.sid=b.sid;

# 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) 以a表为主表
select a.sid,a.score,b.score
from (select sid,score from sc where sc.cid='01')as a
left join (select sid,score from sc where sc.cid='02')as b
on a.sid=b.sid;

# 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from sc
where sc.cid not in(select cid from sc where cid='01')and sc.cid='02';

# 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select sid,avg(score) from sc group by sid;
#GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。group by后面不能接where,having代替了where
select sid,avg(score) from sc group by sid having avg(score)>60;

select b.sid,b.Sname,a.*
from student b,(select sid,avg(score) from sc group by sid having avg(score)>60)as a
where b.SId=a.SId;


select A.Sid,B.Sname,A.dc from(select Sid,AVG(score)dc from SC group by Sid)A
left join Student B on A.Sid=B.Sid where A.dc>=60;

select
s.SId,
st.Sname,
       avg(s.score) as score
from sc s
left join student st
    on s.SId = st.SId
group by s.SId,
st.Sname
having avg(s.score)>=60;

# 查询在 SC 表存在成绩的学生信息
select sc.*,student.*
from sc left join student on sc.SId=student.SId;
#不需要成绩且结果重复

select distinct student.* from student,sc where student.SId=sc.SId;


# 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select student.SId,student.Sname,count(sc.CId),sum(sc.score)
from student left join  sc
on student.sid=sc.sid;
# 结果为啥只有一条数据?

select student.SId,student.Sname,count(sc.CId),sum(sc.score)
from student left join  sc
on student.sid=sc.sid
group by student.sid,student.sname;

# 查有成绩的学生信息
select student.* ,sc.score
from student inner join sc
on student.sid=sc.SId;

# 查询「李」姓老师的数量
select count(teacher.TId)
from teacher
where teacher.Tname like '李%';

# 查询学过「张三」老师授课的同学的信息
select student.*
from student,sc,course,teacher
where teacher.Tname='张三' and teacher.TId=course.TId and course.CId=sc.CId and sc.SId=student.SId;

# 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 (没写出来~~~)
select * from sc group by SId having score<60;
# 不及格的

SELECT student.sid,student.sname,avg(sc.score)
from student INNER  join sc on student.sid = sc.sid
where sc.sid in(SELECT sc.sid from sc  where sc.score<60 group by sc.sid having count(1)>=2) group by sc.sid;

# 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select student.*
from student ,sc
where  sc.cid='01' and sc.score<60 and sc.SId=student.SId order by score desc;

select student.*,sc.score from student
    inner join SC  using(sid)
    where sc.cid='01' and sc.score<60
    order by score desc;

# 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc.*,a.avgscore,student.sname
from sc left join (select sc.sid,avg(sc.score)as avgscore from sc group by sc.sid)as a on sc.sid=a.sid
left join student on sc.SId=student.SId
order by a.avgscore desc;
# 一个人多行

select Sid,max(case Cid when '01' then score else 0 end)'01',
max(case Cid when '02' then score else 0 end)'02',
MAX(case Cid when '03' then score else 0 end)'03',AVG(score)平均分 from SC
group by Sid order by 平均分 desc;
# 别人的
# case when

# 查询各科成绩最高分、最低分和平均分:
select cid,max(score),min(score),avg(score)
from sc
group by CId;

# 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
# 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
# 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select
s.CId,
c.Cname,
max(s.score) as '最高分',
min(s.score) as '最低分',
       avg(s.score) as '平均分',
       concat(round(sum(case when s.score >=60 then 1 else 0 end)*100/count(1),2) ,'%') as '及格率',
       sum(case when s.score >=70 and s.score < 80 then 1 else 0 end)*1.00/count(1) as '中等率',
       sum(case when s.score >=80 and s.score < 90 then 1 else 0 end)*1.00/count(1) as '优良率',
       sum(case when s.score >=90 then 1 else 0 end)*1.00/count(1) as '优秀率'
from sc s
left join course c
    on s.CId = c.CId
group by s.CId,
c.Cname
order by count(*) desc ,s.cid asc;

# 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
select *,rank() over (order by score desc ) from sc
order by score desc;

# 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
select SId,sum(score) as sumscore,rank() over (order by sumscore desc)
from sc group by sc.sid order by sumscore desc;

# 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

# 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select course.cid,course.cname,
       sum((case when sc.score>=85 and sc.score <100 then 1 else 0 end)) as '[100-85]',
       concat(round( sum(case when sc.score>=85 and sc.score <100 then 1 else 0 end)*100/count(1),2) ,'%') as '[100-85]%',
       sum((case when sc.score>=70 and sc.score <85 then 1 else 0 end)) as '[85-70]',
       concat(sum(case when sc.score>=70 and sc.score <85 then 1 else 0 end)/count(1)*100,'%')as '[85-70]%',
       sum((case when sc.score>=60 and sc.score <70 then 1 else 0 end)) as '[70-60]',
       concat(round(sum (case when sc.score>=60 and sc.score<70 then 1 else 0 end )*100/count(1),2),'%')as '[70-60]%',
       sum((case when sc.score>=0 and sc.score <60 then 1 else 0 end)) as '[60-0]',
       concat(round(sum (case when sc.score>=0 and sc.score<60 then 1 else 0 end )*100/count(1),2),'%')as '[0-60]%'
from course
left join sc
on course.CId=sc.CId
group by sc.CId;


# 查询各科成绩前三名的记录
select * from (select *,rank()over (partition by  CID order by score desc )排名 from sc) a
where a.排名<=3;

# 查询每门课程被选修的学生数
select cid,count(sc.SId)
from sc
group by CId;

# 查询出只选修两门课程的学生学号和姓名
select sc.sid
from sc
where  sid in (select * from sc group by sid having count(sc.cid)=2 );

# 查询男生、女生人数
select  count(student.Ssex)
from student
group by SId;

# 查询名字中含有「风」字的学生信息
select  * from student
where Sname like '%风%';

# 查询同名同性学生名单,并统计同名人数
select * from student
left join (select sname,ssex,count(*)人数 from student group by sname,ssex) A
on student.sname=a.Sname and student.ssex=a.Ssex;

# 查询 1990 年出生的学生名单
select * from student
where year(Sage)=1990;

# 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select avg(score) from sc
group by CId
order by avg(score) desc,cid asc;

# 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select student.sid,student.sname,a.平均分
from student,
(select sid,avg(score) 平均分 from sc group by sid having 平均分>=85) a
where  student.sid=a.SId

# 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select student.sname,a.cname,a.score
from student
left join (select sc.cid,sc.sid,course.cname,sc.score from sc left join course on sc.cid=course.cid having sc.score>70)a
on student.sid=a.sid
# 有null值
SELECT a.sname,d.cname,d.score FROM
(SELECT b.*,c.cname from (SELECT * from sc where score>70) b LEFT JOIN course C
on b.cid=c.cid) d
LEFT JOIN student a on d.sid=a.sid

# 查询不及格的课程
select distinct CId
from sc
where sc.score<60

# 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select student.sid,student.Sname
from student
left join (select cid ,sid from sc where cid=01 and score>80  )a
on student.sid=a.sid;

# 求每门课程的学生人数
select cid,count(sid)
from sc
group by CId;

# 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select student.*,sc.score
from student,sc,teacher,course
where student.SId= sc.SId and sc.CId=course.CId and course.TId=teacher.TId  and teacher.Tname='张三'
order by sc.score desc limit 1

# 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩


# 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select * from sc a
where exists(select * from sc b where a.sid=b.sid and a.cid!= b.cid and a.score=b.score);

# 查询每门功成绩最好的前两名

# 统计每门课程的学生选修人数(超过 5 人的课程才统计)
select cid,count(sid)
from sc
group by CId
having count(sid)>5;

# 检索至少选修两门课程的学生学号
select sid,count(cid)
from sc
group by sId
having  count(cid)>2

# 查询选修了全部课程的学生信息
select * from student s
where s.sid in (select sid from sc group by sid having count(cid)>=3)

# 查询各学生的年龄,只按年份来算
select sname,timestampdiff(year,sage,now()) 年龄 from student;

# 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

# 查询本周过生日的学生
select * from student where week(sage)=week(now());

# 查询下周过生日的学生
select * from student where week(Sage)=week(now())+1;

# 查询本月过生日的学生
select * from student where month(sage)=month(now());

# 查询下月过生日的学生
select * from student where month(Sage)=month(now())+1;


select * from student2

update student2 set Ssex='M' where SSEX='男'








猜你喜欢

转载自blog.csdn.net/yangBiBiya/article/details/93736738