【SQL Server数据库】单表查询与多表查询(三)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41427568/article/details/102539133

本文衔接:【SQL Server数据库】建库、建表、简单查询语句(一) ,本文用于记录数据库实验内容。

单表查询

使用select语句对数据库中一张表进行查询:

/*求学院编号为'0001'的学生的学号、姓名、性别*/
select SNO,SNAME,SEX
	from student
		where DNO='0001'

/*求学院编号为'0001'的男生的学号、姓名、性别*/
select SNO,SNAME,SEX
	from student
		where DNO='0001' and SEX='男'
		
/*求选修授课班号为'327401'且成绩在 80~90 之间的学生学号和成绩,并将成绩乘以系数 0.8 输出,且
将 SNO 列更名为学号,成绩列更名为处理成绩 。*/
select SNO as 学号,GRADE*0.8 as 处理成绩
	from sc
		where GRADE between 80 and 90 and CNO='327401'

/*求每个学生的年龄,并输出姓名和年龄*/
select SNAME,AGE
	from student
--使用YEAR函数以及BIRTHDAY计算年龄
select SNAME as 姓名 ,YEAR(GETDATE())-YEAR(BIRTHDAY) as 年龄
	from student

/*寻找数据库中非空的AGE或者BIRTHDAY
select SNAME,AGE,BIRTHDAY
	from student
		where AGE is not null or BIRTHDAY is not null
*/

/*求选修了课程的学生的学号*/
select distinct SNO  --使用distinct去除重复的元素
	from sc
		where CNO is not null

/*求选修授课班号为’327401’的学生学号和成绩,并要求对查询结果按成绩的降序排列,如果成绩相
同则按学号的升序排列*/
select SNO,GRADE
	from sc
		where CNO='327401'
			order by GRADE desc,SNO asc --使用order by进行排序,DESC表示降序,ASC表示升序

/*求缺少了成绩的学生学号和课程号。*/
select SNO,CNO
	from sc
		where GRADE is null

/*统计选课学生人数及最高分成绩和最低分成绩*/
select COUNT(distinct SNO) as 总选课人数,MAX(GRADE) as 最高成绩,MIN(GRADE) as 最低成绩
	from sc

/*求学院编号为’0001’或‘0002‘中姓张的学生的信息。*/
select * 
	from student
		where (DNO='0001' or DNO='0002') and SNAME like '张%'

/*求姓名中包含’丽’的学生信息*/
select *
	from student
		where SNAME like '%丽%'
		
/*求姓名只有两个字,且第二个字为’丽’的学生信息*/
select *
	from student
		where SNAME like '_丽'
		
/*求信息学院计算机专业的学生名单*/
select *
	from student
		where SUBSTRING(SNO,4,2)='10' --SUBSTRING函数用于切割字符串
		--第一个参数是被检测的属性(需要是字符串类型),第二个参数是起始位置,第三个参数是从起始位置到目标位置的字符数
		
/*统计各个学院的人数*/
select DNO as 学院编号,COUNT(SNO) as 学院人数
	from student
		group by DNO --使用group by来对属性分组,属性中相同的元素放在一组,聚集函数(Aggregate functions)根据分组进行分组计算

/*按授课班号统计选修该课程的人数,并按照人数升序排列。*/
select CNO,COUNT(SNO) as 选修人数
	from sc
		group by CNO
			order by COUNT(SNO) asc

/*统计平均成绩超过 80 分的学生的学号及平均成绩*/
select SNO,AVG(GRADE)
	from sc
		group by SNO
			having AVG(GRADE)>= 80 --使用having来限制group by之后的数据
				order by AVG(GRADE) desc
			
/*求选修课程超过 6 门课的学生学号,并按选修课程数目升序排列。*/
select SNO,COUNT(CNO)
	from sc
		group by SNO
			having COUNT(CNO)>6
				order by COUNT(CNO) asc
				
/*求每个学院学生的平均年龄,并把结果存入当前数据库‘系平均年龄’临时表中。*/
select DNO,AVG(AGE) as 平均 INTO 系平均年龄 --使用INTO语句将select出来的属性作为新表存入
	from student
		group by DNO 
		
/*分页浏览数据方法:*/
/*(1)查询student表中第1-10名同学的信息*/
select top 10 *
	from student
