常用SQL语句有哪些
这篇文章将为大家详细讲解有关常用SQL语句有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。
问题及描述:
--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分数
*/
--创建测试数据
createtable Student(Sidvarchar(10),Snamenvarchar(10),Sagedatetime,Ssex
nvarchar(10))
insertinto Studentvalues('01'
, N'赵雷' ,
'1990-01-01' , N'男')
insertinto Studentvalues('02'
, N'钱电' ,
'1990-12-21' , N'男')
insertinto Studentvalues('03'
, N'孙风' ,
'1990-05-20' , N'男')
insertinto Studentvalues('04'
, N'李云' ,
'1990-08-06' , N'男')
insertinto Studentvalues('05'
, N'周梅' ,
'1991-12-01' , N'女')
insertinto Studentvalues('06'
, N'吴兰' ,
'1992-03-01' , N'女')
insertinto Studentvalues('07'
, N'郑竹' ,
'1989-07-01' , N'女')
insertinto Studentvalues('08'
, N'王菊' ,
'1990-01-20' , N'女')
createtable Course(Cidvarchar(10),Cnamenvarchar(10),Tidvarchar(10))
insertinto Coursevalues('01'
, N'语文' ,
'02')
insertinto Coursevalues('02'
, N'数学' ,
'01')
insertinto Coursevalues('03'
, N'英语' ,
'03')
createtable Teacher(Tidvarchar(10),Tnamenvarchar(10))
insertinto Teachervalues('01'
, N'张三')
insertinto Teachervalues('02'
, N'李四')
insertinto Teachervalues('03'
, N'王五')
createtable SC(Sidvarchar(10),Cidvarchar(10),scoredecimal(18,1))
insertinto SCvalues('01'
,'01' , 80)
insertinto SCvalues('01'
,'02' , 90)
insertinto SCvalues('01'
,'03' , 99)
insertinto SCvalues('02'
,'01' , 70)
insertinto SCvalues('02'
,'02' , 60)
insertinto SCvalues('02'
,'03' , 80)
insertinto SCvalues('03'
,'01' , 80)
insertinto SCvalues('03'
,'02' , 80)
insertinto SCvalues('03'
,'03' , 80)
insertinto SCvalues('04'
,'01' , 50)
insertinto SCvalues('04'
,'02' , 30)
insertinto SCvalues('04'
,'03' , 20)
insertinto SCvalues('05'
,'01' , 76)
insertinto SCvalues('05'
,'02' , 87)
insertinto SCvalues('06'
,'01' , 31)
insertinto SCvalues('06'
,'03' , 34)
insertinto SCvalues('07'
,'02' , 89)
insertinto SCvalues('07'
,'03' , 98)
go
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
思路:课程01(一个记录集合),课程02(一个记录集合),STUDENT表(一个记录集合),包含在这三个记录集合里,并且01分数>02分数的记录。
select*
fromstudent s inner
join(select*
from sc where cid='01') a
on s.sid=a.sidinnerjoin
(select*
from sc where cid='02') b
on s.sid=b.sidwherea.score>b.score
select a.*,b.*,c.*fromstudent a
innerjoinsc b
on a.sid=b.sidandb.cid='01'inner
join sc c
on a.sid=c.sidandc.cid='02'where
b.score>c.score
--1.1、查询同时存在"01"课程和"02"课程的情况
思路:课程01(一个记录集合),课程02(一个记录集合),STUDENT表(一个记录集合),包含在这三个记录集合里的记录。
select*
fromstudent s inner
join(select*
from sc where cid='01') a
on s.sid=a.sidinnerjoin
(select
* from sc where cid='02') b
on s.sid=b.sidwherea.sid=b.sid
select s.*,a.*,b.*fromstudent s
innerjoinsc a
on s.sid=a.sidanda.cid='01'inner
joinsc b
on s.sid=b.sidandb.cid='02'
select a.* , b.score[课程'01'的分数],c.score[课程'02'的分数]from
Student a , SC b , SC c
where a.Sid= b.Sid
and a.Sid= c.Sid
and b.Cid='01'and c.Cid='02'and
b.score> c.score
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
思路:课程01(一个记录集合),课程02可能有,可能不存在(cid='02' or cid is null),STUDENT表(一个记录集合)
select*
fromstudent s inner
joinsc a
on s.sid=a.sidanda.cid='01'left
join sc b
on s.sid=b.sidand(b.cid='02'or
b.cid is
null) where a.score>isnull(b.score,0)
select a.* , b.score[课程"01"的分数],c.score[课程"02"的分数]from
Student a leftjoin SC b
on a.Sid= b.Sid
and b.Cid='01'leftjoin SC c
on a.Sid= c.Sid
and c.Cid='02'
where b.score>isnull(c.score,0)
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select*
fromstudent s inner
joinsc a
on s.sid=a.sidanda.cid='01'inner
join sc b
on s.sid=b.sidandb.cid='02'where --2.1、查询同时存在"01"课程和"02"课程的情况 select a.* , b.score[课程'01'的分数],c.score[课程'02'的分数]from where a.Sid= b.Sid --2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况 select* on s.sid=a.sidand(a.cid='01'or on s.sid=b.sidandb.cid='02' select* (select* (select* select a.* , b.score[课程"01"的分数],c.score[课程"02"的分数]from leftjoin SC bon a.Sid leftjoin SC con a.Sid whereisnull(b.score,0)< --3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 思路:平均成绩大于等于60分(一个记录集合),STUDENT表(一个记录集合) select s.sid,s.sname,b.[平均成绩]fromstudent (select sid,convert(decimal(18,2),avg(score))as on s.sid=b.sid select* (select sid,avg(score)as on s.sid=a.sid select a.Sid , a.Sname ,cast(avg(b.score)asdecimal(18,2)) from Student a , sc b where a.Sid= b.Sid groupby a.Sid , a.Sname havingcast(avg(b.score)asdecimal(18,2))>=60 orderby a.Sid --4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 思路:平均成绩小于60分(一个记录集合),STUDENT(一个记录集合) select s.sid,s.sname,b.[平均成绩]fromstudent (select sid,convert(decimal(18,2),avg(score))as on s.sid=b.sid --4.1、查询在sc表存在成绩的学生信息的SQL语句。 思路:STUDENT表(一个记录集合)是否有记录包含在SC表(一个记录集合) select* select* select a.Sid , a.Sname ,cast(avg(b.score)asdecimal(18,2)) from Student a , sc b where a.Sid= b.Sid groupby a.Sid , a.Sname havingcast(avg(b.score)asdecimal(18,2))<60 orderby a.Sid --4.2、查询在sc表中不存在成绩的学生信息的SQL语句。 select select* select a.Sid , a.Sname ,isnull(cast(avg(b.score)asdecimal(18,2)),0) from Student aleftjoin sc b on a.Sid= b.Sid groupby a.Sid , a.Sname havingisnull(cast(avg(b.score)asdecimal(18,2)),0)<60 orderby a.Sid --5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 思路:SC表的选课总数、总成绩(一个记录集合),STUDENT表(一个记录集合) select s.sid,s.sname,a.[选课总数],a.[总成绩]fromstudent (select sid,count(*)as on s.sid=a.sid select* (select sid,count(cid)as on s.sid=a.sid select s.sid,s.sname,count(a.cid)as on s.sid=a.sidgroupby --5.1、查询所有有成绩的SQL。 select s.sid,s.sname,a.[选课总数],a.[总成绩]fromstudent (select sid,count(*)as on s.sid=a.sid select a.Sid[学生编号], from Student a , SC b where a.Sid= b.Sid groupby a.Sid,a.Sname orderby a.Sid --5.2、查询所有(包括有成绩和无成绩)的SQL。 select s.sid,s.sname,a.[选课总数],a.[总成绩]fromstudent (select sid,count(*)as on s.sid=a.sid select* (select sid,count(cid)as on s.sid=a.sidorderby select s.sid,s.sname,count(a.cid)as on s.sid=a.sidgroupby select a.Sid[学生编号], from Student aleftjoin SC b on a.Sid= b.Sid groupby a.Sid,a.Sname orderby a.Sid --6、查询"李"姓老师的数量 select count(*) as '数量' fromteacher where left(tname,1)='李' --方法1 selectcount(Tname)["李"姓老师的数量]from --方法2 selectcount(Tname)["李"姓老师的数量]from --7、查询学过"张三"老师授课的同学的信息 思路: STUDENT(一个记录集合),张三老师(一个记录集合),张三老师上的课(一个记录集合),张三老师上的课的成绩(一个记录集合) select* on s.sid=a.sidinnerjoin on a.cid=c.cidinnerjoin on c.tid=t.tidwheret.tname='张三' 思路:从全部学生中(一个记录集合)提取上过张三老师课的学生(一个记录集合) select* select sidfrom sc a on a.cid=b.cidinnerjoin on b.tid=c.tidandc.tname='张三') selectdistinct Student.*from where Student.Sid= SC.Sid orderby Student.Sid --8☆、查询没学过"张三"老师授课的同学的信息 思路:从全部学生中(一个记录集合)删除上过张三老师课的学生(一个记录集合)。 select* on a.cid=c.cidinnerjoin on c.tid=t.tidwheret.tname='张三') select m.*from Student mwhere --9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 思路:上过课程01(一个记录集合),上过课程02(一个记录集合),STUDENT表(一个记录集合) select* on s.sid=a.sidanda.cid='01'inner on s.sid=b.sidandb.cid='02' 思路:上过课程01的学生(一个记录集合)并且存在上过课程02的学生(一个记录集合) select* on s.sid=a.sidanda.cid='01'and --方法1 select Student.*from Student , SCwhere --方法2 select Student.*from Student , SCwhere --方法3 select m.*from Student mwhere ( selectdistinctSidfrom unionall selectdistinctSidfrom ) orderby m.Sid --10☆、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 思路:上过课程01的学生(一个记录集合)并且不存在上过课程02的学生(一个记录集合) select* on s.sid=a.sidanda.cid='01'and 思路:从全部学生中(一个记录集合)先提取上过课程01的学生记录(一个记录集合)再排除没上过课程02的学生记录(一个记录集合) select* (select sidfrom sc select sidfrom sc select* on s.sid=a.sidanda.cid='01'where --方法1 select Student.*from Student , SCwhere --方法2 select Student.*from Student , SCwhere --11、查询没有学全所有课程的同学的信息 思路:从全部学生中(一个记录集合)提取在SC表中课程总数不是全部的学生(一个记录集合) select* (select sidfrom (select sid,count(*)as 该方法只列出有课程分数的学生,一个课程分数也没有的学生不存在第二个记录集合中。 思路:从全部学生中(一个记录集合)排除在SC表中有全部课程分数的学生(一个记录集合) select* (select sidfrom (select sid,count(*)as 该方法还会列出一个课程分数都没有的学生。 --11.1、 select Student.* from Student , SC where Student.Sid= SC.Sid groupby Student.Sid , Student.Sname ,Student.Sage , Student.Ssexhavingcount(Cid)< --11.2 select Student.* from Studentleftjoin SC on Student.Sid= SC.Sid groupby Student.Sid , Student.Sname ,Student.Sage , Student.Ssexhavingcount(Cid)< --12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 思路:从全部学生中(一个记录集合)提取所学课程中至少有一门和学生01所学课程相同(一个记录集合)(也就是课程ID至少有一个存在于学生01的课程ID中)并排除学生01 select* (selectdistinct sid (select cidfrom sc selectdistinct Student.*from --13☆、查询和"01"号的同学学习的课程完全相同的其他同学的信息 思路:从全部学生中(一个记录集合)提取所学全部课程ID存在于学生01的课程ID中并且课程总数等于学生01的课程总数(一个记录集合) select* (selectdistinct sid (select cidfrom sc havingcount(*)=(selectcount(*) select Student.*from Studentwhere (selectdistinct SC.Sidfrom groupby SC.Sidhavingcount(1)= --14、查询没学过"张三"老师讲授的任一门课程的学生姓名 思路:从全部学生中(一个记录集合)排除学过老师张三上过的课的学生(一个记录集合)(就是在SC表中有张三老师上过的课的分数) select* (selectdistinct a.sid on a.cid=b.cidinnerjoin on b.tid=c.tidwherec.tname='张三') select student.*from studentwhere (selectdistinct sc.Sidfrom orderby student.Sid --15☆、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 思路:全部学生(一个记录集合),两门及以上不及格课程(一个记录集合) select* (select sid,count(*)as on s.sid=b.sid select s.sid,s.sname,convert(decimal(5,2),avg(a.score))as on s.sid=a.sidgroupby (select sidfrom (select sid,count(*)as select student.Sid ,student.sname ,cast(avg(score)asdecimal(18,2)) where student.Sid= SC.Sid groupby student.Sid , student.sname --16、检索"01"课程分数小于60,按分数降序排列的学生信息 思路:全部学生(一个记录集合),课程01分数小于60(一个记录集合) select* on s.sid=a.sidwherecid='01'and select* on s.sid=a.sidorderby select student.* , sc.Cid , sc.scorefrom where student.Sid= SC.Sid orderby sc.scoredesc --17☆☆☆、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 思路:全部学生(一个记录集合),全部课程分数和平均分(一个记录集合),两个记录集合进行合并行转列(新的一个记录集合) select s.sid,s.sname,max(case max(case b.cnamewhen max(case b.cnamewhen convert(decimal(18,2),avg(a.score))as from student sleft on s.sid=a.sidleftjoin on a.cid=b.cidgroupby orderby [平均成绩]desc --17.1 SQL 2000静态 select a.Sid学生编号 , a.Sname学生姓名 max(case c.Cnamewhen max(case c.Cnamewhen max(case c.Cnamewhen cast(avg(b.score)asdecimal(18,2))平均分 from Student a leftjoin SC bon a.Sid leftjoin Course con b.Cid groupby a.Sid , a.Sname orderby平均分desc --17.2 SQL 2000动态 declare@sqlnvarchar(4000) set@sql='select a.Sid '+ select@sql=@sql+',max(case from (selectdistinct Cnamefrom set@sql=@sql+' , cast(avg(b.score) groupby a.Sid , a.Sname order by '+ N'平均分'+' exec(@sql) --17.3有关sql2005的动静态写法参见我的文章《普通行列转换(version 2.0)》或《普通行列转换(version --18☆☆☆☆☆、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 思路:SC表和COURSE表联合查询,每一个字段要求都可以看作是一个子查询,一个一个子查询单独做出来后,再拼接在一起。 select b.cid,b.cname,max(score)as convert(varchar,convert(decimal(5,2),convert(decimal(5,2),count(casewhen convert(varchar,convert(decimal(5,2),convert(decimal(5,2),count(casewhen convert(varchar,convert(decimal(5,2),convert(decimal(5,2),count(casewhen convert(varchar,convert(decimal(5,2),convert(decimal(5,2),count(casewhen from sc ainner --方法1 select m.Cid[课程编号], from Course m , SC n where m.Cid= n.Cid groupby m.Cid , m.Cname orderby m.Cid --方法2 select m.Cid[课程编号], (selectmax(score)from from Course m orderby m.Cid --19、按各科成绩进行排序,并显示排名 思路:利用over(partition by字段名order by 正常排序:1,2,3 select row_number()over(partitionby 合并重复不保留空缺:1,1,2,3 select dense_rank()over(partitionby 合并重复保留空缺:1,1,3 select rank() over(partitionby cid order by cid,score desc) as sort,* --19.1 sql 2000用子查询完成 --Score重复时保留名次空缺 select t.* , px= --Score重复时合并名次 select t.* , px= --19.2sql 2005用rank,DENSE_RANK完成 --Score重复时保留名次空缺(rank完成) select t.* , px= --Score重复时合并名次(DENSE_RANK完成) select t.* , px= --20、查询学生的总成绩并进行排名 思路:所有学生的总成绩(一个记录集合),再使用函数进行排序。 select rank()over(orderby on s.sid=a.sidgroupby 这个查询只能查询到有成绩的7名学生。 select dense_rank()over(orderby isnull(sum(a.score),0)as from student sleft 用了leftjoin就可以查询到所有的8名学生了,包括没有成绩的1名学生。 --20.1查询学生的总成绩 select m.Sid[学生编号] m.Sname isnull(sum(score),0)[总成绩] from Student mleftjoin SC non groupby m.Sid , m.Sname orderby[总成绩]desc --20.2查询学生的总成绩并进行排名,sql 2000用子查询完成,分总分重复时保留名次空缺和不保留名次空缺两种。 select t1.* , px= ( m.Sname isnull(sum(score),0)[总成绩] )t2where总成绩> ( m.Sname isnull(sum(score),0)[总成绩] )t1 orderby px select t1.* , px= ( m.Sname isnull(sum(score),0)[总成绩] )t2where总成绩>= ( m.Sname isnull(sum(score),0)[总成绩] )t1 orderby px --20.3查询学生的总成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分总分重复时保留名次空缺和不保留名次空缺两种。 select t.* , px= ( m.Sname isnull(sum(score),0)[总成绩] )t orderby px select t.* , px= ( m.Sname isnull(sum(score),0)[总成绩] )t orderby px --21、查询不同老师所教不同课程平均分从高到低显示 思路:不同老师所教不同课程的平均分(一个记录集合),再使用函数over(order by字段名) select rank()over(orderby convert(decimal(5,2),avg(score))as innerjoin course b select m.Tid , m.Tname ,cast(avg(o.score)asdecimal(18,2)) from Teacher m , Course n , SCo where m.Tid= n.Tid groupby m.Tid , m.Tname orderby avg_scoredesc --22☆、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 思路:所有课程成绩的学生及课程信息(一个记录集合),再利用函数排序(一个记录集合),选择第2名和第3名的记录。 ;with abc as (select row_number() over(partition by a.cidorder by a.score a.score from student sinner select select (select row_number() over(partition by a.cidorder by a.score a.score from student sinner where t.rankingin(2,3) --22.1 sql 2000用子查询完成 --Score重复时保留名次空缺 select*from (select --Score重复时合并名次 select*from (select --22.2 sql 2005用rank,DENSE_RANK完成 --Score重复时保留名次空缺(rank完成) select*from (select --Score重复时合并名次(DENSE_RANK完成) select*from (select --23☆☆☆、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 思路:SC表和COURSE表联合查询(一个记录集合),然后每个字段都看做是一个子查询,最后将这些子查询拼接起来。 select b.cidas count(1)as'总人数', count(casewhen a.score<60then convert(decimal(5,2),count(casewhen count(casewhen a.score>=60and convert(decimal(5,2),count(casewhen count(casewhen a.score>=70and convert(decimal(5,2),count(casewhen count(casewhen a.score>=85then convert(decimal(5,2),count(casewhen from sc ainner 以上方法为横向显示。 select b.cidas when score>=60 when score>=70 else count(1)as'人数', convert(decimal(18,2),count(1)*100/(selectcount(1)from from sc ainner when score>=60 when score>=70 else orderby b.cid,b.cname,'分数段' 以上方法为纵向显示,但为0的就不显示了。 --23.1统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60] --横向显示 select Course.Cid[课程编号] from sc , Course where SC.Cid= Course.Cid groupby Course.Cid , Course.Cname orderby Course.Cid --纵向显示1(显示存在的分数段) select m.Cid[课程编号] when n.score when n.score else'0-60' from Course m , sc n where m.Cid= n.Cid groupby m.Cid , m.Cname , ( when n.score when n.score else'0-60' orderby m.Cid , m.Cname ,分数段 --纵向显示2(显示存在的分数段,不存在的分数段用0显示) select m.Cid[课程编号] when n.score when n.score else'0-60' from Course m , sc n where m.Cid= n.Cid groupbyall m.Cid , m.Cname , ( when n.score when n.score else'0-60' orderby m.Cid , m.Cname ,分数段 --23.2统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[<60]及所占百分比 --横向显示 select m.Cid课程编号, m.Cname课程名称, (selectcount(1)from (selectcount(1)from from Course m orderby m.Cid --纵向显示1(显示存在的分数段) select m.Cid[课程编号] when n.score when n.score else'0-60' from Course m , sc n where m.Cid= n.Cid groupby m.Cid , m.Cname , ( when n.score when n.score else'0-60' orderby m.Cid , m.Cname ,分数段 --纵向显示2(显示存在的分数段,不存在的分数段用0显示) select m.Cid[课程编号] when n.score when n.score else'0-60' from Course m , sc n where m.Cid= n.Cid groupbyall m.Cid , m.Cname , ( when n.score when n.score else'0-60' orderby m.Cid , m.Cname ,分数段 --24、查询学生平均成绩及其名次 思路:所有学生的平均成绩(一个记录集合),再使用函数进行排序。 select s.sid,s.sname,row_number()over(orderby avg(score))as 只显示有成绩的学生。 select s.sid,s.sname,row_number()over(orderby avg(score))as 显示所有学生。 --24.1查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。 select t1.* , px= ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t2where平均成绩> ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t1 orderby px select t1.* , px= ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t2where平均成绩>= ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t1 orderby px --24.2查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。 select t.* , px= ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t orderby px select t.* , px= ( m.Sname isnull(cast(avg(score)asdecimal(18,2)),0)[平均成绩] )t orderby px --25、查询各科成绩前三名的记录 思路:各学科成绩排序(一个记录集合),再取前3。 select (select row_number() over(partition by a.cidorder by a.score s.sid,s.sname,a.score from student sinnerjoin --25.1分数重复时保留名次空缺 select m.* , n.Cid , n.scorefrom (selecttop3 scorefrom --25.2分数重复时不保留名次空缺,合并名次 --sql 2000用子查询实现 select*from (select --sql 2005用DENSE_RANK实现 select*from (select --26、查询每门课程被选修的学生数 思路:每门课被选修的学生数(一个记录集合)。 select* (select cid,count(*)as on a.cid=b.cid select a.cid,a.cname,count(1)as on a.cid=b.cidgroupby select cid ,count(Sid)[学生数]from --27、查询出只有两门课程的全部学生的学号和姓名 select Student.Sid ,Student.Sname from Student , SC where Student.Sid= SC.Sid groupby Student.Sid , Student.Sname havingcount(SC.Cid)=2 orderby Student.Sid --28、查询男生、女生人数 思路: select ssex,count(1)as'人数'from selectcount(Ssex)as男生人数from selectcount(Ssex)as女生人数from selectsum(casewhen selectcasewhen Ssex= --29、查询名字中含有"风"字的学生信息 select* select*from studentwhere select*from studentwherecharindex(N'风' --30、查询同名同性学生名单,并统计同名人数 思路:按照姓名字段进行GROUP BY,同时计算人数,只要大于1,就是同姓同名。 select sname,count(1)as select Sname[学生姓名],count(*)[人数]from --31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) select* select*from Studentwhereyear(sage)=1990 select*from Studentwheredatediff(yy,sage,'1990-01-01')=0 select*from Studentwheredatepart(yy,sage)=1990 select*from Studentwhereconvert(varchar(4),sage,120)='1990' --32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 思路:每门课程的平均成绩(一个记录集合),再使用函数排序,排序时根据平均成绩、课程编号。 select row_number()over(orderby on a.cid=b.cidgroupby select m.Cid , m.Cname ,cast(avg(n.score)asdecimal(18,2)) from Course m, SC n where m.Cid= n.Cid groupby m.Cid , m.Cname orderby avg_scoredesc, m.Cid --33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 select s.sid,s.sname,convert(decimal(18,2),avg(a.score))as on s.sid=a.sidgroupby select a.Sid , a.Sname ,cast(avg(b.score)asdecimal(18,2)) from Student a , sc b where a.Sid= b.Sid groupby a.Sid , a.Sname havingcast(avg(b.score)asdecimal(18,2))>=85 orderby a.Sid --34、查询课程名称为"数学",且分数低于60的学生姓名和分数 select s.sid,s.sname,b.cname,a.scorefrom on s.sid=a.sidinnerjoin on a.cid=b.cid where b.cname='数学'and select sname , score from Student , SC , Course where SC.Sid= Student.Sid --35、查询所有学生的课程及分数情况; select s.sid,s.sname,b.cid,b.cname,a.score from student sinner select Student.* , Course.Cname , SC.Cid ,SC.score from Student, SC , Course where Student.Sid= SC.Sid orderby Student.Sid , SC.Cid --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; select s.sid,s.sname,b.cid,b.cname,a.scorefrom on s.sid=a.sidinnerjoin on a.cid=b.cid where a.score>70 select Student.* , Course.Cname , SC.Cid ,SC.score from Student, SC , Course where Student.Sid= SC.Sid orderby Student.Sid , SC.Cid --37、查询不及格的课程 select s.sid,s.sname,b.cid,b.cname,a.scorefrom on s.sid=a.sidinnerjoin on a.cid=b.cid where a.score<60 select Student.* , Course.Cname , SC.Cid ,SC.score from Student, SC , Course where Student.Sid= SC.Sid orderby Student.Sid , SC.Cid --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; select s.sid,s.sname,b.cid,b.cname,a.scorefrom on s.sid=a.sidinnerjoin on a.cid=b.cid where a.score>=80and b.cid='01' select Student.* , Course.Cname , SC.Cid ,SC.score from Student, SC , Course where Student.Sid= SC.Sid orderby Student.Sid , SC.Cid --39、求每门课程的学生人数 select b.cid,b.cname,count(1)as on a.cid=b.cidgroupby select Course.Cid , Course.Cname,count(*)[学生人数] from Course , SC where Course.Cid= SC.Cid groupby Course.Cid , Course.Cname orderby Course.Cid , Course.Cname --40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 思路:上张三老师课的学生(一个记录集合) selecttop 1 on s.sid=a.sidinnerjoin on a.cid=b.cidinnerjoin on b.tid=c.tidwherec.tname='张三'order --40.1当最高分只有一个时 selecttop1 Student.* from Student, SC , Course ,Teacher where Student.Sid= SC.Sid orderby SC.scoredesc --40.2当最高分出现多个时 select Student.* , Course.Cname , SC.Cid ,SC.score from Student, SC , Course ,Teacher where Student.Sid= SC.Sid SC.score= (selectmax(SC.score)from --41☆☆☆☆☆、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 思路: --方法1 select m.*from SC m ,(select where m.Cid= n.Cidand --方法2 select m.*from SC mwhereexists where m.Cid= n.Cidand --42、查询每门课程成绩最好的前两名 思路:每门课程全部成绩(一个记录集合)。 select select t.*from sc twhere --43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 select b.cid,b.cname,count(1)as on a.cid=b.cidgroupby select Course.Cid , Course.Cname,count(*)[学生人数] from Course , SC where Course.Cid= SC.Cid groupby Course.Cid , Course.Cname havingcount(*)>=5 orderby[学生人数]desc --44、检索至少选修两门课程的学生学号 select s.sid,s.sname,count(1)as on s.sid=a.sidgroupby select student.Sid ,student.Sname from student , SC where student.Sid= SC.Sid groupby student.Sid , student.Sname havingcount(1)>=2 orderby student.Sid --45、查询选修了全部课程的学生信息 select s.sid,s.sname,count(1)as on s.sid=a.sidgroupby --方法1根据数量来完成 select student.*from studentwhere (select Sidfrom sc --方法2使用双重否定来完成 select t.*from student twhere ( select Sid , Cidfrom student , course ) --方法3使用双重否定来完成 select t.*from student twherenotexists(select1from ( select Sid , Cidfrom student , course ) kwhere k.Sid ) --46、查询各学生的年龄 select*,datediff(year,sage,getdate())as 粗略算法 select*,datediff(day,sage,getdate())/365as 具体算法 --46.1只按照年份来算 select* ,datediff(yy , sage ,getdate()) --46.2按照出生日期来算,当前月日 < select* ,casewhenright(convert(varchar(10),getdate(),120),5) --47、查询本周过生日的学生 思路:将学生出生日期的年换成今年,然后加上具体日期,再和今天比较,如果为0,就是本周,如果为-1,就是下周,如果为1,就是上周。 select* wheredatediff(week,convert(varchar,datepart(yy,getdate()))+right(convert(varchar(10),sage,120),6),getdate())=0 select*from studentwheredatediff(week,datename(yy,getdate())+right(convert(varchar(10),sage,120),6),getdate())=0 --48、查询下周过生日的学生 select* wheredatediff(week,convert(varchar,datepart(yy,getdate()))+right(convert(varchar(10),sage,120),6),getdate())=-1 select*from studentwheredatediff(week,datename(yy,getdate())+right(convert(varchar(10),sage,120),6),getdate())=-1 --49、查询本月过生日的学生 思路:把学生的出生日期的年换成今年,然后判断月是否在当前月。为0就是本月,为1就是上月,为-1就是下月。 select* wheredatediff(mm,convert(varchar,datepart(yy,getdate()))+right(convert(varchar(10),sage,120),6),getdate())=0 select*from studentwheredatediff(mm,datename(yy,getdate())+right(convert(varchar(10),sage,120),6),getdate())=0 --50、查询下月过生日的学生 select* wheredatediff(mm,convert(varchar,datepart(yy,getdate()))+right(convert(varchar(10),sage,120),6),getdate())=-1 select*from studentwheredatediff(mm,datename(yy,getdate())+right(convert(varchar(10),sage,120),6),getdate())=-1 总结: 1.一种是先组合成一个总的记录集合,然后再进行GROUP BY或者ORDER 2.针对排序,有三种情况: RANK()OVER():排名1,1,3--保留 DENSE_RANK()OVER:排名1,1,2--不保留 ROW_NUMBEROVER():排名1,2,3--没有同排名的 3.有关日期的计算,一是要注意东西方对星期开始的差异,最好是使用SET DATEFIRST 1来人为的设定每周开始为星期一。二是要注意年、月、日三个元素的分别调整。三是要注意在调整过程中数据类型的变换。 关于"常用SQL语句有哪些"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
a.score
Student a , SC b , SC c
and a.Sid= c.Sid
and b.Cid='01'and c.Cid='02'and
b.score< c.score
fromstudent s left
joinsc a
a.cid is
null) innerjoin sc b
fromstudent s inner
join
from sc where cid='02') aon s.sid=a.sidleft
join
from sc where
(cid='01'or cid
is null)) b
on s.sid=b.sid
Student a
= b.Sid and b.Cid='01'
= c.Sid and c.Cid='02'
c.score
s innerjoin
'平均成绩'from sc
group by sid
having avg(score)>=60) b
fromstudent s inner
join
avgscore from scgroup
by sid having
avg(score)>=60) a
avg_score
s innerjoin
'平均成绩'from sc
group by sid
having avg(score)>60) b
fromstudent where sid
in(select sidfrom sc)
fromstudent s where
exists(select 1from sc a
where s.sid=a.sid)
avg_score
* from student where sid not
in (select distinct sid from sc)
fromstudent s where
notexists(select 1
from sc a where s.sid=a.sid)
avg_score
s innerjoin
'选课总数',sum(score)as
'总成绩'from sc
group by sid) a
fromstudent s inner
join
'课程总数',sum(score)as
'课程总成绩'from sc
group by sid) a
'课程总数',sum(a.score)as
'课程总成绩'from student s
innerjoin sc a
s.sid,s.sname
s innerjoin
'选课总数',sum(score)as
'总成绩'from sc
group by sid) a
a.Sname[学生姓名],count(b.Cid)
选课总数,sum(score)
[所有课程的总成绩]
s leftjoin
'选课总数',sum(score)as
'总成绩'from sc
group by sid) a
fromstudent s left
join
'课程总数',sum(score)as
'课程总成绩'from sc
group by sid) a
s.sid
'课程总数',sum(a.score)as
'课程总成绩'from student s
leftjoin sc a
s.sid,s.snameorder
by s.sid
a.Sname[学生姓名],count(b.Cid)
选课总数,sum(score)
[所有课程的总成绩]
Teacher where Tnamelike N'李%'
Teacher whereleft(Tname,1)=
N'李'
fromstudent s inner
joinsc a
course c
teacher t
fromstudent where sid
in(
inner join course b
teacher c
Student , SC , Course , Teacher
and SC.Cid
= Course.Cidand Course.Tid
= Teacher.Tid
and Teacher.Tname= N'张三'
fromstudent where sid
notin(
select distinct sid
from sc a inner
join course c
teacher t
Sid notin (selectdistinct SC.Sidfrom
SC , Course , Teacherwhere SC.Cid
= Course.Cid
and Course.Tid= Teacher.Tid
and Teacher.Tname
= N'张三')orderby
m.Sid
fromstudent s inner
joinsc a
join sc b
fromstudent s inner
joinsc a
exists (select 1
from sc bwhere s.sid=b.sidand b.cid='02')
Student.Sid= SC.Sid
and SC.Cid
='01'andexists (Select1from
SC SC_2 where SC_2.Sid= SC.Sid
and SC_2.Cid
='02')orderby Student.Sid
Student.Sid= SC.Sid
and SC.Cid
='02'andexists (Select1from
SC SC_2 where SC_2.Sid= SC.Sid
and SC_2.Cid
='01')orderby Student.Sid
Sid in
select Sid from
(
SC where Cid='01'
SC where Cid='02'
) t groupby Sidhavingcount(1)=2
fromstudent s inner
joinsc a
not exists
(select 1from sc b
where s.sid=b.sidand b.cid='02')
fromstudent where sid
in
where cid='01')and sid
not in
(
where cid='02')
fromstudent s inner
joinsc a
s.sid not
in (select sidfrom sc
where cid='02')
Student.Sid= SC.Sid
and SC.Cid
='01'andnotexists (Select1from
SC SC_2where SC_2.Sid
= SC.Sid
and SC_2.Cid='02')orderby
Student.Sid
Student.Sid= SC.Sid
and SC.Cid
='01'and Student.Sidnotin (Select
SC_2.Sidfrom SC SC_2
where SC_2.Sid
= SC.Sidand SC_2.Cid
='02')orderby Student.Sid
fromstudent where sid
in
abc from sc group
by sid havingcount(*)<(selectcount(*)
from course)) t)
fromstudent where sid
notin
abc from sc group
by sid havingcount(*)=(selectcount(*)
from course)) t)
(selectcount(Cid)from Course)
(selectcount(Cid)from Course)
fromstudent where sid
in
from scwhere cid
in
where sid='01')and sid<>'01')
Student , SC where Student.Sid= SC.Sid
and SC.Cid
in (select Cidfrom SC
where Sid='01')and Student.Sid
<>'01'
fromstudent where sid
in
from scwhere cid
in
where sid='01')and sid<>'01'group
by sid
from sc where sid='01'))
Sid in
SC where Sid<>'01'and SC.Cidin
(selectdistinct Cidfrom SC
where Sid='01')
(selectcount(1)from
SC where Sid='01'))
fromstudent where sid
notin
from sc a inner
join course b
teacher c
student.Sidnotin
sc , course , teacherwhere sc.Cid
= course.Cid
and course.Tid= teacher.Tid
and teacher.tname
= N'张三')
fromstudent s inner
join
'不及格课程总数',convert(decimal(18,2),avg(score))as
'平均分数'from sc
where score<60group
by sid having
count(*)>=2) b
average fromstudent sinner
joinsc a
s.sid,s.snamehaving s.sid
in
times from sc where score<60
groupby sid
having count(*)>=2) t)
avg_score from student , sc
and student.Sid
in (select Sidfrom SC
where score<60groupby
Sidhavingcount(1)>=2)
fromstudent s inner
joinsc a
score<60 order
by score desc
fromstudent s inner
join(select*
from sc where cid='01'and score<60)
a
a.score
student , sc
and sc.score
<60and sc.Cid='01'
b.cname when N'语文'then a.score
else null
end)as
'语文',
N'数学'then a.score
else null
end)as
'数学',
N'英语'then a.score
else null
end)as
'英语',
'平均成绩'
join sc a
course b
s.sid,s.sname
,
N'语文'then b.score
elsenullend)[语文],
N'数学'then b.score
elsenullend)[数学],
N'英语'then b.score
elsenullend)[英语],
= b.Sid
= c.Cid
N'学生编号'+' , a.Sname '+
N'学生姓名'
c.Cname when N'''+Cname+''' then b.score else null end) ['+Cname+']'
Course) as t
as decimal(18,2))'+ N'平均分'+'
from Student a left join SC b on a.Sid= b.Sid left join Course c on b.Cid = c.Cid
desc'
3.0)》。
'最高分',min(score)as
'最低分',convert(decimal(5,2),avg(score))as
'平均分',
a.score>=60then 1
else null
end))/count(1)*100))+'%'as
'及格率',
a.score>=70and a.score<80then 1
else null
end))/count(1)*100))+'%'as
'中等率',
a.score>=80and a.score<90then 1
else null
end))/count(1)*100))+'%'as
'优良率',
a.score>=90then 1
else null
end))/count(1)*100))+'%'as
'优秀率'
join course bon a.cid=b.cidgroup
by b.cid,b.cname
m.Cname[课程名称],
max(n.score)
[最高分],
min(n.score)
[最低分],
cast(avg(n.score)asdecimal(18,2))[平均分],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=60)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[及格率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=70and score<80
)*100.0/ (selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[中等率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=80and score<90
)*100.0/ (selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[优良率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=90)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[优秀率(%)]
m.Cname[课程名称],
SC where Cid= m.Cid)
[最高分],
(selectmin(score)from SCwhere Cid
= m.Cid)
[最低分],
(selectcast(avg(score)asdecimal(18,2))from
SC where Cid= m.Cid)
[平均分],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=60)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[及格率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=70and score<80
)*100.0/ (selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[中等率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=80and score<90
)*100.0/ (selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[优良率(%)],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=90)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[优秀率(%)]
字段名)函数。
cid order by cid,score
desc)as sort,*
from sc
cid order by cid,score
desc)as sort,*
from sc
from sc
(selectcount(1)from
SC where Cid= t.Cid
and score> t.score)
+1from sc torderby
t.cid , px
(selectcount(distinct score)from
SC where Cid= t.Cid
and score>= t.score)
from sc t
orderby t.cid , px
rank() over(partition
by cidorderby scoredesc)
from sc torderby t.Cid , px
DENSE_RANK() over(partition
by cidorderby scoredesc)
from sc torderby t.Cid , px
sum(a.score)desc)
as ranking,s.sid,s.sname,sum(a.score)as
'总成绩'from student s
innerjoin sc a
s.sid,s.sname
isnull(sum(a.score),0)desc)
as ranking,s.sid,s.sname,
'总成绩'
join sc a on s.sid=a.sidgroup
by s.sid,s.sname
,
[学生姓名] ,
m.Sid = n.Sid
(selectcount(1)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
t1.总成绩)+1from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
(selectcount(distinct总成绩)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
t1.总成绩)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
rank() over(orderby[总成绩]desc)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
DENSE_RANK() over(orderby[总成绩]desc)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
convert(decimal(5,2),avg(score))desc)
as ranking,c.tid,c.tname,b.cid,b.cname,
'平均分'from sc a
on a.cid=b.cidinner
join teacher con b.tid=c.tidgroup
by c.tid,c.tname,b.cid,b.cname
avg_score
and n.Cid= o.Cid
desc)as ranking,s.sid,s.sname,a.cid,b.cname,
join sc a on s.sid=a.sidinner
join course b on a.cid=b.cid)
* from abc where ranking in
(2,3)
* from
desc)as ranking,s.sid,s.sname,a.cid,b.cname,
join sc a on s.sid=a.sidinner
join course b on a.cid=b.cid) t
t.* , px
= (selectcount(1)from
SC where Cid= t.Cid
and score> t.score)
+1from sc t) mwhere px
between2and3orderby
m.cid , m.px
t.* , px
= (selectcount(distinct score)from
SC where Cid= t.Cid
and score>= t.score)
from sc t) m
where pxbetween2and3orderby
m.cid , m.px
t.* , px
= rank() over(partitionby cid
orderby scoredesc)
from sc t) mwhere px
between2and3orderby
m.Cid , m.px
t.* , px
= DENSE_RANK() over(partitionby cid
orderby scoredesc)
from sc t) mwhere px
between2and3orderby
m.Cid , m.px
'课程编号',b.cnameas
'课程名称',
1 else null
end) as
'不及格人数',
a.score>=0and a.score<60then 1
else null
end)*100/count(1))as
'不及格率%',
a.score<70then 1
else null
end) as
'及格人数',
a.score>=60and a.score<70then 1
else null
end)*100/count(1))as
'及格率%',
a.score<85then 1
else null
end) as
'优良人数',
a.score>=70and a.score<85then 1
else null
end)*100/count(1))as
'优良率%',
1 else null
end) as
'优秀人数',
a.score>=85then 1
else null
end)*100/count(1))as
'优秀率%'
join course bon a.cid=b.cidgroup
by b.cid,b.cname
'课程编号',b.cnameas
'课程名称',(casewhen score<60
then '0-59'
and score<70
then'60-69'
and score<85
then'70-85'
'85-100' end)
as '分数段',
sc where cid=b.cid))as
'百分比'
join course bon a.cid=b.cidgroup
by all b.cid,b.cname,(casewhen
score<60 then
'0-59'
and score<70
then'60-69'
and score<85
then'70-85'
'85-100' end)
, Cnameas[课程名称] ,
sum(casewhen score>=85then1else0end)[85-100],
sum(casewhen score>=70and
score<85then1else0end)[70-85],
sum(casewhen score>=60and
score<70then1else0end)[60-70],
sum(casewhen score<60then1else0end)[0-60]
, m.Cname[课程名称] ,分数段=
(
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end) ,
count(1)数量
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end)
, m.Cname[课程名称] ,分数段=
(
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end) ,
count(1)数量
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end)
SC where Cid= m.Cid
and score<60)[0-60],
cast((selectcount(1)from
SC where Cid= m.Cid
and score<60)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[百分比(%)],
(selectcount(1)from
SC where Cid= m.Cid
and score>=60and score<70)[60-70],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=60and score<70)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[百分比(%)],
(selectcount(1)from
SC where Cid= m.Cid
and score>=70and score<85)[70-85],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=70and score<85)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[百分比(%)],
SC where Cid= m.Cid
and score>=85)[85-100],
cast((selectcount(1)from
SC where Cid= m.Cid
and score>=85)*100.0/
(selectcount(1)from
SC where Cid= m.Cid)
asdecimal(18,2))[百分比(%)]
, m.Cname[课程名称] ,分数段=
(
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end) ,
count(1)数量
,
cast(count(1)*100.0/
(selectcount(1)from
sc where Cid= m.Cid)
asdecimal(18,2))[百分比(%)]
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end)
, m.Cname[课程名称] ,分数段=
(
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end) ,
count(1)数量
,
cast(count(1)*100.0/
(selectcount(1)from
sc where Cid= m.Cid)
asdecimal(18,2))[百分比(%)]
casewhenn.score>=85then'85-100'
>=70and n.score<85then'70-85'
>=60and n.score<70then'60-70'
end)
avg(score)desc)
as ranking,convert(decimal(18,2),
'平均成绩'from student s
innerjoin sc a
on s.sid=a.sidgroup
by s.sid,s.sname
avg(score)desc)
as ranking,convert(decimal(18,2),
'平均成绩'from student s
leftjoin sc a
on s.sid=a.sidgroup
by s.sid,s.sname
(selectcount(1)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
t1.平均成绩)+1from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
(selectcount(distinct平均成绩)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
t1.平均成绩)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
rank() over(orderby[平均成绩]desc)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
DENSE_RANK() over(orderby[平均成绩]desc)from
select m.Sid [学生编号] ,
[学生姓名] ,
from Student m leftjoin SC non m.Sid=
n.Sid
groupby m.Sid, m.Sname
* from
desc)as ranking,
sc a on s.sid=a.sid) t where rankingin
(1,2,3)
Student m, SC nwhere m.Sid
= n.Sid and n.scorein
sc where Cid= n.Cid
orderby scoredesc)
orderby n.Cid , n.scoredesc
t.* , px
= (selectcount(distinct score)from
SC where Cid= t.Cid
and score>= t.score)
from sc t) m
where pxbetween1and3orderby
m.cid , m.px
t.* , px
= DENSE_RANK() over(partitionby cid
orderby scoredesc)
from sc t) mwhere px
between1and3orderby
m.Cid , m.px
fromcourse a inner
join
'人数'from sc
group by cid) b
'人数'from course a
innerjoin sc b
a.cid,a.cname
sc groupby Cid
student groupby ssex
Studentwhere Ssex
= N'男'
Studentwhere Ssex
= N'女'
Ssex = N'男'then1else0end)[男生人数],sum(casewhen
Ssex = N'女'then1else0end)[女生人数]from
student
N'男'then N'男生人数'else
N'女生人数'end[男女情况]
, count(1)[人数]from
studentgroupbycasewhen Ssex=
N'男'then N'男生人数'else
N'女生人数'end
fromstudent where sname
like'%风%'
sname like N'%风%'
, sname) >0
'人数'from student
groupby sname
having count(1)>1
Studentgroupby Snamehavingcount(*)>1
fromstudent where
datepart(year,sage)='1990'
convert(decimal(18,2),avg(a.score))desc,b.cid)as
'排名',b.cid,b.cname,convert(decimal(18,2),avg(a.score))as
'平均成绩'from sc a
inner join course b
b.cid,b.cname
avg_score
asc
'平均成绩'from student s
innerjoin sc a
s.sid,s.snamehaving
avg(a.score)>=85
avg_score
student sinnerjoin sc a
course b
a.score<60
and SC.Cid
= Course.Cidand Course.Cname= N'数学'and
score <60
join sc a on s.sid=a.sidinner
join course bon a.cid=b.cid
and SC.Cid
= Course.Cid
student sinnerjoin sc a
course b
and SC.Cid
= Course.Cidand SC.score
>=70
student sinnerjoin sc a
course b
and SC.Cid
= Course.Cidand SC.score
<60
student sinnerjoin sc a
course b
and SC.Cid
= Course.Cidand SC.Cid
='01'and SC.score>=80
'人数'from sc a
inner join course b
b.cid,b.cname
* from student s
inner join sc a
course b
teacher c
by a.scoredesc
, Course.Cname , SC.Cid ,SC.score
and SC.Cid
= Course.Cidand Course.Tid
= Teacher.Tid
and Teacher.Tname= N'张三'
and SC.Cid
= Course.Cidand Course.Tid
= Teacher.Tid
and Teacher.Tname= N'张三'and
SC , Course , Teacherwhere SC.Cid
= Course.Cid
and Course.Tid= Teacher.Tid
and Teacher.Tname
= N'张三')
Cid , score from SCgroupby Cid , scorehavingcount(1)>1)
n
m.score = n.score
orderby m.Cid , m.score , m.Sid
(select1from (select Cid , scorefrom
SC groupby Cid , scorehavingcount(1)>1)
n
m.score = n.score)
orderby m.Cid , m.score , m.Sid
* from (selectrow_number() over(partitionby cid order by score desc) as ranking,* from sc)
a whereranking in (1,2)
score in (selecttop2 scorefrom
sc where Cid= T.Cid
orderby scoredesc)
orderby t.Cid , t.scoredesc
'人数'from sc a
inner join course b
b.cid,b.cnamehaving
count(1)>5order
by count(1)
desc,b.cid
, Course.Cid
'课程数'from student s
innerjoin sc a
s.sid,s.snamehaving
count(1)>=2
'课程数'from student s
innerjoin sc a
s.sid,s.snamehaving
count(1)>=(selectcount(1)from
course)
Sid in
groupby Sidhavingcount(1)=
(selectcount(1)from
course))
t.Sid notin
selectdistinctm.Sidfrom
(
) m wherenotexists (select1from
sc n where n.Sid= m.Sid
and n.Cid= m.Cid)
selectdistinctm.Sidfrom
(
) m wherenotexists (select1from
sc n where n.Sid= m.Sid
and n.Cid= m.Cid)
= t.Sid
'年龄'from student
'年龄'from student
[年龄]from student
出生年月的月日则,年龄减一
, sage ,getdate())
-1elsedatediff(yy , sage ,getdate())
end[年龄]from student
fromstudent
fromstudent
fromstudent
fromstudent
BY等其他操作;另一种是分别先对小的记录集合进行其他操作,然后再组合到一起成为最终的一个记录集合。