SQL server查询代码实操例子

–查询选修了课程号为"01",“02”,“03"的学号、课程号和成绩记录
select sno,s.cno,grade
from score s,course c
where s.cno=c.cno and (c.cno=‘08181192’ or c.cno=‘08181170’ or c.cno=‘08181060’)
–(2)查询课程号"01”,“02”,“03"除外的,成绩大于分的学号、课程号和成绩记录
select sno,s.cno,grade
from score s,course c
where grade>60 and s.cno=c.cno and (c.cno<>‘08181192’ and c.cno<>‘08181170’ and c.cno<>‘08181060’)
–(3)查询选修了课程号为"01”,“02”,“03”,成绩在-80分的学号、课程号和成绩记录
select sno,s.cno,grade
from score s,course c
where grade>70 and grade<80 and s.cno=c.cno and (c.cno=‘08181192’ or c.cno=‘08181170’ or c.cno=‘08181060’)
–(4)查询选修了课程号为"01"的最好成绩、最差成绩、平均成绩记录
select max(grade) 最好成绩,min(grade) 最差成绩,avg(grade) 平均成绩
from score
where cno=‘08181192’
–(5)查询’201401’班的男生人数
select count() 男生人数
from student
where depart=‘001’ and gender=‘男’
–(6)查询’201401’班张姓同学的人数
select count(
) 姓张的人数
from student
where depart=‘003’ and sname like ‘张%’
–(7)查询’201401’班张姓同学的学号、姓名
select sno,sname
from student
where depart=‘003’ and sname like ‘张%’
–8 查询年后出生的副教授的记录
select tname
from teacher
where YEAR(GETDATE()) - age >1980
and prof like ‘副教授’
–9 查询编号为‘’()授课门数
select count (cno) 总和
from course
where tno=‘0128’
– alter table course
–alter column tno char(4) NULL
insert into course
values(‘19764228’,‘线性代数’,NULL,NULL)
–10 查询没有安排授课老师的课程信息
select *
from course
where tno is NULL

–二()在表score中插入数据,要求每个同学选修门课以上,每门课至少个同学选修。
insert into student values
(‘20197600’,‘张一’,‘男’,‘22’,‘003’,NULL),
(‘20197601’,‘张二’,‘男’,‘23’,‘003’,NULL),
(‘20197602’,‘张三’,‘男’,‘20’,‘003’,NULL)

update student
set specialty=‘The Internet of things engineering’
where sname=‘张三’

insert into course values
(‘08191455’,‘高等数学’,NULL,NULL),
(‘08191456’,‘线代’,NULL,NULL),
(‘08191457’,‘大学英语’,NULL,NULL)

insert into score values
(‘20197600’,‘08191455’,‘70’),
(‘20197600’,‘08191456’,‘71’),
(‘20197600’,‘08191457’,‘72’),
(‘20197601’,‘08191455’,‘80’),
(‘20197601’,‘08191456’,‘77’),
(‘20197601’,‘08191457’,‘60’),
(‘20197602’,‘08191455’,‘90’),
(‘20197602’,‘08191456’,‘84’),
(‘20197602’,‘08191457’,‘91’)

–2 查询至少选修了门课的同学的学号和选修课程门数。
select sno,COUNT(sno) 总门数
from score
group by sno
having COUNT(sno)>2

–3 查询学号为,102,103三位同学不及格课程门数,查询结果按照学号降序排列。
select distinct sno 学号,COUNT(sno) 总门数
from score
where grade <80
GROUP by sno
having sno like ‘%111’ or sno like ‘%144’ or sno like ‘%107’
order by sno desc
–(4)查询每个同学的学号、姓名、选修的课程名称、成绩、上课老师姓名,按照学号升序排列结果。
select s.sno,s.sname,c.cname,sc.grade,t.tname
from student s join score sc on s.sno=sc.sno
join course c on sc.cno=c.cno
join teacher t on c.tno=t.tno

order by s.sno asc
–查询《数据库课程设计》的间接先修课,要求出输出课程编号,课程名称,间接先修课的课程编号和名称。

select y.cno 课程号,y.cname 课程名称,x.cno 间接先修课的课程编号,x.cname 名称
from course x,course y
where y.cname=‘数据库课程设计’ and y.pcno=x.cno
–查询所有学生的选课情况(包括没有选课的学生)。
select *
from course
–3.查询每一个同学的学号、最好成绩、最差成绩、平均成绩。
select sno 学号,max(grade) 最好成绩,min(grade) 最差成绩,avg(grade) 平均成绩
from score
GROUP by sno
–查询最低分大于(),最高分小于的学生学号。
select sno 学号
from score
GROUP by sno
having max(grade)<90 and MIN(grade) >65
–查询所有同学的学号、姓名、最好成绩、最差成绩、平均成绩。
select s.sno 学号,s.sname 姓名,max(sc.grade) 最好成绩,min(sc.grade) 最差成绩,avg(sc.grade) 平均成绩
from score sc,student s
where sc.sno=s.sno
GROUP by sc.sno,s.sno,s.sname
–查询最低分大于,最高分小于的学生学号、姓名、班级。
select s.sno 学号,s.sname 姓名,depart 班级
from score sc,student s
where sc.sno=s.sno
GROUP by sc.sno,s.sno,s.sname,s.depart
having max(grade)<90 and MIN(grade) >65
–(4)查询选修’203’课程的成绩高于’103’号同学成绩的所有学生学号
select y.sno 学号
from score x,score y
where x.sno like ‘%101’ and y.grade>x.grade and y.sno not like ‘%101’
and x.cno like ‘%192’ and y.cno like ‘%192’
–(4)查询选修’203’课程的成绩高于’103’号同学成绩的所有学生学号,姓名
select sno 学生学号,sname 姓名
from student
where sno in(
select y.sno 学号
from score x,score y
where x.sno like ‘%101’ and y.grade>x.grade and y.sno not like ‘%101’
and x.cno like ‘%192’ and y.cno like ‘%192’
)
–(7)查询与”张三”同学同岁的所有学生的信息
select *
from student
where sname<>‘张三’ and age in(
select age
from student
where sname=‘张三’
)
–查询与"张三"同龄同班的学生姓名
select sname
from student
where sname<>‘张三’ and age in(
select age
from student
where sname=‘张三’
)
and specialty in(
select specialty
from student
where sname=‘张三’

)

–查询成绩比该课程平均成绩低的学生的学号,成绩
select x.sno 学生学号,x.cno 课程号
from score x
where x.grade <(

select avg(grade)
from score y
where x.cno=y.cno )

你可能感兴趣的:(数据库,sqlserver,mysql)