/*(2)查询student表中第11-20名同学的信息*/
select top 10 *
	from student
		where SNO not in (select top 10 SNO 
						  from student)

/*查询‘1987-1-1’号以后出生的女生的学生信息*/
select *
	from student
		where BIRTHDAY>'1987-1-1' and SEX='女'

go

/*创建'计算机系学生'视图,用于浏览计算机系学生的学号、姓名和年龄*/
create view 计算机系学生
	as select SNO,SNAME,AGE 
		from student
			where SUBSTRING(SNO,4,2)='10'
go

/*查询分数在70和90之间的学生学号*/
select SNO
	from sc
		group by SNO
			having MIN(GRADE)>70 and MAX(GRADE)<90
	
/*查询少于 10 名同学选修的授课班号*/		
select CNO
	from sc
		group by CNO
			having COUNT(SNO)<10
				order by CNO
				
/*查询选课表中的最高分*/
select MAX(GRADE) as 最高分
	from sc
	
/*查询授课编号为‘153701’的课程的平均分*/
select AVG(GRADE) as 课程平均分
	from sc
		where CNO='153701'

/*查询课程平均分超过 85 的授课班号,输出结果按课程平均分升序排列*/
select CNO,AVG(GRADE)
	from sc
		group by CNO
			having AVG(GRADE)>85
				order by AVG(GRADE) asc
				
/*查询课程名称为’线性代数’的排课情况*/
select *
	from course
		where CNAME='线性代数'
		
/*查询授课班号为‘218801’的学生学号*/
select SNO
	from sc
		where CNO='218801'
			order by SNO

/*按授课班号查询课程的平均分,输出授课班号和平均成绩*/
select CNO,AVG(GRADE)
	from sc
		group by CNO
			order by AVG(GRADE)
			
/*在 sc 中输出成绩在 90-100 之间的学生信息*/
select *
	from sc
		where GRADE between 90 and 100
		
--或者使用CONVERT函数将GRADE转化成字符串,然后匹配'9%'\
select *
	from sc
		where CONVERT(char(20),GRADE) like '9%'
		
/*查询‘周芬’老师,这个学期的上课安排情况*/
select *
	from course
		where TNAME='周芬'
		
/*查询姓‘周’的教师的排课情况*/
select *
	from course
		where TNAME like '周%'

/*按教室分组统计排课门数情况*/
select ROOM,COUNT(CNO) as 排课门数
	from course
		group by ROOM

/*查询排课门数超过 8 门的教室名单及其排课门数*/
select ROOM,COUNT(CNO)
	from course
		group by ROOM
			having COUNT(CNO)>8
go

/*创建机电学院女生的视图*/
create view 机电学院女生
as select SNO,SNAME
	from student,dept
		where student.DNO=dept.DNO 
		and SEX='女'
		and DNAME like '机电%'
go

/*查询学分超过 4 分的课程,输出课程名和学分,并要求按学分升序*/
select CNAME,CREDIT
	from course
		where CREDIT>4 
			order by CREDIT asc
			
/*按教室明细并汇总排课情况*/
select ROOM,COUNT(CNO) as 排课情况
	from course
		group by ROOM
			order by 排课情况 desc
			
/*查询课程号为“203402”的成绩最高的前 5 名学生的学号及成绩,结果按成绩降序*/
select top 5 SNO,GRADE
	from sc
		where CNO='203402'
			order by GRADE desc
			
/*查询年龄小于 20 岁的学生学号*/
select SNAME
	from student
		where AGE<20

--或者使用BIRTHDAY计算年龄
select SNAME
	from student
		where YEAR(GETDATE())- YEAR(BIRTHDAY)<20

/*查询有 90 人以上选修的课程号*/
select CNO,COUNT(SNO) as 选课人数
	from sc
		group by CNO
			having COUNT(SNO)>90

/*查询全体男生的姓名,要求查询结果按所在系升序排列,对相同系的学生按姓名升序排列*/
select SNAME
	from student
		order by DNO asc,SNAME asc

/*查询成绩在 70-90 范围内的学生学号*/
select SNO
	from sc
		where GRADE between 70 and 90

多表查询

在大多数情况下,我们需要多张表进行查询:

/*求学号为‘20022037’的同学的每门课的成绩,输出格式为:学号,课程名,课程成绩*/
select SNO as 学号,CNAME as 课程名,GRADE as 课程成绩
from sc,course
where SNO='20022037'
	  and sc.CNO=course.CNO
