实验四、复杂查询

实验目的

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


实验内容

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);
    

    在这里插入图片描述


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

### SQL 实验复杂查询解决方案 在处理复杂SQL 查询时,通常需要综合运用多种技术手段来提高效率并减少资源消耗。以下是针对复杂查询的一些常见示例及其解决方案。 #### 示例一:通过索引来优化查询性能 对于涉及大量数据的表,在 `WHERE` 和 `ORDER BY` 子句中的字段上创建合适的索引可以显著提升查询速度[^3]。例如: 假设有一个名为 `employees` 的表,其中包含员工的信息以及他们的部门编号 (`department_id`) 和入职日期 (`hire_date`)。如果经常执行如下查询: ```sql SELECT * FROM employees WHERE department_id = 5 ORDER BY hire_date DESC; ``` 可以通过为 `department_id` 和 `hire_date` 创建复合索引来加速此操作: ```sql CREATE INDEX idx_emp_dept_hire ON employees(department_id, hire_date); ``` 这样可以在过滤条件和排序过程中利用索引结构,从而避免全表扫描。 #### 示例二:分组统计与聚合函数的应用 当需要计算某个特定时间段内的销售总额或者平均订单金额时,可以使用 GROUP BY 结合 SUM() 或 AVG() 函数完成这些任务。比如有张记录商品交易详情的数据表叫做 orders (order_id, product_name, quantity, price),那么求每种产品的总销量可写成下面形式: ```sql SELECT product_name, SUM(quantity*price) AS total_sales FROM orders GROUP BY product_name; ``` 这里我们不仅实现了按产品名称汇总销售额的功能,还展示了如何正确地组合多个列来进行数值运算后再做进一步分析[^1]。 #### 示例三:子查询与连接操作相结合解决问题 有时候单靠简单的 SELECT 不足以满足业务需求,则可能需要用到更高级别的语法——嵌套查询或者是 JOIN 。举个例子来说吧 ,如果我们想知道哪些客户在过去一个月里至少购买过一次我们的旗舰型号手机,并且他们所在城市的人口数大于一百万的话,就可以按照这种方式构建语句: ```sql SELECT c.customer_id, c.name FROM customers c INNER JOIN ( SELECT DISTINCT customer_id FROM purchases p WHERE purchase_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND item='flagship_phone' ) recent_purchases rp ON c.customer_id=rp.customer_id INNER JOIN cities ci ON c.city_id=ci.id WHERE ci.population > 1000000; ``` 上述代码片段首先筛选出了符合条件的商品最近购入者列表作为内部临时结果集;接着再把这个集合跟顾客基本信息表进行匹配找到对应的名字等属性;最后再次关联城市资料确认居住环境规模是否达标[^2]。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lilianac

你的鼓励是我的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值