实验四、复杂查询

实验目的

掌握两个表以上的连接查询的应用,包括嵌套查询。


实验内容

1.同一数据库中的多表查询

  1. 查询比“林红”年纪大的男学生信息。

    select * from Student 
    where Sex = '男' and Birth < (
    	select Birth 
    	from Student
    	where Sname = '林红'
    );
    

    在这里插入图片描述

  2. 查询所有学生的选课信息,包括学号、姓名、课号、课程名、成绩。

    select t1.Sno 学号,t1.Sname 姓名,t2.Cno 课号, t2.Cname 课程名, t3.Grade 成绩 
    from Student t1,Course t2,SC t3 
    where t1.Sno=t3.Sno and t2.Cno=t3.Cno;
    

    在这里插入图片描述

  3. 查询已选课学生的学号、姓名、课程名、成绩。

    select t1.Sno 学号,t1.Sname 姓名,t2.Cname 课程名,t3.Grade 成绩
    from SC t3
    left outer join Student t1 on (t1.Sno=t3.Sno),Course t2
    where t2.Cno=t3.Cno;
    

    在这里插入图片描述

  4. 查询选修了“C语言程序设计”的学生的学号和姓名。

    select Sno 学号,Sname 姓名
    from Student
    where Sno in(
    	select Sno
    	from SC
    	where Cno in(
    		select Cno
    		from Course
    		where Cname='C语言程序设计'
    		)
    	);
    

    在这里插入图片描述

  5. 查询与“张虹”在同一个班级的学生学号、姓名、家庭住址。

    select Sno 学号,Sname 姓名,Home_addr 家庭住址
    from Student
    where Classno=(
    	select Classno
    	from Student
    	where Sname='张虹'
    	);
    

    在这里插入图片描述

  6. 查询其他班级中比“051”班所有学生年龄大的学生的学号、姓名。

    select Sno,Sname
    from Student
    where Classno <>'051'
    	and Birth < all(
    		select Birth
    		from Student
    		where Classno='051');
    

    在这里插入图片描述

  7. (选做)查询选修了全部课程的学生姓名。
    将题目意思转换为等价的用存在量词的形式,即 查询这样一个学生,没有一门课程中没有他的名字

    select Sname
    from Student
    where not exists(
    	select *
    	from Course
    	where not exists(
    		select *
    		from SC
    		where Student.Sno=SC.Sno and Course.Cno=SC.Cno
    		)
    	)
    

    在这里插入图片描述

  8. (选做)查询至少选修了学生“20110002”选修的全部课程的学生的学号、姓名。
    将题目的意思转换为等价的用存在量词的形式: 不存在这样的学生,学生’20110002’选修了一些课程y,而学生x没有选修这些课程y

    select distinct sx.Sno,Student.Sname
    from SC sx,Student
    where sx.Sno=Student.Sno and
    	not exists(
    		select * from SC sy
    		where sy.Sno='20110002' and
    			not exists(
    				select * from SC sz
    				where sz.Sno=sx.Sno and sz.Cno=sy.Cno
    				)
    	);
    

    在这里插入图片描述

  9. 查询学生的学号、姓名、学习课程名及课程成绩。

    select Student.Sno,Sname,Cname,Grade
    from Student,Course,SC
    where Student.Sno=SC.Sno and Course.Cno=SC.Cno;
    

    在这里插入图片描述

  10. 查询选修了“高数”课且成绩至少高于选修课程号为“002”课程的学生的学号、课程号、成绩,并按成绩从高到低次序排列。

    select Sno,SC.Cno,Grade
    from SC
    where Cno in(
    		select Cno from Course
    		where Cname='高数'
    		)
    	and Grade>all(
    		select Grade
    		from SC
    		where Cno='002')
    order by Grade desc;
    

    在这里插入图片描述

  11. 查询选修3门以上课程的学生的学号、总成绩(不统计不及格的课程),并要求按总成绩的降序排列出来。

    select Sno 学号,sum(case when Grade>=60 then Grade else 0 end) 总成绩
    from SC
    where Sno in(
    	select Sno from SC group by Sno having count(*)>3
    	)
    group by Sno
    order by 总成绩 desc;
    

    在这里插入图片描述

  12. 查询多于3名学生选修的并以3结尾的课程号的平均成绩。

    select Cno 课程号, avg(Grade) 平均成绩
    from SC
    where Cno in(
    	select Cno from SC group by Cno having count(*)>3
    	) and Cno='%3'
    group by Cno;
    
  13. 查询最高分与最低分之差大于5分的学生的学号、姓名、最高分、最低分。

    select SC.Sno 学号,Sname 姓名,max(Grade) 最高分,min(Grade) 最低分
    from SC,Student
    where SC.Sno in (
    	select Sno from SC
    	group by Sno
    	having count(*)>2 and (max(Grade)-min(Grade))>5
    	) and Student.Sno=SC.Sno
    group by SC.Sno,Sname;
    

    在这里插入图片描述

  14. 创建一个表student_other,结构同Student,输入若干记录,部分记录和Student表中的相同。

    create table student_other
    ( 
    	Sno char(8) not null primary key,
    	Sname varchar(8) not null,
    	Sex char(2) not null default '男',
    	Birth smalldatetime not null,
    	Classno char(3) not null,
    	Entrance_date smalldatetime not null,
    	Home_addr varchar(40),
    	Sdept char(20) not null,
    	Postcode char(6),
    	check(Postcode like'[0-9][0-9][0-9][0-9][0-9][0-9]')
    )
    insert into student_other (Sno,Sname,Sex,Birth,Classno,Entrance_date,Home_addr,Sdept,Postcode)
    values('20110001','张虹','男','1992-09-11','051','2011-09-01','南京','计算机系','200413'),
    ('20110002','林红','女','1991-11-12','051','2011-09-01','北京','计算机系','100010'),
    ('20110103','赵清','男','1993-05-11','061','2011-09-01','上海','软件工程','200013');
    

    a.查询同时出现在Student表和student_other表中的记录。

    select * from Student intersect
    select * from student_other;
    

    在这里插入图片描述

    b.查询Student表和student_other表中的全部记录。

    select * from Student
    select * from student_other
    

    在这里插入图片描述

