数据库SQL Server实验报告 之 SQL语言进行简单查询(3/8)

实验名称          SQL语言进行简单查询

注意:原版word在下载资源里面(免费下载)

实验目的及要求:

  1. 掌握各种查询的SQL脚本写法,包括SELECT、FROM、WHERE、GROUP BY、HAVING子句的用法,特别要求比较内连接、外连接的结果
  2. 在XSGL数据库中,使用各种查询条件完成指定的查询操作。

实验内容及步骤:

注:所有程序第一行均在XSGL数据库中进行,即每次运行程序第一行均为use XSGL

  1. 查询选修了课程的学生人数。

select count(distinct sno) 学生人数--distinct表示去掉重复行

from sc

  1. 查询学生200515001选修课程的总学分数。

select sum(credit)

from course,sc

where sno = '200515001'

and sc.cno = course.cno and grade>=60; --成绩超过60才有学分,并连接两个表

  1. 查所有有成绩的学生学号和课程号。

select sno,cno

from sc

where grade is not null; --排除空值

  1. 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

select sname,sdept,sage

from student

where sage>=20and sage<=23; --范围条件语句  也可以用betweenand

  1. 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。

select sno,grade

from sc

where cno=3

order by grade desc;--降序排列;

  1. 求哪些学生还没有被分配系?

select *--查询所有表的所有信息

from student

where sdept=' 'or sdept is null;--sdept 为空或者为空格

  1. 求CS系中所有男同学的学生姓名和年龄。

select sname,sage

from student

where ssex='男'and sdept='CS';

  1. 我校开设的课程中哪些课程名以“数据”两个字开头?

select cname

from course--like语句表示求相似的,

where cname like '数据%';--数据%表示以数据开头,后面0到无穷多个字符

  1. 求哪些学生的姓名中第2个字是“立”?

select sname

from student

where sname like '_向%';--_表示一个字符

  1. 求哪些学生的成绩为优秀,求出该学生的学号及相应的课程号。

select sno,cno

from sc

where grade>90;--以90分以上为优秀

  1. 求既不是CS系,也不是MA系的学生中年龄不小于20的学生姓名。

select sname

from student

where (sdept<>'cs'and sdept<>'ma')and sage>=20;--注意优先级,最好加括号

  1. 求CS系中男女学生的数量分别是多少?

select ssex,count(ssex) 人数--也可以用*代替ssex

from student

where sdept='cs'

group by ssex--分别输出男女需要分组

  1. 求各系中每个年龄段的学生总人数,要求结果中对系进行排序,同一个系的按年龄排序。

select count(sno) 人数,sdept,sage

from student

group by sdept,sagegroup by 语句后面的词数与 select后单个词数一致

order by sdept desc,sage ;--降序为desc 升序为asc可省略

  1. 查询各系的学生的人数并按人数从多到少排序 

select count(sno) 人数,sdept

from student

group by sdept

order by count(sno) desc;--降序为desc 升序为asc可省略

  1. 查询各系的男女生学生总数, 并按系别升序排列, 女生排在前

select sdept,ssex,count(sno) 人数

from student

group by sdept,ssex

order by sdept,ssex desc;--女生在前,按照第二字母顺序降序为desc

  1. 查询选修了3门课程已上的学生的学号和姓名

select student.sno,sname--必须加上student.或者sc.因为两个表都有sno

from sc,student

where student.sno=sc.sno--连接两个表

group by student.sno,sname

having count(*)>3--分组后显示选了三门以上的,而不是直接显示整个表三门以上的

  1. 查询每个学生所选课程的平均成绩, 最高分, 最低分和选课门数

select sno,avg(grade) 平均分,max(grade) 最高分,

min(grade) 最低分,count(cno) 选课门数

from sc

group by sno

  1. 查询至少选修了2门课程的学生的平均成绩.

select sno,avg(grade) 平均分

from sc

group by sno--查询的是选修至少两门的每个学生,然后计算每个学生的课程的信息,则对学生分组,每一个学生为一组

having  count(cno)>=2; --having后面可以使用聚合函数。聚合函数就是对一组值进行计算并且返回单一值的函数:sum---求和,count---计数,max---最大值,avg---平均值等。所以不用where。

  1. 查询平均分超过80分的学生的学号和平均分.