/*也可以使用连接表达式实现查询*/
select SNO as 学号,CNAME as 课程名,GRADE as 课程成绩
from sc inner join course on sc.CNO=course.CNO
where SNO='20022037'

/*查询每个学生的每门课程的成绩,要求输出学号,课程名,成绩*/
select SNO as 学号,CNAME as 课程名,GRADE as 成绩
from sc,course
where sc.CNO=course.CNO
/*也可以使用连接表达式实现查询*/
select SNO as 学号,CNAME as 课程名,GRADE as 成绩
from sc inner join course on sc.CNO=course.CNO

/*查询每个学生的每门课程的成绩,要求输出学号,姓名,课程号,成绩*/
select sc.SNO,SNAME,CNO,GRADE
from sc,student
where sc.SNO=student.SNO
/*也可以使用连接查询实现*/
select sc.SNO,SNAME,CNO,GRADE
from sc inner join student on sc.SNO=student.SNO

/*查询选修了'线性代数'课程的学生学号、姓名*/
select sc.SNO,SNAME
from sc,student,course
where sc.SNO=student.SNO
	  and sc.CNO=course.CNO
	  and CNAME='线性代数'

/*查询'线性代数'的所有授课班级的平均成绩,并列出授课班号、教师名、平均成绩,
且按平均成绩排序*/
select course.CNO,TNAME,AVG(GRADE)
from sc,course
where sc.CNO=course.CNO
	  and CNAME='线性代数'
	  group by course.CNO,course.TNAME
	  order by AVG(GRADE) desc

/*使用多表连接方法,查询和学号为‘20000156’的同学同年同月同日出生的所有学生
的学号、姓名、生日。*/
select stu2.SNO,stu2.SNAME,stu2.BIRTHDAY
from student as stu1,student as stu2
where stu1.BIRTHDAY=stu1.BIRTHDAY 
	  and stu1.SNO='20000156'

/*使用嵌套查询方法,查询和学号为‘20000156’的同学同年同月出生的所有学生的学
号、姓名、生日。*/
select SNO,SNAME,BIRTHDAY
from student
where YEAR(BIRTHDAY)=(select YEAR(BIRTHDAY)
					  from student
					  where SNO='20000156') and
	  MONTH(BIRTHDAY)=(select MONTH(BIRTHDAY)
					   from student
					   where SNO='20000156')

/*使用嵌套查询方法,查询“赵蓉”教师任课的学生成绩,并按成绩递增排列*/
select GRADE
from sc
where CNO in (select CNO
			  from course
			  where TNAME='赵蓉')
order by GRADE asc

/*使用嵌套查询方法,查询课程最低分大于 70,最高分小于 90 的学生学号和姓名*/
select SNO,SNAME
from student
where SNO in (select SNO
			  from sc
			  group by SNO
			  having MIN(GRADE) > 70 and MAX(GRADE) < 90)

/*用嵌套法查询选修了“线性代数“的学生学号和姓名*/
select SNO,SNAME
from student
where SNO in (select SNO
			  from sc
			  where CNO in (select CNO
							from course
							where CNAME='线性代数'))

/*从选修’218801’课程的同学中,选出成绩高于’季莹’的学生的学号和成绩*/
select SNO,GRADE
from sc
where GRADE >(select GRADE
			  from sc
			  where CNO='218801' 
			  and SNO=(select SNO
					   from student
					   where SNAME='季莹'))
	  and CNO='218801'

/*查询成绩比该课程平均成绩低的学生成绩表*/
select SNO,CNO,GRADE
from sc as a
where GRADE<(select AVG(GRADE)
			 from sc as b
			 group by CNO
			 having a.CNO=b.CNO)

/*查询选修了'线性代数'这门课程的学生学号*/
select SNO
from sc
where CNO in (select CNO
			 from course
			 where CNAME='线性代数')
/*或者使用exists进行查询*/
select SNO
from sc
where exists(select *
			 from course
			 where sc.CNO=course.CNO
			 and CNAME='线性代数')

/*查询所有学生都选修的课程名*/
/*问题相当于:对于这一门选修,不存在有的学生没有选的*/
select CNAME
from course
where not exists(select *
				 from student
				 where not exists(select *
								  from sc
								  where sc.CNO=course.CNO
								  and SNO=student.SNO))
								  
