sqlserver查询语句实例

sqlserver查询语句,用案例说话

(1)用SQL语句表示下列操作,在学生库中实现数据查询。
①求数学系学生的学号和姓名。
②求选修了课程的学生学号。
③求选修001号课程的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。
④求选修课程001且成绩在80~90分之间的学生学号和成绩,并将成绩乘以系数0.8输出。
⑤求数学系或计算机系姓张的学生的信息。
⑥查看选修了课程、但没有成绩学生的学号、姓名、课程号和所在是系部。
⑦查询学生的学号、姓名、课程名和成绩。
⑧分别实现学生和系的交叉连接、内连接、外连接。

1select SNo,SName from student where SDept = (
	select DNo from Department where DName = '数学系'
)

2select SNo from SCource where CNo is not null;

3select SNo,Grade from SCource where CNo=1 order by SNo,Grade DESC;

4select SNo,Grade*0.8 from SCource where CNo=1

5select * from Student where SDept in (
	select DNo from Department where DName = '数学系' or DName = '计算机系'
) and SName like '张%'

6.select SCource.SNo,SName,CNo,SDept from Student,SCource where Student.SNo in (
	select Student.SNo from SCource where CNo is not null and Grade is null
)

7select SCource.SNo,SName,CName,SCource.Grade from Student,SCource,Cource

8select Student.*,Department.* from Department cross join Student;
select Student.*,Department.* from student inner join Department on Student.SDept = Department.DNo;
select Student.*,Department.* from  Student left outer join Department on Student.SDept = Department.DNo;

(2)在SQL Server查询分析器中使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。具体内容如下:
用SQL语句表示,在学生选课库中实现其数据嵌套查询操作。
①求选修了高等数学的学生学号和姓名。
②求001课程的成绩高于张力的学生学号和成绩。
③求其他系中年龄小于计算机系年龄最大者的学生。
④求其他系中比计算机系学生年龄都小的学生。
⑤求选修了001课程的学生姓名。
⑥求没有选修001课程的学生姓名。
⑦查询选修了全部课程的学生的姓名。

select SNo,SName from Student where SNo in(
	select SNo from SCource where CNo in(
		select CNo from Cource where CName = '高等数学'
	)
)

select SCource.SNo,Grade from SCource,Student where SCource.CNo = 1 and SCource.Grade > (
	select Grade from SCource where SNo = (
			select SNo from Student where SName = '张力'
	)
)

select SName,SAge,SDept from Student where SDept<>(
	select DNo from Department where DName = '计算机工程系'
) and SAge < any (
	select SAge from Student where SDept=(
		select DNo from Department where DName = '计算机工程系'
	)
)

select SName,SAge,SDept from Student where SDept<>(
	select DNo from Department where DName = '计算机工程系'
) and SAge < all (
	select SAge from Student where SDept=(
		select DNo from Department where DName = '计算机工程系'
	)
)

select SName from Student,SCource where Student.SNo = SCource.SNo and CNo='1'

select SName from Student where not exists (
	select * from SCource where SNo=Student.SNo and CNo='1'
)

select SName from Student where not exists(
	select * from Cource where not exists(
		select * from SCource where SNo=Student.SNo and CNo = Cource.CNo
	)
)

发布了8 篇原创文章 · 获赞 10 · 访问量 2579

猜你喜欢

转载自blog.csdn.net/m0_46493091/article/details/105373582