--检测数据库studentdb是否存在,存在即删除
if db_id('studentdb') is not null
drop database studentdb
go
--创建数据库studentdb
create database studentdb
go
--使用数据库
use studentdb
go
--创建学生信息表
create table student(
sno int not null primary key, --学号
sname nvarchar(10), --姓名
sage tinyint, --年龄
ssex nchar(1) , --性别
)
go
--创建课程表
create table course
(cno varchar(4) not null primary key, --课程编号
cname nvarchar(10), --课程名称
tno varchar(4) references teacher(tno) --教师编号
)
go
--创建成绩表
create table sc
(sno int not null foreign key references student(sno),
cno varchar(4) not null foreign key references course(cno),
score int ,
primary key(sno,cno)
)
go
--创建教师表
create table teacher
(
tno varchar(4) not null primary key,
tname nvarchar(10)
)
go
select * from student
/*
sno sname sage ssex
611101 李勇 21 男
611102 刘晨 20 男
611103 王敏 20 女
611104 张小红 20 女
621101 张立 20 男
621102 吴宾 20 女
621103 张海 20 男
631101 钱小平 20 女
631102 王大力 20 男
631103 张姗姗 20 女
*/
select * from course
/*
cno cname tno
c001 高等数学 t001
c002 c语言 t002
c003 大学英语 t003
c004 计算机文化学 t004
c005 VB t005
c006 数据库基础 t006
c007 数据结构 t007
c008 计算机网络 t008
*/
select * from sc
/*
sno cno score
611101 c001 96
611101 c002 80
611101 c003 82
611101 c005 62
611102 c001 92
611102 c002 90
611102 c004 84
621102 c001 76
621102 c004 85
621102 c005 73
621102 c007 NULL
621103 c001 50
621103 c004 80
631101 c001 50
631101 c004 80
631102 c007 NULL
631103 c004 78
631103 c005 65
631103 c007 NULL
*/
select * from teacher
/*
tno teacher
t001 吴兵
t002 周川
t003 张晓
t004 张步群
t005 侯西洋
t006 陈兵
t007 孙冰
t008 陈立刚
*/
use studentdb
go
/*-------------------------------------练习---------------------------------------*/
--1、查询"c001"课程比"c002"课程成绩高的所有学生的学号;
select * from sc where cno='c001'
and score>all(select score from sc where cno='c002')
--2、查询课程编号"C002"的成绩比课程编号"C001"课程低的所有同学的学号、姓名;
select * from sc where cno='c002'
and score<all(select score from sc where cno='c001')
--3、查询平均成绩大于60分的同学的学号和平均成绩;
select 学号,平均成绩 from
(select sno as 学号,avg(score) as 平均成绩 from sc
group by sno)t where 平均成绩>60
--4、查询所有同学的学号、姓名、选课数、总成绩;
select s.sno 学号,sname 姓名,选课数,总成绩 from student s left join
(select sc.sno,count(cno) as 选课数,sum(score) as 总成绩 from student s1 left join
sc on s1.sno=sc.sno
group by sc.sno)t on s.sno=t.sno
--5、查询没学过"吴兵"老师课的同学的学号、姓名;
select s.sno,sname from student s join sc on s.sno=sc.sno
and cno!=(select cno from course c join teacher t on c.tno=t.tno where tname='吴兵')
--6、查询学过"c001"并且也学过编号"c002"课程的同学的学号、姓名;
select s.sno,sname from student s join sc on s.sno=sc.sno where cno in ('c001','c002')
--7、查询学过"周川"老师所教的所有课的同学的学号、姓名;
select sno,sname from student where sno in
( select sno from sc
where cno=(select cno from course c join teacher t on c.tno=t.tno where tname='周川')
)
--8、查询所有课程成绩小于60分的同学的学号、姓名;
select s.sno,sname from student s
where sno in (select s.sno from student s join sc on s.sno=sc.sno where score<60)
--9、查询没有学全所有课的同学的学号、姓名;
select s.sno,sname from student s,sc
group by s.sno,s.sname
having count(cno)!=(select count(cno) from course)
--10、查询至少有一门课与学号为"611101"的同学所学相同的同学的学号和姓名;
select distinct s.sno,sname from student s join sc on s.sno=sc.sno where cno in
(select cno from sc where sno=611101)
--11、查询至少学过学号为"611102"同学所有一门课的其他同学学号和姓名;
select distinct s.sno,sname from student s join sc on s.sno=sc.sno where cno in
(select cno from sc where sno=611102)
--12、把"SC"表中"吴兵"老师教的课的成绩都更改为此课程的平均成绩;
update sc set score=(select avg(score) from sc sc2 where sc2.cno=sc.cno )
from course,teacher t where c.tno=t.tno and tname='吴兵'
--13、查询和"1002"号的同学学习的课程完全相同的其他同学学号和姓名;
select * from sc where sno='1002'
--14、删除学习"吴兵"老师课的SC表记录;
delete from sc
from sc join course c on c.cno=sc.cno join teacher t on t.tno=c.cno where tname='吴兵'
--15、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号"c002"课程的同学学号、2号课的平均成绩;
Insert SC select sno,'002',(Select avg(score)
from SC where cno='c002') from Student where sno not in (Select sno from SC where cno='c002');
--16、按平均成绩从高到低显示所有学生的"数据库"、"企业管理"、"英语"三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分
select sno as 学生id,
(select score from sc where cno='c004' and sno=sc1.sno) as 数据库,
(select score from sc where cno='c002' and sno=sc1.sno) as 企业管理,
(select score from sc where cno='c001' and sno=sc1.sno) as 英语,
count(*) as 有效课程数,avg(sc1.score) as 有效平均分
from sc as sc1
group by sno
order by 有效平均分
--17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select 课程id=cno,最高分=max(score),最低分=min(score) from sc
group by cno
--18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select avg(score) as 平均成绩,
(convert(float,sum(case when score>60 then 1 end))/ convert(float,count(cno))) as 及格率 from sc group by cno
order by 平均成绩 desc
--19、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(c001),马克思(c002),UML (c003),数据库(c004)
select
企业管理平均分=avg(case when cno='c001' then score end),
企业管理合格百分比=100*sum(case when cno='c001' and score>=60 then 1.0 end)/sum(case when cno='c001' then 1.0 end),
马克思平均分=avg(case when cno='c002' then score end),
企业管理合格百分比=100*sum(case when cno='c002' and score>=60 then 1.0 end)/sum(case when cno='c002' then 1.0 end),
UML平均分=avg(case when cno='c003' then score end),
UML合格百分比=100*sum(case when cno='c003' and score>=60 then 1.0 end)/sum(case when cno='c003' then 1.0 end),
数据库平均分=avg(case when cno='c004' then score end) ,
数据库百分比=100*sum(case when cno='c004' and score>=60 then 1.0 end)/sum(case when cno='c004' then 1.0 end)
from sc
--20、查询如下课程成绩第3 名到第6 名的学生成绩单:企业管理(c001),马克思(c002),UML (c003),数据库(c004)
[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩
SELECT DISTINCT top 3
SC.Sno As 学生学号,
Student.Sname AS 学生姓名,
T1.score AS 企业管理,
T2.score AS 马克思,
T3.score AS UML,
T4.score AS 数据库,
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 总分
FROM Student,SC LEFT JOIN SC AS T1
ON SC.Sno = T1.Sno AND T1.Cno = '001'
LEFT JOIN SC AS T2
ON SC.Sno = T2.Sno AND T2.Cno = '002'
LEFT JOIN SC AS T3
ON SC.Sno = T3.Sno AND T3.Cno = '003'
LEFT JOIN SC AS T4
ON SC.Sno = T4.Sno AND T4.Cno = '004'
WHERE student.Sno=SC.Sno and
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
NOT IN
(SELECT
DISTINCT
TOP 15 WITH TIES
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
FROM sc
LEFT JOIN sc AS T1
ON sc.Sno = T1.Sno AND T1.Cno = 'k1'
LEFT JOIN sc AS T2
ON sc.Sno = T2.Sno AND T2.Cno = 'k2'
LEFT JOIN sc AS T3
ON sc.Sno = T3.Sno AND T3.Cno = 'k3'
LEFT JOIN sc AS T4
ON sc.Sno = T4.Sno AND T4.Cno = 'k4'
ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC)
--21、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select sc.cno as 课程id,
cname as 课程名称,
sum(case when score>85 and score<=100 then 1 end )[100-85],
sum(case when score>70 and score<=85 then 1 end ) [85-70],
sum(case when score>60 and score<=70 then 1 end )[70-60],
sum(case when score<60 then 1 end )[<60]
from sc join course c on sc.cno=c.cno
group by sc.cno,cname
--22、查询学生平均成绩及其名次
select avg(score) as 平均成绩,(row_number() over(order by avg(score) desc)) as 排名 from sc group by sno
--23、查询各科成绩前三名的记录:(不考虑成绩并列情况)
select * from ( select sno,cno,score,row_number() over(partition by cno order by score desc) as num from sc )t where num<=3
--24、查询每门课程被选修的学生数
select cno as 课程编号,count(sno) as 学生数 from sc group by cno
--25、查询出只选修了一门课程的全部学生的学号和姓名
select s.sno,sname from student s
where exists(select sno,count(cno) from sc where sno=s.sno group by sno having count(cno)=1)
--26、查询男生、女生人数
--方法一:
select ssex as 性别,count(ssex) as 人数 from student group by ssex
--方法二:
select sum(case when ssex='男' then 1 end) as 男生人数,sum(case when ssex='女' then 1 end ) as 女生人数 from student
--27、查询姓"张"的学生名单
select sname from student where sname like '张%'
--30、查询同名同性学生名单,并统计同名人数
select * from student group by
--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select sname from student where year(sage)='1981'
--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select avg(score) avg_score from sc group by cno
order by avg_score asc,cno desc
--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select s.sno,sname,平均成绩 from student s
join
(
select sno,avg(score) as 平均成绩 from sc
where score>85
group by sno
)t on s.sno=t.sno
--34、查询课程名称为"数据库",且分数低于60的学生姓名和分数
select sname,score from student s
join ( select sno,score from sc
where exists(select cno from course where cname='数据库' and cno=sc.cno) and score<60 )t on s.sno=t.sno
select * from sc where cno='c004'
--35、查询所有学生的选课情况;
select s.sno,s.sname,sc.cno,score from student s left join sc on s.sno=sc.sno join course c on c.cno=sc.cno
--36、查询任何一门课程成绩都在70分以上的姓名、课程名称和分数;
SELECT distinct student.Sno,student.Sname,SC.Cno,SC.score
FROM student,Sc
WHERE SC.score>=70 AND SC.Sno=student.Sno;
--37、查询不及格的课程,并按课程号从大到小排列
select distinct cno from sc where score<60 order by cno desc
--38、查询课程编号为c003且课程成绩在80分以上的学生的学号和姓名;
select sname,score from sc join student s on sc.sno=s.sno where score>80 and cno='c003'
--39、求选了课程的学生人数
select count(distinct sno) as 人数 from sc
--40、查询选修"吴兵"老师所授课程的学生中,成绩最高的学生姓名及其成绩
select top 1 s.sno,t.score from student s join (select sno,score from sc where cno in
( select cno from course c where tno=(select tno from teacher where tname='吴兵')))t on s.sno=t.sno order by score desc
--41、查询各个课程及相应的选修人数
select cno,count(sno) from sc group by cno
--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct A.Sno,B.score from SC A ,SC B where A.Score=B.Score and A.Cno <>B.Cno;
--43、查询每门功课成绩最好的前两名
select t.sno,cno,num from (select sno,cno,(row_number() over(partition by cno order by score desc)) as num from sc)t where num<=2
--44、统计每门课程的学生选修人数(超过3人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno 课程编号,count(sno) as 选课人数
from sc
group by cno
having count(sno)>3
order by 选课人数 desc,课程编号
--45、检索至少选修两门课程的学生学号
select sno as 学号 from sc
group by sno
having count(cno)>=2
--46、查询全部学生都选修的课程的课程号和课程名
select distinct sc.cno,cname from course c right join sc on c.cno=sc.cno
--47、查询没学过"吴兵"老师讲授的任一门课程的学生姓名
--方法一:
select sname from student where sno not in
( select distinct sno from sc where cno in
( select cno from course c
where exists( select tno from teacher where tname='吴兵' and tno=c.tno )))
--48、查询两门以上不及格课程的同学的学号及其平均成绩
select sno 学号,avg(score) as 平均成绩 from sc
group by sno
having sum(case when score<60 then 1 end)>2
--49、检索"c004"课程分数小于60,按分数降序排列的同学学号
select * from sc where score<60 and cno='c004'
order by score desc
--50、删除"1002"同学的"c001"课程的成绩
delete from sc where cno='c001' and sno='1002'