2.多个数据库间的多表查询

(选做)创建一个数据库student_info_other,参数自定。

create database student_info_other;
  1. 当前数据库为Student_info,将student_info数据库中的表student_other复制到student_info_other中。

    select * into student_info_other.dbo.student_other1
    from student_info.dbo.student_other;
    

    在这里插入图片描述

  2. 查询同时出现在Student表和student_info_other数据库student_other表中的记录。

    select * from Student intersect
    select * from student_info_other.dbo.student_other1;
    

    在这里插入图片描述

3.外连接查询

  1. 查询所有课程信息及其选课信息,包含未被学生选修的课程。

    select * from Course
    left outer join SC on(SC.Cno=Course.Cno);
    

    在这里插入图片描述

  2. 查询所有学生信息,所有课程信息及其选课信息,包含未选修课程的学生及未被学生选修的课程。

    select * from Student
    left join SC on(SC.Sno=Student.Sno)
    left join Course on(SC.Cno=Course.Cno);
    

    在这里插入图片描述


注:为了能更明显地显示出查询语句是否正确,我在中途自己添加了很多数据,所以前后查询应该显示的结果可能会有出入

一、实验目的 1.掌握SQL Server 2005的安装。 2.掌握SQL Server Management Studio的启动和使用。 3.掌握SQL Server 2005服务器的配置和注册。 4.掌握SQL Server 2005查询的基本使用。 5.掌握应用SQL Server Management Studio创建数据库的方法。 6.掌握应用SQL Server Management Studio修改和查看数据库的方法。 7.掌握应用SQL Server Management Studio删除数据库的方法。 8.掌握应用Transact-SQL语句创建数据库的方法。 9.掌握应用Transact-SQL修改和查看数据库的方法。 10.掌握通过Transact-SQL删除数据库的方法。 11.掌握SQL Server 2005数据库和操作系统物理文件的关系。 12.掌握数据库的分离和附加方法。 二、实验内容 1.完成SQL Server 2005开发版的安装。 提示:若计算机系统中已经安装有SQL Server 2005系统,则在安装时需要选择安装命名实例。安装过程中身份验证模式选择“混合模式”并设置sa账户的密码。 2.利用SQL Server配置管理器启动、停止SQL Server服务(包括默认实例和命名实例),配置SQL Server服务为自动启动。 3.利用SQL Server配置管理器配置进行SQL Server 2005网络配置,启用默认实例和命名实例的TCP/IP协议。 4.利用SQL Server外围配置器配置数据库引擎的服务及远程连接,设置为“本地连接和远程连接”,选择“同时使用TCP/IP和named pipes”。 5.利用SQL Server Management Studio注册安装的命名实例。 6.利用SQL Server Management Studio注册远程服务器。 提示:注册远程服务器时需要使用混合验证模式,利用sa账户和密码登录远程服务器。 7.启动SQL Server Management Studio,连接到服务器。新建一个查询,在其中输入如下代码: DECLARE @position int, @string char(5) SET @position = 1 SET @string = 'China' WHILE @position <= DATALENGTH(@string) BEGIN SELECT SUBSTRING(@string, @position, 1) 字符, ASCII(SUBSTRING(@string, @position, 1)) ASCII码 SET @position = @position + 1 END
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lilianac

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值