/************************************************************************************************************
* 数据见50条常用sql.sql脚本
* 1.学生表
* student(studentID,stu_name,stu_age,stu_sex) --studentID 学生编号,stu_name 学生姓名,stu_age 出生年月,stu_sex 学生性别
* 2.课程表
* course(courseID,crs_name,teacherID) --courseID --课程编号,crs_name 课程名称,teacherID 教师编号
* 3.教师表
* teacher(teacherID,tea_name) --teacherID 教师编号,tea_name 教师姓名
* 4.成绩表
* stu_crs(studentID,courseID,score) --studentID 学生编号,courseID 课程编号,score 分数
"01"课程、"02"课程是变量(记录)。不是常量(字段)。
如果把"01"、"02"设计成字段,就会使难度降低,但是不够灵活。比如以后会再加一科目04物理。
* ########方法:先确定哪些表的字段,再一一满足条件,即可。
************************************************************************************************************/
表达式:where isnull(b.score,0)是简单的动态,还有动态sql。
连接顺序:选择那个表作为源数据表,那个表优先被连接
3、group by子句将数据划分为多个分组;
5、使用having子句筛选分组;
7、使用order by对结果集进行排序。
group by 、左外连接和where并用。
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
#####同一个表同一个字段值的比较,把一个表变为两个表,才能比较。
#####正常的是,一个字段值和一个常量比较。
有两种情况:1.1、1.2
--1.1、同时存在"01"课程和"02"课程的情况(01必选课,02必选课)
SELECT a.*,b.score,c.score
FROM student a,stu_crs b,stu_crs c
WHERE b.courseID='01' AND c.courseID='02' AND b.score>c.score AND a.studentID=b.studentID AND c.studentID=a.studentID
(a.studentID=b.studentID AND c.studentID=a.studentID这两个都需要,因为c.studentID和b.studentID与a表关联的结果集不一样)
--1.2、存在"01"课程但可能不存在"02"课程的情况、(01必选课,02选修课)、(不存在时显示为null)(以下存在相同内容时不再解释)
select * from student a
left join stu_crs b on a.studentID=b.studentID and b.courseID='01'
left join stu_crs c on a.studentID=c.studentID and c.courseID='04'
and b.score>c.score (and 有8条,where b.score>c.score只有一条)
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.*,avg(b.score) from student a,stu_crs b
where a.studentID = b.studentID
group by a.studentID
having avg(b.score)>70
order by avg(b.score) desc
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--5.1、查询所有有成绩的SQL。
select a.*,count(b.score),sum(b.score) from student a,stu_crs b
where a.studentID = b.studentID #######因为“表达式需要b.score”,且b.score可能有空的。resultSet7条。
group by a.studentID
--5.2、查询所有(包括有成绩和无成绩)的SQL。
select a.*,count(b.score),sum(b.score) from student a
left join stu_crs b on a.studentID = b.studentID #######left join,因为b.score可能有空的。resultSet8条。
group by a.studentID #######对结果集还要进行计算的,一般需要group by、having
--11、查询没有学全所有课程的同学的信息
select * from student a,stu_crs b,course c
where a.studentID=b.studentID
group by a.studentID
having count(distinct b.courseID) ######不能是count(distinct b.courseID)==1,因为group by a.studentID过了,
< count(distinct c.courseID)
--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct ########distinct过滤重复结果
a.* from student a, stu_crs b where a.studentID = b.studentID and b.courseID
in (select courseID from stu_crs where studentID = '01') and a.studentID <> '01'
--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select student.* from student where studentID in
(select distinct stu_crs.studentID from stu_crs where studentID <> '01'
and stu_crs.courseID in (select distinct courseID from stu_crs where studentID = '01') ######限定范围和限定数量一块就可以确定完全一样了。
group by stu_crs.studentID having count(1) = (select count(1) from stu_crs where studentID='01')) ###限定数量,count(1) 与count(*)效果一样。
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.*,avg(b.score) from student a,stu_crs b
where a.studentID=b.studentID and a.studentID
in(select studentID from stu_crs where score <60 group by studentID having count(1)>1)
group by a.studentID
--------------------------------未完成--------------------------------
--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.studentID , a.stu_name ,avg(b.score) ps
max(case c.crs_name when N'语文' then b.score else null end) [语文], #####表达式???????????
from student a
left join stu_crs b on a.studentID = b.studentID
left join course c on b.courseID = c.courseID
group by a.studentID , a.stu_name
order by ps desc #####order by时可以用别名,不用再查一次了。
--21、查询不同老师所教不同课程平均分从高到低显示
select m.teacherID , m.tea_name , cast(avg(o.score) as decimal(18,2)) avg_score
from teacher m , course n , stu_crs o
where m.teacherID = n.teacherID and n.courseID = o.courseID
group by m.teacherID , m.tea_name
order by avg_score destu_crs
--22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
--22.1 sql 2000用子查询完成
--score重复时保留名次空缺
select * from (select t.* , px = (select count(1) from stu_crs where courseID = t.courseID and score > t.score) + 1 from stu_crs t) m where px between 2 and 3 order by m.courseID , m.px
--score重复时合并名次
select * from (select t.* , px = (select count(distinct score) from stu_crs where courseID = t.courseID and score >= t.score) from stu_crs t) m where px between 2 and 3 order by m.courseID , m.px
--22.2 sql 2005用rank,DENSE_RANK完成
--score重复时保留名次空缺(rank完成)
select * from (select t.* , px = rank() over(partition by courseID order by score destu_crs) from stu_crs t) m where px between 2 and 3 order by m.courseID , m.px
--score重复时合并名次(DENSE_RANK完成)
select * from (select t.* , px = DENSE_RANK() over(partition by courseID order by score destu_crs) from stu_crs t) m where px between 2 and 3 order by m.courseID , m.px
--23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
--23.1 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]
--横向显示
select course.courseID [课程编号] , crs_name as [课程名称] ,
sum(case when score >= 85 then 1 else 0 end) [85-100],
sum(case when score >= 70 and score < 85 then 1 else 0 end) [70-85],
sum(case when score >= 60 and score < 70 then 1 else 0 end) [60-70],
sum(case when score < 60 then 1 else 0 end) [0-60]
from stu_crs , course
where stu_crs.courseID = course.courseID
group by course.courseID , course.crs_name
order by course.courseID
--纵向显示1(显示存在的分数段)
select m.courseID [课程编号] , m.crs_name [课程名称] , 分数段 = (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) ,
count(1) 数量
from course m , stu_crs n
where m.courseID = n.courseID
group by m.courseID , m.crs_name , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.courseID , m.crs_name , 分数段
--纵向显示2(显示存在的分数段,不存在的分数段用0显示)
select m.courseID [课程编号] , m.crs_name [课程名称] , 分数段 = (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) ,
count(1) 数量
from course m , stu_crs n
where m.courseID = n.courseID
group by all m.courseID , m.crs_name , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.courseID , m.crs_name , 分数段
--23.2 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[<60]及所占百分比
--横向显示
select m.courseID 课程编号, m.crs_name 课程名称,
(select count(1) from stu_crs where courseID = m.courseID and score < 60) [0-60],
cast((select count(1) from stu_crs where courseID = m.courseID and score < 60)*100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)],
(select count(1) from stu_crs where courseID = m.courseID and score >= 60 and score < 70) [60-70],
cast((select count(1) from stu_crs where courseID = m.courseID and score >= 60 and score < 70)*100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)],
(select count(1) from stu_crs where courseID = m.courseID and score >= 70 and score < 85) [70-85],
cast((select count(1) from stu_crs where courseID = m.courseID and score >= 70 and score < 85)*100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)],
(select count(1) from stu_crs where courseID = m.courseID and score >= 85) [85-100],
cast((select count(1) from stu_crs where courseID = m.courseID and score >= 85)*100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)]
from course m
order by m.courseID
--纵向显示1(显示存在的分数段)
select m.courseID [课程编号] , m.crs_name [课程名称] , 分数段 = (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) ,
count(1) 数量 ,
cast(count(1) * 100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)]
from course m , stu_crs n
where m.courseID = n.courseID
group by m.courseID , m.crs_name , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.courseID , m.crs_name , 分数段
--纵向显示2(显示存在的分数段,不存在的分数段用0显示)
select m.courseID [课程编号] , m.crs_name [课程名称] , 分数段 = (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) ,
count(1) 数量 ,
cast(count(1) * 100.0 / (select count(1) from stu_crs where courseID = m.courseID) as decimal(18,2)) [百分比(%)]
from course m , stu_crs n
where m.courseID = n.courseID
group by all m.courseID , m.crs_name , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.courseID , m.crs_name , 分数段
--24、查询学生平均成绩及其名次
--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select t1.* , px = (select count(1) from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t2 where 平均成绩 > t1.平均成绩) + 1 from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t1
order by px
select t1.* , px = (select count(distinct 平均成绩) from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t2 where 平均成绩 >= t1.平均成绩) from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t1
order by px
--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select t.* , px = rank() over(order by [平均成绩] destu_crs) from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t
order by px
select t.* , px = DENSE_RANK() over(order by [平均成绩] destu_crs) from
(
select m.studentID [学生编号] ,
m.stu_name [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]
from student m left join stu_crs n on m.studentID = n.studentID
group by m.studentID , m.stu_name
) t
order by px
--25、查询各科成绩前三名的记录
--25.1 分数重复时保留名次空缺
select m.* , n.courseID , n.score from student m, stu_crs n where m.studentID = n.studentID and n.score in
(select top 3 score from stu_crs where courseID = n.courseID order by score destu_crs) order by n.courseID , n.score destu_crs
--25.2 分数重复时不保留名次空缺,合并名次
--sql 2000用子查询实现
select * from (select t.* , px = (select count(distinct score) from stu_crs where courseID = t.courseID and score >= t.score) from stu_crs t) m where px between 1 and 3 order by m.courseID , m.px
--sql 2005用DENSE_RANK实现
select * from (select t.* , px = DENSE_RANK() over(partition by courseID order by score destu_crs) from stu_crs t) m where px between 1 and 3 order by m.courseID , m.px
--26、查询每门课程被选修的学生数
select courseID , count(studentID)[学生数] from stu_crs group by courseID
--27、查询出只有两门课程的全部学生的学号和姓名
select student.studentID , student.stu_name
from student , stu_crs
where student.studentID = stu_crs.studentID
group by student.studentID , student.stu_name
having count(stu_crs.courseID) = 2
order by student.studentID
--28、查询男生、女生人数
select count(stu_sex) as 男生人数 from student where stu_sex = N'男'
select count(stu_sex) as 女生人数 from student where stu_sex = N'女'
select sum(case when stu_sex = N'男' then 1 else 0 end) [男生人数],sum(case when stu_sex = N'女' then 1 else 0 end) [女生人数] from student
select case when stu_sex = N'男' then N'男生人数' else N'女生人数' end [男女情况] , count(1) [人数] from student group by case when stu_sex = N'男' then N'男生人数' else N'女生人数' end
--29、查询名字中含有"风"字的学生信息
select * from student where stu_name like N'%风%'
select * from student where charindex(N'风' , stu_name) > 0
--30、查询同名同性学生名单,并统计同名人数
select stu_name [学生姓名], count(*) [人数] from student group by stu_name having count(*) > 1
--31、查询1990年出生的学生名单(注:student表中stu_age列的类型是datetime)
select * from student where year(stu_age) = 1990
select * from student where datediff(yy,stu_age,'1990-01-01') = 0
select * from student where datepart(yy,stu_age) = 1990
select * from student where convert(varchar(4),stu_age,120) = '1990'
--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select m.courseID , m.crs_name , cast(avg(n.score) as decimal(18,2)) avg_score
from course m, stu_crs n
where m.courseID = n.courseID
group by m.courseID , m.crs_name
order by avg_score destu_crs, m.courseID astu_crs
--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.studentID , a.stu_name , cast(avg(b.score) as decimal(18,2)) avg_score
from student a , stu_crs b
where a.studentID = b.studentID
group by a.studentID , a.stu_name
having cast(avg(b.score) as decimal(18,2)) >= 85
order by a.studentID
--34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select stu_name , score
from student , stu_crs , course
where stu_crs.studentID = student.studentID and stu_crs.courseID = course.courseID and course.crs_name = N'数学' and score < 60
--35、查询所有学生的课程及分数情况;
select student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID
order by student.studentID , stu_crs.courseID
--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID and stu_crs.score >= 70
order by student.studentID , stu_crs.courseID
--37、查询不及格的课程
select student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID and stu_crs.score < 60
order by student.studentID , stu_crs.courseID
--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID and stu_crs.courseID = '01' and stu_crs.score >= 80
order by student.studentID , stu_crs.courseID
--39、求每门课程的学生人数
select course.courseID , course.crs_name , count(*) [学生人数]
from course , stu_crs
where course.courseID = stu_crs.courseID
group by course.courseID , course.crs_name
order by course.courseID , course.crs_name
--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
--40.1 当最高分只有一个时
select top 1 student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course , teacher
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID and course.teacherID = teacher.teacherID and teacher.tea_name = N'张三'
order by stu_crs.score destu_crs
--40.2 当最高分出现多个时
select student.* , course.crs_name , stu_crs.courseID , stu_crs.score
from student, stu_crs , course , teacher
where student.studentID = stu_crs.studentID and stu_crs.courseID = course.courseID and course.teacherID = teacher.teacherID and teacher.tea_name = N'张三' and
stu_crs.score = (select max(stu_crs.score) from stu_crs , course , teacher where stu_crs.courseID = course.courseID and course.teacherID = teacher.teacherID and teacher.tea_name = N'张三')
--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
--方法1
select m.* from stu_crs m ,(select courseID , score from stu_crs group by courseID , score having count(1) > 1) n
where m.courseID= n.courseID and m.score = n.score order by m.courseID , m.score , m.studentID
--方法2
select m.* from stu_crs m where exists (select 1 from (select courseID , score from stu_crs group by courseID , score having count(1) > 1) n
where m.courseID= n.courseID and m.score = n.score) order by m.courseID , m.score , m.studentID
--42、查询每门功成绩最好的前两名
select t.* from stu_crs t where score in (select top 2 score from stu_crs where courseID = T.courseID order by score destu_crs) order by t.courseID , t.score destu_crs
--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select course.courseID , course.crs_name , count(*) [学生人数]
from course , stu_crs
where course.courseID = stu_crs.courseID
group by course.courseID , course.crs_name
having count(*) >= 5
order by [学生人数] destu_crs , course.courseID
--44、检索至少选修两门课程的学生学号
select student.studentID , student.stu_name
from student , stu_crs
where student.studentID = stu_crs.studentID
group by student.studentID , student.stu_name
having count(1) >= 2
order by student.studentID
--45、查询选修了全部课程的学生信息
--方法1 根据数量来完成
select student.* from student where studentID in
(select studentID from stu_crs group by studentID having count(1) = (select count(1) from course))
--方法2 使用双重否定来完成
select t.* from student t where t.studentID not in
(
select distinct m.studentID from
(
select studentID , courseID from student , course
) m where not exists (select 1 from stu_crs n where n.studentID = m.studentID and n.courseID = m.courseID)
)
--方法3 使用双重否定来完成
select t.* from student t where not exists(select 1 from
(
select distinct m.studentID from
(
select studentID , courseID from student , course
) m where not exists (select 1 from stu_crs n where n.studentID = m.studentID and n.courseID = m.courseID)
) k where k.studentID = t.studentID
)
--46、查询各学生的年龄
--46.1 只按照年份来算
select * , datediff(yy , stu_age , getdate()) [年龄] from student
--46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select * , case when right(convert(varchar(10),getdate(),120),5) < right(convert(varchar(10),stu_age,120),5) then datediff(yy , stu_age , getdate()) - 1 else datediff(yy , stu_age , getdate()) end [年龄] from student
--47、查询本周过生日的学生
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),stu_age,120),6),getdate()) = 0
--48、查询下周过生日的学生
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),stu_age,120),6),getdate()) = -1
--49、查询本月过生日的学生
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),stu_age,120),6),getdate()) = 0
--50、查询下月过生日的学生
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),stu_age,120),6),getdate()) = -1
drop table student,course,teacher,stu_crs