/*查询选修了'线性代数'课程或'英语口语'课程的学生学号、姓名。*/
select distinct SNO,SNAME
from student
where SNO in (select SNO
			  from sc
			  where CNO in (select CNO
							from course
							where CNAME='线性代数'
							or CNAME='英语口语'))
--或者:
select distinct student.SNO,student.SNAME
from student,sc,course
where student.SNO=sc.SNO 
	  and sc.CNO=course.CNO
	  and (CNAME='线性代数' or CNAME='英语口语')
	  
/*用集合操作符 UNION 查询选修了'线性代数'课程或'英语口语'课程的学生学号、姓名。*/
select student.SNO,student.SNAME
from student,sc,course
where student.SNO=sc.SNO
	  and sc.CNO=course.CNO
	  and CNAME='线性代数'
union
select student.SNO,student.SNAME
from student,sc,course
where student.SNO=sc.SNO
	  and sc.CNO=course.CNO
	  and CNAME='英语口语'

/*查询选修了'218801'课程但没有选修'216301'课程的学生学号、姓名。*/
/*使用集合差except实现*/
select sc.SNO,SNAME
from sc,student
where student.SNO=sc.SNO and CNO='218801'
except
select sc.SNO,SNAME
from sc,student
where student.SNO=sc.SNO and CNO='216301'

/*也可以使用子查询实现:*/
select SNO,SNAME
from student
where SNO in(
select SNO
from sc
where CNO='218801')
and SNO not in(
select SNO
from sc
where CNO='216301')

/*求同时选修'218801'课程和'216301'课程的学生学号、姓名。*/
select SNO,SNAME
from student
where SNO in (select SNO 
			  from sc
			  where CNO='218801')
	  and SNO in (select SNO
				  from sc
				  where CNO='216301')

/*可以用集合的交计算*/
select  student.SNO,SNAME
from student,sc
where sc.SNO=student.SNO
	  and CNO='218801'
intersect
select  student.SNO,SNAME
from student,sc
where sc.SNO=student.SNO
	  and CNO='216301'

/*查询所有学生及其选课信息*/
select student.SNO,SNAME,CNO,GRADE
from student,sc
where student.SNO=sc.SNO

/*可以使用左外连接解决*/
select student.SNO,SNAME,CNO,GRADE
from student left outer join sc
on student.SNO=sc.SNO
go

/*创建课程平均分视图*/
create view 课程平均分视图 as 
select CNAME,AVG(GRADE) as 平均分
from sc,course
where sc.CNO=course.CNO
group by CNAME
go

/*以列的方式统计每门课程的分数段人数。分数段为:不及格、60-70、70-80、80-90、90-100*/
select CNAME,'不及格' as '分数段',COUNT(SNO) as '人数' 
from sc,course
where sc.CNO=course.CNO
	  and GRADE<60
group by CNAME
union
select CNAME,'60-70' as '分数段',COUNT(SNO) as '人数' 
from sc,course
where sc.CNO=course.CNO
	  and GRADE between 60 and 70 
group by CNAME
union
select CNAME,'70-80' as '分数段',COUNT(SNO) as '人数' 
from sc,course
where sc.CNO=course.CNO
	  and GRADE between 70 and 80 
group by CNAME
union
select CNAME,'80-90' as '分数段',COUNT(SNO) as '人数' 
from sc,course
where sc.CNO=course.CNO
	  and GRADE between 80 and 90 
group by CNAME
union
select CNAME,'90-100' as '分数段',COUNT(SNO) as '人数' 
from sc,course
where sc.CNO=course.CNO
	  and GRADE between 90 and 100 
group by CNAME

/*查询所有选课学生的姓名*/
select SNAME
from student
where SNO in(select SNO 
			 from sc)
/*使用exists*/
select SNAME
from student as a
where exists(select *
			 from sc
			 where a.SNO=sc.SNO)
			
/*查询所有未选课的学生的姓名*/
select SNAME
from student
where SNO not in (select SNO
				  from sc)
/*使用not exists*/
select SNAME
from student as a
where not exists(select *
				 from sc
				 where a.SNO=sc.SNO)
				 
