《2022年2022年经典SQL查询语句条 .pdf》由会员分享,可在线阅读,更多相关《2022年2022年经典SQL查询语句条 .pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、create table student ( sid varchar(50) primary key , sname varchar(20) , sage int , ssex varchar(5) ) create table teacher ( tid varchar(50) primary key , tname varchar(20) ) create table course( cid varchar(50) primary key , cname varchar(30), tid varchar(50) , ) alter table course add constraint f
2、k_1 foreign key (tid) references teacher(tid); create table sc( sid varchar(50) not null, cid varchar(50) not null, score int not null ) alter table sc add constraint fk_2 foreign key (sid) references student(sid); alter table sc add constraint fk_3 foreign key (cid) references course(cid); insert i
3、nto teacher (tid,tname) values (001,teacher1) insert into teacher (tid,tname) values (002,teacher2) insert into teacher (tid,tname) values (003,teacher3) insert into teacher (tid,tname) values (004,teacher4) insert into teacher (tid,tname) values (005,teacher5) insert into student values (001,studen
4、t1,20,man) insert into student values (002,student2,16,woman) insert into student values (003,student3,30,man) insert into student values (004,student4,9,woman) insert into student values (005,student5,25,man) insert into course values (001,course1,001) insert into course values (002,course2,002) in
5、sert into course values (003,course3,003) insert into course values (004,course4,004) insert into course values (005,course5,005) insert into sc values (001,001,80) insert into sc values (001,002,60) insert into sc values (001,003,40) insert into sc values (001,004,100) 名师资料总结 - - -精品资料欢迎下载 - - - -
6、- - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - insert into sc values (001,005,90) insert into sc values (002,001,40) insert into sc values (002,002,60) insert into sc values (002,003,70) insert into sc values (002,004,100) insert into sc values (002,005,90) insert int
7、o sc values (003,001,90) insert into sc values (003,002,60) insert into sc values (003,003,70) insert into sc values (003,004,60) insert into sc values (003,005,40)-1、查询“ 001”课程比“ 002”课程成绩高的所有学生的学号;select a.sid from (select sid ,score from sc where cid=001) a, (select sid,score from sc where cid=002
8、) b where a.scoreb.score and a.sid=b.sid -2 、查询平均成绩大于60 分的同学的学号和平均成绩;select sid ,avg(score) from sc group by sid having avg(score)60 -3 、查询所有同学的学号、姓名、选课数、总成绩1)select a.sid ,a.sname ,(select count(*) from sc as b where a.sid=b.sid) 选课数 ,(select sum(score) from sc as b where a.sid=b.sid) 总成绩 from stud
9、ent as a 2)select a.sid ,a.sname , count(b.cid) 选课数,sum(b.score) 总成绩 from student a left outer join sc b on a.sid=b.sid group by a.sid,a.sname -4 、查询姓“李”的老师的个数select count(*) from teacher where tname like 李% -5 、查询没学过“ teacher1 ”老师课的同学的学号、姓名select sid ,sname from student where sid not in (select sid
10、 from teacher t,course c,sc s where s.cid=c.cid and t.tid=c.tid and t.tname=teacher1) -6 、查询学过“ 001”并且也学过编号“ 002”课程的同学的学号、姓名;- 解法 1. select sid ,sname from student where sid in (select sid from sc where cid=001) and sid in (select sid from sc where cid=002) - 解法 2. select s.sid ,s.sname from student
11、 s,sc where s.sid=sc.sid and cid=001 and exists( select * from sc sc1 where sc1.sid=sc.sid and sc1.cid=002) -7 、查询学过“ teacher1 ”老师所教的所有课的同学的学号、姓名;select sid,sname from student where sid in(select sc.sid from sc,course c,teacher t where t.tname=teacher1 and t.tid=c.tid and c.cid = sc.cid group by sid
12、 having count(sid)=(select count(cid) from course c,teacher t where t.tname=teacher1 and t.tid=c.tid ) -8 、查询课程编号“ 002”的成绩比课程编号“ 001”课程低的所有同学的学号、姓名;select sid,sname from(select s.sid ,sname ,score score1, (select score from sc where sc.sid=s.sid and sc.cid=002)score2 from student s,sc where s.sid=sc
13、.sid and sc.cid=001) a where a.score1a.score2 -9 、查询所有课程成绩小于60 分的同学的学号、姓名;select sid ,sname from student where sid not in(select distinct(sid) from sc where score60) -10 、查询没有学全所有课的同学的学号、姓名( 包括一门课程都没学的同学);select s.sid ,sname from student s left outer join sc on sc.sid=s.sid group by s.sid ,sname hav
14、ing count(cid)=60 then 1 else 0 end)/count(*) 及格率 from sc,course c where sc.cid=c.cid group by sc.cid ,cname order by 及格率 desc; -20 、查询如下课程平均成绩和及格率的百分数(用1 行显示): 企业管理( 001),马克思( 002),OO&UML (003),数据库( 004)select sum(case when cid=001 then score else 0 end)/sum(case when cid=001 then 1 else 0 end) 企业管
15、理平均分 , 100*sum(case when cid=001 and isnull(score,0)=60 then 1 else 0 end)/sum(case when cid=001 then 1 else 0 end) 企业管理及格率 , sum(case when cid=002 then score else 0 end)/sum(case when cid=002 then 1 else 0 end) 马克思平均分, 100*sum(case when cid=002 and isnull(score,0)=60 then 1 else 0 end)/sum(case whe
16、n cid=002 then 1 else 0 end) 马克思及格率 , sum(case when cid=003 then score else 0 end)/sum(case when cid=003 then 1 else 0 end) OO&UML平均分, 100*sum(case when cid=003 and isnull(score,0)=60 then 1 else 0 end)/sum(case when cid=003 then 1 else 0 end) OO&UML及格率 , sum(case when cid=004 then score else 0 end)
17、/sum(case when cid=004 then 1 else 0 end) 数据库平均分, 100*sum(case when cid=004 and isnull(score,0)=60 then 1 else 0 end)/sum(case when cid=004 then 1 else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - 0 end) 数据库及格率 from sc -21 、查询不同老师所教不同课程平
18、均分从高到低显示( 注:一门课程只能有一位老师教) select c.cid,tid, avg(score) 平均分 from sc,course c where sc.cid=c.cid group by c.cid,tid order by 平均分 -22 、查询如下课程成绩第 3 名到第 5 名的学生成绩单:企业管理(001),马克思( 002),UML (003),数据库(004)- 学生 ID,学生姓名, 企业管理 , 马克思,UML,数据库, 总分select distinct top 3 with ties sc.sid ,s.sname,s1.score 企业管理 ,s2.sc
19、ore 马克思,s3.score UML,s4.score 数据库 , isnull(s1.score,0)+isnull(s2.score,0)+isnull(s3.score,0)+isnull(s4.score,0) as 总分 from student s,sc left join sc s1 on sc.sid=s1.sid and s1.cid=001 left join sc s2 on sc.sid=s2.sid and s2.cid=002 left join sc s3 on sc.sid=s3.sid and s3.cid=003 left join sc s4 on s
20、c.sid=s4.sid and s4.cid=004 where s.sid=sc.sid and isnull(s1.score,0)+isnull(s2.score,0)+isnull(s3.score,0)+isnull(s4.score,0) not in ( select distinct top 3 with ties isnull(s1.score,0)+isnull(s2.score,0)+isnull(s3.score,0)+isnull(s4.score,0) 总分 from sc left join sc s1 on sc.sid=s1.sid and s1.cid=0
21、01 left join sc s2 on sc.sid=s2.sid and s2.cid=002 left join sc s3 on sc.sid=s3.sid and s3.cid=003 left join sc s4 on sc.sid=s4.sid and s4.cid=004 order by 总分 desc ) order by 总分 desc -23 、统计列印各科成绩 ,各分数段人数 : 课程 ID, 课程名称 ,100-85,85-70,70-60, =85 then 1 else 0 end) 100-85, sum(case when isnull(score,0)
22、=70 and isnull(score,0)=60 and isnull(score,0)70 then 1 else 0 end) 70-60, sum(case when isnull(score,0)60 then 1 else 0 end) t2. 平均成绩 ) 名次, sid, 平均成绩from (select sid,avg(score) 平均成绩 from sc group by sid) t2 order by 名次 -25 、查询各科成绩前三名的记录:( 不考虑成绩并列情况 ) select sid,cid,score from sc s where score in(se
23、lect top 3 score from sc where s.cid=sc.cid order by score desc) order by cid -26 、查询每门课程被选修的学生数select cid,count(sid) from sc group by cid -27 、查询出只选修了一门课程的全部学生的学号和姓名select s.sid ,sname from sc,student s where sc.sid=s.sid group by s.sid,sname having count(sc.cid)=1 -28 、查询男生、女生人数select count(sid) 女
24、生人数 , (select count(sid) from student where ssex=man group by ssex) 男生人数 from student where ssex=woman group by ssex; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - -29 、查询姓“张”的学生名单select * from student where sname like 张% -30 、查询同名同性学生名单,
25、并统计同名人数select sname,count(*) 同名人数 from student group by sname having count(sname)1 -31 、1981 年出生的学生名单 (注:Student 表中 Sage列的类型是 datetime) select * from student where CONVERT(char(11),DATEPART(year,Sage)=1981; -32 、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列select cid, avg(score) from sc group by cid orde
26、r by avg(score) asc ,cid desc -33 、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩select s.sid ,sname ,avg(score) from student s,sc where s.sid=sc.sid group by s.sid,sname having avg(score)85 -34 、查询课程名称为course1,且分数低于 60 的学生姓名和分数select s.sid,sname,cname,score from course c,sc,student s where c.cid=sc.cid and sc.sid=s.s
27、id and isnull(score,0)70 -37 、查询不及格的课程,并按课程号从大到小排列select * from sc where isnull(score,0)80 -39 、求选了课程的学生人数select count(cid) from course -40 、查询选修“ teacher1 ”老师所授课程的学生中,成绩最高的学生姓名及其成绩select sname,score from student s,sc,teacher t,course c where s.sid=sc.sid and sc.cid=c.cid and c.tid=t.tid and t.tname
28、=teacher1 and score=(select max(s1.score) from sc s1 where sc.cid=s1.cid) -41 、查询各个课程及相应的选修人数select cid ,count(sid) from sc group by cid -42 、查询不同课程成绩相同的学生的学号、课程号、学生成绩select sc1.sid ,sc1.cid,sc1.score from sc sc1,sc sc2 where sc1.score=sc2.score and sc1.cidsc2.cid -43 、查询每门功成绩最好的前两名select * from sc
29、s1 where s1.score in (select top 2 score from sc s2 where s1.cid=s2.cid ) -44 、统计每门课程的学生选修人数(超过2 人的课程才统计)。要求输出课程号和选修人数, 查询结果按人数降序排列,若人数相同,按课程号升序排列select cid,count(*) 选修人数 from sc group by cid having count(*) 2 order by 选修人数 desc ,cid -45 、检索至少选修两门课程的学生学号select sid ,count(*) from sc group by sid havi
30、ng count(*)=2 -46 、查询全部学生都选修的课程的课程号和课程名select sc.cid,cname from sc ,course c where sc.cid=c.cid group by sc.cid,cname having count(cname)= (select count(*) from student ) -47 、查询没学过“ teacher1 ”老师讲授的任一门课程的学生姓名select sname from sc,student s where sc.sid=s.sid and sc.sid not in( select distinct sid fro
31、m sc where cid=sc.cid and cid=001) -48 、查询两门以上不及格课程的同学的学号及其平均成绩select sid ,avg(score) from sc where sid in (select sid from sc where isnull(score,0)2 ) group by sid 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - -49 、检索“ 004”课程分数小于60,按分数降序
32、排列的同学学号select sid from sc where cid=004 order by sid desc -50 、删除“ 002”同学的“ 001”课程的成绩delete sc where sid=002 and cid=001 一些常用的正则表达式d+$ / 非负整数(正整数 + 0) 0-9*1-90-9*$ / 正整数 (-d+)|(0+)$ / 非正整数(负整数 + 0) -0-9*1-90-9*$ / 负整数 -?d+$ / 整数 d+(.d+)?$ / 非负浮点数(正浮点数 + 0) (0-9+.0-9*1-90-9*)|(0-9*1-90-9*.0-9+)|(0-9*
33、1-90-9*)$ / 正浮点数 (-d+(.d+)?)|(0+(.0+)?)$ / 非正浮点数(负浮点数 + 0) (-(0-9+.0-9*1-90-9*)|(0-9*1-90-9*.0-9+)|(0-9*1-90-9*)$ / 负浮点数 (-?d+)(.d+)?$ / 浮点数 A-Za-z+$ / 由 26个英文字母组成的字符串 A-Z+$ / 由 26个英文字母的大写组成的字符串 a-z+$ / 由 26个英文字母的小写组成的字符串 A-Za-z0-9+$ / 由数字和 26个英文字母组成的字符串 w+$ / 由数字、26 个英文字母或者下划线组成的字符串 w-+(.w-+)*w-+(.w-+)+$ /email地址 a-zA-z+:/(w+(-w+)*)(.(w+(-w+)*)*(?S*)?$ /url /+0,1(d)1,3 ?(-?(d)| )1,12)+$/ /校验普通电话、传真号码:可以“+”开头,除数字外,可含有“ - ”名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -
限制150内