MYSQL 练习

数据表介绍

--1.学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--3.教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--4.成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数

select * from  Student;
select * from  Course;
select * from `Teacher`;
select * from `SC`;

# 1:查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select a.SId,  a.Cid, b.Cid, a.score, b.score from 
(select * from SC where Cid = '01') as a 
join 
(select * from SC where Cid = '02') as b 
on a.SId= b.SId
where a.score < b.score)  as c
ON C.SId = Stdent.SId

select * from `Student` where SId in ('01','05')

# 1.1. 查询同时存在" 01 "课程和" 02 "课程的情况
select * from SC ;

select * from SC left join Student on  Student.SId = SC.SId

select * FROM SC LEFT JOIN Student on Student.SId = SC.SId
where SC.CId in ('01', '02')


#1.2. 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select SC.CId, Student.Sname FROM SC left JOIN Student on Student.SId = SC.SId 
where SC.CId  in ('01','02')

#1.3. 查询不存在" 01 "课程但存在" 02 "课程的情况
select SC.CId, Student.Sname FROM SC left JOIN Student on Student.SId = SC.SId 
where SC.CId  in ('03','02')

#2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select avg(score) as avge,Sname from 
(select SC.CId, Student.Sname,SC.score FROM SC left JOIN Student on Student.SId = SC.SId ) as a
group by Sname
having avge>= 60
order by avge 


#3. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select sum(score) as sum ,Sname, count(CId) as course from 
(select SC.CId, Student.Sname,SC.score FROM SC left JOIN Student on Student.SId = SC.SId ) as a
group by Sname

#4. 查询在 SC 表存在成绩的学生信息

select Student.SId,SC.CId,Student.Sname, SC.score from SC left join Student 
on Student.SId = SC.SId

#5. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select student.SID, student.SNAME, a.total, a.count from student left join (select SId, sum(score) as total ,count(CId) as count from SC 
group by  SId) as a on student.sid = a.sid


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


#7 查询学过「张三」老师授课的同学的信息
select student.sid, student.sname, b.tname from student  right join 
(select distinct a.sid, a.tid, teacher. tname from (
select sc.sid, sc.cid, course.tid from sc left join course
on sc.cid=course.cid) as a left join teacher on a.tid = teacher.tid
where tname = '张三') as b
on student.sid = b.sid


#8 查询没有学全所有课程的同学的信息

select sname,count(cid) as count from 
(select  student.sid, student.sname, student.sage, student.ssex, student.sc.cid
from student right join sc 
on student.sid = sc.sid) as c
group by sname
having count !=3

#9 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

select student.sname, a.* from student right join 
select * from sc where cid in (select cid from sc where sid = '01')
 on student.sid = a.sid

# 10 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

select distinct c.sname, count(c.cid) as count  from 
(select  student.sname, a.* from student right join 
(select * from sc where cid in (select cid from sc where sid = '01') )as a
 on student.sid = a.sid)as c
group by c.sname
having count = 3


#11. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select student.sname from student join 
(select * from sc where cid != (select cid from course where  Tid in  (select  tid from teacher where tname like '张%')))as a
on a.sid= student.sid


# 12 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select sname, sid, avg (score), sum(score)from 
(select sc.*, student.sname from student right join sc
on student.sid = sc.sid where score < '60') as a
group by sid, sname


#13 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select a.*, student.sname from 
(select sid,score from sc where cid ='01' and score <60
order by score desc) as a
left join student 
on a.sid= student.sid


#14 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select student.sid, student.sname,  sum(sc.score) as sum, avg(sc.score) as avg
from student right join sc on student.sid = sc.sid
group by student.sid, student.sname
order by avg desc


#15 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

select case score when min(score) then 'a' else 'null' end seq, score, sid, avg (score) from 
(select sid, avg (score), sum(score) as score from sc group by sid) as a
group by score,sid


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

select student.sname, a.* from student right join
(select sum(score)as sum ,sid from sc group by sid ) as a 
on student.sid=a.sid
order by sum desc

# 17 rank 排名
set @currank :=0
select score,  @currank := @currank + 1  as a from sc order by score desc
SELECT sid,score, rank() over(partition by sid ORDER BY score )mm from sc


# 18 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select sid,
count(if( score>85, 1, null)) as "[100-85]",
count(if( score<60, 1, null)) as "[0-60]" ,
count(if( score<85 and score > 70, 1, null)) as "[85-70]",
count(if( score<70 and score > 60, 1, null)) as "[70-60]"
from sc group by sid


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

#20 查询男生、女生人数
select ssex, count(ssex) from Student group by ssex


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


#22 查询同名同性学生名单,并统计同名人数
select sname, ssex from student where sname in 
(select sname from student group by snameF
having count(sname) >= 2 and count(ssex) >=2)

#23 查询 1990 年出生的学生名单
select * from student where sage like '%1990%' ;
select * from student where year(sage) = 1990;


# 24 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select cid, avg(score) as avg from sc group by cid
order by avg desc, cid

# 25 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select Student.SId,Student.Sname, avg(SC.score) from SC left join Student 
on Student.SId = SC.SId
group by sc.sid,Student.Sname
having avg(SC.score) >= 85


# 26 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

select course.cname, a.* from course right join 
(select Student.Sname, SC.score,sc.cid from SC left join Student 
on Student.SId = SC.SId)  as a
on course.cid = a.cid
where course.cname = '数学'and score <= 60


# 27 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select Student.Sname, SC.score,sc.cid from SC right join Student 
on Student.SId = SC.SId

# 28 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select course.cname, a.* from course right join 
(select Student.Sname, SC.score,sc.cid from SC left join Student 
on Student.SId = SC.SId)  as a
on course.cid = a.cid
where score >70 

# 29 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select max(score)
from sc , student, course, teacher
where sc.sid = student.sid
and sc.cid = course.cid
and course.tid = teacher.tid
and tname = '张三'


# 30 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select *, rank () over (order by score) m  from 
(select distinct (sc.sid), score
from sc, student, course, teacher
where sc.sid = student.sid
and sc.cid = course.cid
and course.tid = teacher.tid
and tname = '张三') as c
where c.m = 1


# 31 统计每门课程的学生选修人数(超过 5 人的课程才统计)
select count(sc.cid) as count from sc left join student 
on sc.sid = student.sid
group by cid
having count > 5


#32 查询选修了全部课程的学生信息
select count(distinct(cid))as count, sid from sc 
group by sid
having count = (select count(cid) from course)


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,817评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,329评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,354评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,498评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,600评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,829评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,979评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,722评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,189评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,519评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,654评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,329评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,940评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,762评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,993评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,382评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,543评论 2 349

推荐阅读更多精彩内容