/*按学生分类查询其选修课程的平均分,输出学号、姓名和平均成绩*/
select student.SNO,SNAME,AVG(GRADE)
from student,sc
where student.SNO=sc.SNO
group by student.SNO,SNAME

/*查询所有课程的平均分,输出课程名和平均成绩,并按平均成绩递增*/
select CNAME,AVG(GRADE)
from course,sc
where course.CNO=sc.CNO
group by CNAME
order by AVG(GRADE) asc

/*查询少于 10 名同学选修的课程名称,授课班号,教师名,选课人数*/
select CNAME,course.CNO,TNAME,COUNT(*)
from course,sc
where course.CNO=sc.CNO
group by CNAME,course.CNO,TNAME
having COUNT(course.CNO)<10
order by course.CNO

/*按学号显示信息学院,‘通信专业’或‘电子科学专业’的每个学生的每门课程的成绩
明细,并统计每个学生的总成绩,平均成绩*/
select SNO,CNO,GRADE
from sc
where SUBSTRING(SNO,5,2) in ('22','24')
order by SNO
compute SUM(GRADE),AVG(GRADE),MAX(GRADE) by SNO

/*统计每门课的不及格人数,列出课程名和不及格人数*/
select CNAME,COUNT(SNO)
from sc,course
where sc.CNO=course.CNO
	  and GRADE<60
group by CNAME

/*使用嵌套方法查询存在有 95 分以上成绩的课程 CNO*/
select CNO,CNAME
from course
where CNO in (select distinct CNO
from sc
where GRADE>95)

/*查询成绩比该课程平均成绩低的学生成绩表*/
select *
from sc as sc1
where GRADE<(select AVG(GRADE)
			 from sc as sc2
			 where sc1.CNO=sc2.CNO
			 group by CNO)
/*或者使用exists语句*/
select *
from sc as sc1
where exists(select CNO 
			 from sc as sc2
			 where sc1.CNO=sc2.CNO
			 group by CNO
			 having sc1.GRADE<AVG(GRADE))

/*按课程名称统计每一门课程的平均分,输出课程名称和平均分*/
select CNAME,AVG(GRADE) as 平均分
from course,sc
where course.CNO=sc.CNO
group by CNAME

/*按学生姓名统计其选修课程的总学分,输出学生姓名和总学分*/
select SNAME,SUM(CREDIT) as 总学分
from student,sc,course
where student.SNO=sc.SNO
and sc.CNO=course.CNO 
group by SNAME

/*查询同时选修了‘203402’和‘244501’课程的同学名称*/
select SNAME
from student
where SNO in (select SNO
			  from sc
			  where CNO='203402')
	  and SNO in (select SNO
			  from sc
			  where CNO='244501')

/*可以用集合的交计算*/
select SNAME
from student,sc
where student.SNO=sc.SNO
and CNO='203402'
intersect
select SNAME
from student,sc
where student.SNO=sc.SNO
and CNO='244501'

/*求最高分学生的学号*/
select SNO,CNO,GRADE
from sc as sc1
where GRADE = (select MAX(GRADE)
			   from sc as sc2
			   where sc1.CNO=sc2.CNO
			   group by sc2.CNO)

/*查询“线性代数”的所有授课班级的平均成绩,列出课程名和平均成绩*/
select '线性代数' as 课程名,sc.CNO,AVG(GRADE)
from sc,course
where sc.CNO=course.CNO
	  and CNAME='线性代数'
group by sc.CNO

/*查询“线性代数”成绩最高的前 5 名学生的姓名及成绩,结果按成绩降序*/
select top 5 SNAME,GRADE
from sc,student,course
where sc.CNO=course.CNO
	  and sc.SNO=student.SNO
	  and CNAME='线性代数'
order by GRADE desc

/*查询学生“20002059”选修课程的总学分数*/
select SUM(CREDIT)
from sc,course
where sc.CNO=course.CNO
	  and SNO='20002059'

/*对每个同学,查找其获得最高成绩的课程号*/
select SNO,CNO,GRADE
from sc as sc1
where exists(select sc2.SNO
			 from sc as sc2
			 where sc1.SNO=sc2.SNO
			 group by sc2.SNO
			 having sc1.GRADE=MAX(GRADE))
order by SNO

猜你喜欢

转载自blog.csdn.net/qq_41427568/article/details/102539133