Mysql复习必备----50条经典Sql语句

关于学生,课程,成绩,教师表

student(学号#,姓名,性别,年龄)

course(课程号#,课程名,教师号#)

score(学号#,课程号#,成绩)

teacher(教师号#,教师名)


--1.查询“001”课程比“002”课程成绩高的所有学生的学号 

select a.stuNo from score a,score b 

where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score 

--2.查询平均成绩大于60分的同学的学号和平均成绩 

select stuNo,avg(score)from score 

group by stuNo 

having avg(score)>60 

--3.查询所有同学的学号、姓名、选课数、总成绩 

select a.stuNo,a.stuName,count(cNo),sum(score) from student a,score b 

where a.stuNo=b.stuNo 

group by a.stuNo,a.stuName 

--4.查询姓“赵”的老师的个数 

select count(tName),tName from teacher 

where tName like '赵%' 

group by tName 

--5.查询没学过“某某”老师课的同学的学号、姓名 

select stuNo,stuName from student 

where stuNo not in 

(select a.stuNo from student a,score b where a.stuNo=b.stuNo and cNo in 

(select d.cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保')) 

--6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 

select a.stuNo,a.stuName from student a,score b,score c 

where a.stuNo=b.stuNo and b.stuNo=c.stuNo and b.cNo='c001' and c.cNo='c002' 

--7.查询学过“某某”老师所教的所有课的同学的学号、姓名 

select stuNo,stuName from student 

where stuNo in (select stuNo from score a,course b,teacher c 

where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='钱市保' 

group by stuNo             

having count(a.cNo)>=(select count(cNo) from course d,teacher e 

where d.tNo=e.tNo and e.tName='钱市保')) 

--老师所教课程为一门课 

select stuNo,stuName from student 

where stuNo in (select a.stuNo from student a,score b where a.stuNo=b.stuNo and b.cNo in 

(select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保')) 

--8.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名 

select stuNo,stuName from student 

where stuNo in 

(select a.stuNo from score a,score b 

where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score) 

--9.查询所有课程成绩小于60分的同学的学号、姓名 

select stuNo,stuName from student 

where stuNo in (select stuNo from score 

where score<60 

group by stuNo 

having count(cNo)=(select count(cNo) from course)) 

--10.查询没有学全所有课的同学的学号、姓名 

select b.stuNo,a.stuName,count(b.cNo) from student a,score b 

where a.stuNo=b.stuNo 

group by b.stuNo,a.stuName 

having count(b.cNo)<(select count(cNo) from course) 

--11.查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名 

select distinct a.stuNo,stuName from student a,score b 

where a.stuNo=b.stuNo and cNo in (select cNo from score 

where stuNo='001') 

--12.查询至少学过学号为“001”同学一门课的其他同学学号和姓名 

&&& select distinct a.stuNo,stuName from student a,score b 

where a.stuNo=b.stuNo and cNo all join (select cNo from score 

where stuNo='001') 

--13.把“SC”表中“某某”老师教的课的成绩都更改为此课程的平均成绩 

update score set score=savg 

from score d,(select avg(score) as savg,a.cNo from score a,course b,teacher c 

where a.cNo=b.cNo and b.tNo=c.tNo and tName='钱市保' 

group by a.cNo) e 

where d.cNo=e.cNo 

--老师所教课程为一门课 

update score 

set score=(select avg(score) from score 

group by cNo 

having cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保')) 

where cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保') 

select * from score 

--14.查询和“001”号的同学学习的课程完全相同的其他同学学号和姓名 

select stuNo from score 

where cNo in (select cNo from score where stuNo='005') 

group by stuNo 

having count(cNo)=(select count(*) from score where stuNo='005') 

--15.删除学习“某某”老师课的SC表记录 

delete from score where cNo=(select cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保') 

select * from score 

--16.向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩 

--17.按平均成绩从高到低显示所有学生的“C语言”、“sql”、“java”三门的课程成绩 

--按如下形式显示: 学生ID,C语言,sql,JAVA,有效课程数,有效平均分 

--18.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 

select  cNo,max(score) as 最高分,min(score) as 最低分 from score 

group by cNo 

--19.按各科平均成绩从低到高和及格率的百分数从高到低顺序 

select avg(c.score),count(a.score)/count(b.score) from score c,(select a.cNo,count(a.score) from score a 

where a.score<60 

group by a.cNo) d,(select b.cNo,count(b.score) from score b 

group by b.cNo) e 

where d.cNo=e.cNo 

group by c.cNo 

order by avg(c.score) desc 

(select a.cNo,count(a.score) from score a 

where a.score<60 

group by a.cNo) d 

(select b.cNo,count(b.score) from score b 

group by b.cNo) e 

--20.查询如下课程平均成绩和及格率的百分数(用"1行"显示): C语言(001),数据结构(002),JAVA(003),离散数学(004) 

--21.查询不同老师所教不同课程平均分从高到低显示 

select tNo,a.cNo,avg(score) from course a,score b 

where a.cNo=b.cNo 

group by tNo,a.cNo 

order by avg(score) desc 

--22.查询如下课程成绩第 3 名到第 6 名的学生成绩单:C语言(001),数据结构(002),JAVA(003),离散数学(004) 

--  [学生ID],[学生姓名],C语言,数据结构,JAVA,离散数学,平均成绩 

--23.统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 

select distinct e.cNo,count(a.stuNo) as '100-85',count(b.stuNo) as '85-70',count(c.stuNo) as '70-60',count(d.stuNo) as '<60' from score a,score b,score c,score d,score e 

where a.cNo in (select cNo from course) and a.score between 85 and 100 and b.cNo in (select cNo from course) and b.score between 71 and 84 and c.cNo in (select cNo from course) and  c.score between 60 and 70 and d.cNo in (select cNo from course) and d.score<60 

group by e.cNo,a.stuNo,b.stuNo,c.stuNo,d.stuNo 

having a.stuNo<>b.stuNo and a.stuNo<>c.stuNo and a.stuNo<>d.stuNo and b.stuNo<>c.stuNo and b.stuNo<>d.stuNo and c.stuNo<>d.stuNo 

select cNo,count(stuNo) from score 

where score between 70 and 100 and cNo='c001' 

group by cNo 

--24.查询学生平均成绩及其名次 

select stuNo,avg(score) from score 

group by stuNo 

order by avg(score) desc 

--25.查询各科成绩前三名的记录:(不考虑成绩并列情况) 

select a.stuNo,a.cNo,a.score 

from score a 

where a.score in (select top 3 score from score b 

where a.cNo=b.cNo 

order by score) 

order by a.cNo 

--26.查询每门课程被选修的学生数 

select b.cNo ,count(stuNo) from score a right join course  b 

on a.cNo=b.cNo 

group by b.cNo 

--27.查询出只选修了一门课程的全部学生的学号和姓名 

select b.stuNo,a.stuName from student a,score b 

where a.stuNo=b.stuNo 

group by b.stuNo,a.stuName 

having count(b.cNo)=1 

--28.查询男生、女生人数 

select stuSex,count(stuSex) from student 

group by stuSex 

--29.查询姓‘zhao’的学生名单 

select * from student 

where stuName like '赵%' 


--30.查询同名同性学生名单,并统计同名人数 

select a.stuNo,a.stuName,count(a.stuNo) from student a,student b 

where a.stuName=b.stuName and a.stuSex=b.stuSex and a.stuNo<>b.stuNo 

group by a.stuNo,a.stuName 


--31.1981年出生的学生名单(注:Student表中Sage列的类型是datetime 

--32.、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 

select cNo,avg(score) from score 

group by cNo 

order by avg(score) asc,cNo 

--33.查询平均成绩大于70的所有学生的学号、姓名和平均成绩 

select b.stuNo,a.stuName,avg(score) from student a,score b 

where a.stuNo=b.stuNo 

group by b.stuNo,a.stuName 

having avg(score)>70 

--34.查询课程名称为“java”,且分数低于70的学生姓名和分数 

select a.stuName,b.score from student a,score b 

where a.stuNo=b.stuNo and score<70 and b.cNo=(select cNo from course where cName='java') 

--35.查询所有学生的选课情况 

select a.stuNo,c.cNo from student a,score b,course c 

where a.stuNo=b.stuNo and b.cNo=c.cNo 

order by a.stuNo 

select a.stuNo,cNo from student a left join (select a.stuNo,c.cNo from student a,score b,course c 

where a.stuNo=b.stuNo and b.cNo=c.cNo) d 

on a.stuNo=d.stuNo 

order by a.stuNo 

--36.查询任何一门课程成绩在70分以上的姓名、课程名称和分数 

select a.stuName,b.cNo,score from student a,score b 

where score>70 and a.stuNo=b.stuNo 

--37.查询不及格的课程,并按课程号从大到小排列 

select cNo,score from score 

where score<60 

order by cNo 

--38.查询课程编号为003且课程成绩在60分以上的学生的学号和姓名 

select b.stuNo,a.stuName from student a,score b 

where b.cNo='c003' and score>60 and a.stuNo=b.stuNo 

--39.求选了课程的学生人数 

select count(a.stuNo) from (select distinct stuNo from score) a 

--40.查询选修“赵”老师所授课程的学生中,成绩最高的学生姓名及其成绩 

select b.stuNo,a.stuName,max(score) from student a,score b 

where a.stuNo=b.stuNo and b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保') 

group by b.stuNo,a.stuName,b.cNo 

having b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保') 

--41.查询各个课程及相应的选修人数 

select cNo,count(stuNo) from score 

group by cNo 

select b.cNo ,count(stuNo) from score a right join course  b 

on a.cNo=b.cNo 

group by b.cNo 

--42.查询不同课程成绩相同的学生的学号、课程号、学生成绩 

select a.stuNo,a.cNo,a.score from score a,score b 

where a.stuNo=b.stuNo and a.score=b.score and a.cNo<>b.cNo 

--43. 查询每门功成绩最好的前两名 

select a.stuNo,a.cNo,a.score 

from score a 

where score in(select top 2 score from score b 

where a.cNo=b.cNo 

order by score desc) 

order by a.cNo 

--44.统计每门课程的学生选修人数(超过2人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 

select cNo,count(stuNo) 课程数 from score 

group by cNo 

having count(stuNo)>2 

order by count(stuNo) desc,cNo 

--45.检索至少选修两门课程的学生学号 

select stuNo from score 

group by stuNo 

having count(cNo)>=2 

--46.查询全部学生都选修的课程的课程号和课程名 

select a.cNo,b.cName from score a,course b 

where a.cNo=b.cNo 

group by a.cNo,b.cName 

having count(a.stuNo)=(select count(stuNo) from student) 

select a.cNo,b.cName from score a,course b 

group by a.cNo,b.cName,b.cNo 

having a.cNo=b.cNo and count(a.stuNo)=(select count(stuNo) from student) 

--47.查询没学过“钱”老师讲授的任一门课程的学生姓名 

select stuNo,stuName from student 

where stuNo not in (select stuNo from score a,course b,teacher c 

where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='钱市保' 

group by stuNo 

having count(a.cNo)<=(select count(cNo) from course d,teacher e 

where d.tNo=e.tNo and e.tName='钱市保')) 

select stuNo,stuName from student 

where stuNo not in 

(select stuNo from score where cNo in 

(select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保')) 

--48.查询两门以上不及格课程的同学的学号及其平均成绩 

select stuNo,avg(score) from score 

where score<60 

group by stuNo 

having count(cNo)>2 

--49.检索“004”课程分数小于60,按分数降序排列的同学学号 

select stuNo from score 

where score<60 and cNo='c004' 

order by score desc 

--50.删除“2”同学的“001”课程的成绩 

delete from score where stuNo='002' and cNo='c001'

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

推荐阅读更多精彩内容