【SQL】实验六 带函数查询和综合查询(1)

本文主要介绍如何使用SQL进行带函数查询和综合查询,包括统计年龄、成绩分布、课程最高分、学生选课情况等。通过实例演示TOP1与MAX()函数的区别,以及解决在查询过程中遇到的问题,如聚合函数与WHERE子句的使用、无主键表的GROUP BY操作等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实验目的

1.掌握Management Studio的使用。

2.掌握带函数查询和综合查询的使用。

二、实验内容及要求

1.统计年龄大于30岁的学生的人数。

select  count(*)  as 人数

  from Student

  where(year(gatdate())-year(Birthday))>30

2.统计数据结构有多少人80分或以上。

select count(*) as 人数
  from StudentGrade
  where Grade>=80 and Course_id in(select StudentGrade.Course_id 
                                     from Course,StudentGrade
                                     where StudentGrade.Course_id=Course.Course_id and 
                                           Course_name='数据结构')

3.查询“0203”课程的最高分的学生的学号。(请分别用TOP1和函数来进行查询,并分析它们的区别)

TOP1 

select  top 1 Stu_id 
  from StudentGrade
  where Course_id='0203'

函数MAX()

select Stu_id
  from StudentGrade
  where Course_id='0203' and Grade=(
   select Max(Grade) from StudentGrade )

4.统计各系开设班级的数目(系名称、班级数目),并创建结果表。(需考虑没有班级的系)

select Depar_name as 系名称,count(*) as 班级数目 into new
from Deparment group by Depar_name order by 班级数目 asc

打开new表查看如下 

 

5.选修了以“01”开头的课程的学生学号,姓名,选课的课程号。

select StudentGrade.Stu_id as 学生学号,Stu_name as 学生姓名,Course_id as 选课的课程号
from StudentGrade,Student
where Course_id like '01%'and StudentGrade.Stu_id=Student.Stu_id 

6.统计每科目的最高分、最低分,平均分、总分,并以中文列名显示。

select Course_name as 课程,MAX(Grade) as 最高分,MIN(Grade) as 最低分,SUM(Grade)/count(*) as 平均分,SUM(Grade) as 总分
from Course,StudentGrade
where Course.Course_id=StudentGrade.Course_id
group by Course_name order by 最高分 desc

7.所有成绩都在70分以上的学生姓名(提示:使用子查询。需考虑未选课的学生)。

使用聚合函数min()

select Student.Stu_id,Stu_name--防止重名
from Student,StudentGrade
where Student.Stu_id=StudentGrade.Stu_id and min(Grade)>70

使用子查询 

select Stu_name as 所有成绩都在70分以上的学生姓名
from Student,StudentGrade
where Student.Stu_id in(select distinct Stu_id from StudentGrade where Grade>70) and Student.Stu_id=StudentGrade.Stu_id 

8.“数据库”课程得最高分的学生的学号、姓名和所在系(提示:使用子查询)。

select  top 1 StudentGrade.Stu_id,Stu_name,Depar_name
  from StudentGrade,Deparment,Student
  where Student.Stu_id=StudentGrade.Stu_id 
        and Course_id in(select Course.Course_id from Course,StudentGrade where 
                         Course.Course_id=StudentGrade.Course_id and Course_name='数据库')
		and Depar_id in (select Depar_id from Class,Student where 
                         Student.Class_id=Class.Class_id)
  order by Grade desc

9.至少选修了两门课及以上的学生姓名和性别。

出现错误如下:

select Stu_name,Stu_sex
from Student,StudentGrade
where StudentGrade.Stu_id=Student.Stu_id  and count(Student.Stu_id)>=2
		

消息 147,级别 15,状态 1,第 3 行
聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。

完成时间: 2022-03-25T19:31:25.1897136+08:00
	     
select Stu_name,Stu_sex
from Student
where Stu_id in(select Student.Stu_id 
                from StudentGrade,Student 
				where StudentGrade.Stu_id=Student.Stu_id 
				      and count(Student.Stu_id)>=2
			     )

消息 147,级别 15,状态 1,第 6 行
聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。

完成时间: 2022-03-25T19:28:36.6061070+08:00

错误原因在于 count()语句作为聚合语句不能出现在where语句中,将其放入having子句中即可,改正如下:

方法一,外部连接,并采用having子句

select Stu_name,Stu_sex,Course_id 
from Student,StudentGrade
where StudentGrade.Stu_id=Student.Stu_id  
group by Stu_name,Stu_sex,Course_id 
having count(Student.Stu_id)>=2
		

语句可以正常运行,却无结果 

原因:对一张没有主键的表进行 group by 时,group by 后面跟的字段要能唯一识别一条数据,否则会出现结果为1条或没有结果;

 

若去掉course-id,则可以正常出现结果,但是无法验证答案正确性  97列

select Stu_name,Stu_sex
from Student,StudentGrade
where StudentGrade.Stu_id=Student.Stu_id  
group by Stu_name,Stu_sex
having count(Student.Stu_id)>=2

 方法二,直接采用子查询 100列

select Stu_name as 学生姓名,Stu_sex as 学生性别
from Student
where Stu_id in (select Stu_id
                 from StudentGrade
                 group by Stu_id
                 having count(Stu_id)>=2)

两者数值差异的原因在于 可能把空值也count进去了,应在where语句中加一个非空值条件(where Stu_name is not null),但是还是100列,查找错误中

 

 

1实验目的 本实验的目的是要求学生熟练掌握使用SQL 通过SQL Server企业管理器向数据库输入数据、修改数据删除数据的操作。 2. 实验时数 2学时 [相关知识] SQL Server提供了很多方法更新表中数据。以插入记录为例,可以使用INSERT语句、VALUES子句将特定值集合插入一行;可以使用数据库接口API(ADO、OLE DB、ODBCDB-Library)中的函数进行数据插入等。本实验主要掌握INSERT、UPDATEDELETE语句的基本用法。  插入操作,其语句的一般格式为 INSERT INTO table_or_view [(colum_list)] VALUES (date_values); 此语句是使date_values作为一行数据记录插入已命名的表或视图table_or_view中。 Column_list是由逗号分隔的table_or_view的列名列表,用来指定为其提供数据的列。如果没有指定column_list,表或视图中的所有列都将接收数据。 如果column_list没有为表或视图中的所有列命名,将在列表中没有命名的任何列中插入一个NULL值(或者在默认情况下为这些列定义的默认值)。在列的列表中没有指定的所有列都必须允许NULL值或者指定的默认值。否则SQL Server将返回一个错误。  修改操作语句的一般格式为 UPDATE 表名 SET <列名>=<表达式>[,<列名>=<表达式>]…… [where <条件>]; 其功能是修改指定表中满足WHERE子句条件的元组。其中SET子句用于指定修改方法,即用<表达式>的值取代相应的属性列值。如果省略WHERE子句,则表示要修改表中的所有元组。  删除语句的一般格式为 DELETE FROM <表名> [WHERE <条件>]; 其功能是从指定表中删除满足WHERE子句条件的所有元组。如果省略WHERE子句,则表示要删除表中的所有元组,但表的定义仍在字典中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

老坛酸菜吃鸭子

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值