SQL server 复杂查询

1. 实验目的

    通过本次实验使学生掌握数据库中表数据的各种复杂查询操作。

2.实验内容

  1. 连接查询
  2. 嵌套查询
  3. 谓词查询

3.实验环境

  1. Windows
  2. SQL Server

实验步骤及结果

  1. 创建一个数据库,文件名为“教学”
  2. 打开“教学”数据库

Student 表

S#SnameAgeSsex
S1WANG15
S2LI17
S3LU19
S4ZHAO13
S5YANG20

Course表

C#CnameT#
C1MathsT1
C2DBT2
C3EnglishT3
C4ComputerT2
C5ChineseT2

SC表

S#C#Score
S1C150
S2C170
S3C180
S4C175
S5C187
S1C2
S2C2
S4C285
S1C3
S5C460
S4C445

Title表

T#TnameTitle
T1吴恩达教师
T2刘晓庆教授
T3张龙副教授

注明:表格和下面的代码可能不会一一对应,可能需要增加,删除,更改表中的数据

  1. 输入如下数据:

1、检索年龄小于17的女学生的学号和年龄

    select S#,sname
    	from Student
    	where age<17 and Ssex='女'
    go

2、检索男学生所学课程的课程号和成绩

    select distinct Student.s#,score
    	from Student,sc
    	where Ssex='男'
    go

3、检索男学生所学课程的任课老师的工号和姓名

    select distinct title.T#,Tname
    	from course,title,Student,sc
    	where course.t#=title.t# and 
    		Student.s#=sc.s# and course.C#=sc.c# 
    		and Student.Ssex='男'
    go

4、检索至少选修两门课的学生学号

    select distinct a.s#
    	from sc a,sc b
    	where a.s#=b.s# and a.c#<>b.c#
    go

5、检索至少有学号s2和s4学生选修的课程的课程号(两种方法解决)

    (1).
    select distinct a.c#
    	from sc a,sc b
    	where a.s#='s2' and b.s#='s4' and a.c#=b.c#
    go

    (2).
    select distinct C#
    	from sc
    	where c# 
    	in(
    		select c#
    		from sc
    		where s#='s2' )
    		and s#='s4'
    go

6、检索wang同学不学的课程的课程号

    select distinct C#
    	from c
    	where C# not in (		
    		select distinct c#
    			from sc
    			where s# in(
    			select s# 
    				from s
    				where sname='wang'))
	go

7、统计有学生选修的课程门数。

    select count(distinct course.c#) 选课人数
    	from course,Student,sc
    	where Student.s#=sc.s# and sc.c#=course.c#
    go

8、求选修C4课程的女学生的平均年龄。

select avg(AGE) 平均年龄
from Student, SC
where Student.S#=SC.S# and SC.C#='C4' 
and Ssex='女'

9、求LIU老师所授课程的每门课程的学生平均成绩。

select course.c#,avg(score) 平均成绩
	from sc,title,course
	where title.t#=course.t# and course.c#=sc.c# and tname='刘晓庆'
	group by course.c#
go

10、统计每门课程的学生选修人数(超过1人的课程才统计)。要求输出课程号和选修人数, 查询结果按人数降序排列,若人数相同,按课程号升序排列。

select c#,count (s#) 人数
	from sc
	group by c#
	having COUNT(*)>1
	order by 2 desc ,1

        order by 2 desc ,1中的“2”和“1”代表SC表中的第二列和第一列,如果写成C#,S#,编译器会报错。

11、检索学号比WANG同学大,而年龄比他小的学生姓名

select sname
	from Student
	where s#>all(select s#
		from Student
		where sname='wang')
		and age<all(select age
			from Student
			where sname='wang')
go

12、在SC中检索成绩为空值的学生学号和课程号。

select s#,c#
	from sc
	where score is null
go

13、检索姓名以L打头的所有学生的姓名和年龄。

select sname,age
	from Student
	where sname like 'l%'
go

14、 求年龄大于女同学平均年龄的男学生姓名和年龄。

select sname,age
	from Student
	where  Ssex='男'and age >(
	select avg (age)
		from Student
		where Ssex='女')
go

        我现在也是一名大三的学生,接触SQL Server的时间并不是很长,里面的代码难免会出错误,如果是引用数据错误,请读者们自己修改一下自己的代码,如果是我的语法和引用出错误,请大家给我在评论区留言,我看到并验证成功后我会改正自己的代码,写这个的目的也是为了同行的朋友们有一个借鉴和参考。

  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值