select sno,avg(grade) 平均分

from sc

group by sno

having  avg(grade)>=80;

比较: 求各学生的60分以上课程的平均分.

select sno,avg(grade) 平均分

from sc

where grade>60

group by sno--having既然是对查出来的结果进行过滤,那么就不能对没有查出来的值使用having。

  1. 查询”信息系”(IS)中选修了2门课程以上的学生的学号.

select student.sno

from sc,student

where sdept='is'and student.sno=sc.sno--一定要记得连接两个表

group by student.sno

having count(cno)>2;

  1. 打印李勇的成绩单(即求李勇所选修的课程名及其成绩)。

select cname,grade

from sc,student,course--需要连接三个表

where course.cno=sc.cno

and student.sno=sc.sno and sname='李勇 ';

  1. 求不及格和缺考的学生所在系、学号、姓名及相应课程名,要求按系排序,同一个系的按学号排序。

select sdept,student.sno,sname,cname

from student,course,sc

--sno是student表的主码,cno是course表的主码,需要用主码与sc表中的外码连接

where student.sno=sc.sno

and course.cno=sc.cno

and (grade<60 or grade is null)

--不用and因为成绩小于六十与成绩为空不能同时发生

--有or出现注意通过题目判断是否加括号

order by sdept ,student.sno--先按系排序,则系别在前面

  1. 求CS系不及格和缺考的学生学号、姓名及相应课程名,要求按学号排序。

select student.sno,sname,cname

from student,course,sc

where student.sno=sc.sno

and course.cno=sc.cno

and (grade<60 or grade is null)

and sdept='cs'

order by student.sno

  1. 求既不是CS系,也不是MA系缺考学生的学号、姓名及相应课程名。

select student.sno,sname,cname

from student, course, sc

where student.sno=sc.sno and course.cno=sc.cno

and grade is null

and sdept not in('cs','ma')--与sdept<>'cs'and sdept<>'ma'一致

  1. 求选修BD课程的学生平均成绩。

select avg(grade)

from course,sc

where course.cno=sc.cno and cname='DB'

  1. 求每一门课程的学生平均成绩,要求输出课程名及对应的平均成绩,并按平均成绩由大到小排序。

select cname, avg(grade) --成绩在sc表,课程名在course表

from course, sc

where course.cno=sc.cno

group by cname--求每一门课程,所以分组

order by avg(grade) desc--从大到小即为倒序

  1. 求李勇所选修的总学分(即成绩及格的课程学分总和)。

select sum(credit) 总学分

from student,course,sc--成绩在sc表,学分在course表,李勇在student表

where student.sno=sc.sno and course.cno=sc.cno

and sname='李勇' and grade>=60

  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
很抱歉,我并不是一个可以接收并处理实验报告的程序。但是,我可以为您提供一些关于 JSP 访问数据库的基本知识和指导。 首先,JSP(Java Server Pages)是一种用于创建动态Web页面的Java技术。它允许在页面中嵌入Java代码,以及使用标签库和表达式语言来简化页面开发。 要在JSP中访问数据库,您需要使用JDBC(Java Database Connectivity)API。这个API提供了一组Java类和接口,用于连接和操作各种类型的数据库。 下面是一个简单的JSP页面,演示如何使用JDBC API连接到MySQL数据库,并查询其中的数据: ``` <%@ page language="java" %> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>JSP Database Example</title> </head> <body> <% // Connect to the database Connection conn = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/mydatabase"; String username = "root"; String password = "password"; conn = DriverManager.getConnection(url, username, password); // Query the database Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); // Display the results while (rs.next()) { out.println(rs.getString("name") + " " + rs.getInt("age")); } // Close the database connection rs.close(); stmt.close(); conn.close(); %> </body> </html> ``` 在这个例子中,我们首先使用Class.forName方法加载MySQL驱动程序,然后使用DriverManager.getConnection方法连接到MySQL数据库。接下来,我们使用Statement.executeQuery方法查询数据库,并使用ResultSet.next方法依次遍历查询结果。最后,我们关闭了ResultSet、Statement和Connection对象,以释放资源。 当然,上面的例子只是一个简单的示例,实际情况可能更加复杂。如果您想了解更多关于JSP访问数据库的知识,请参考相关的教程和文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

君无戏言。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值