[数据库] 合肥工业大学—SQL Server数据库实验七:数据查询

1048 0
Honkers 2025-4-8 22:07:09 来自手机 | 显示全部楼层 |阅读模式


1. 单表查询

1. 查询全体学生的信息;

  1. -- 查询全体学生的信息
  2. select * from student
复制代码

2. 根据专业编号(21)查询学生的学号、性别和年龄;

  1. -- 根据专业编号查询学生的学号、性别和年龄
  2. select st_id '学号',st_sex '性别',datediff(year,st_born,getdate()) '年龄' from student where tc_mj = 21
复制代码

3. 查询未设定先修课的所有课程的信息;

  1. -- 查询未设定先修课的所有课程的信息
  2. select * from course where cs_prerequisite is null
复制代码

4. 查询选修了‘100’号课程,且成绩达到80分的学号;

  1. -- 查询选修了‘100’号课程,且成绩达到80分的学号;
  2. select sc_id from select_course where sc_num = 100 and sc_grade > 80
复制代码


2. 多表连接查询

1. 查询“计算机与信息系”(系号1)全体学生的学号、姓名、专业名称;

  1. -- 查询“计算机与信息系”(系号1)全体学生的学号、姓名、专业名称
  2. select st_id,st_name,mj_name from student,major where student.tc_mj = major.mj_id and mj_dpt = '1'
复制代码

2. 查询非“人工智能”(专业号21)专业,年龄小于20的学生信息;

  1. -- 查询非“人工智能”(专业号21)专业,年龄小于20的学生信息
  2. select * from student where tc_mj <> 23 and datediff(year,st_born,getdate())<20
复制代码

3. 查询先修课是“计算机基础”(课程号94)的所有课程的信息;

  1. -- 查询先修课是“计算机基础”(课程号94)的所有课程的信息
  2. select * from course where cs_prerequisite = 94
复制代码

4. 查询至少选修了‘100’和‘96’课程的学生学号

  1. -- 查询至少选修了‘100’和‘96’课程的学生学号
  2. select sc_id from select_course sc1 where sc_num = 100 and exists (select * from select_course sc2 where sc2.sc_num = 96 and sc1.sc_id = sc2.sc_id)
复制代码

5. 查询未选修“JAVA技术”(课程号100)课程的学生学号、姓名、性别和专业号

  1. -- 查询未选修“JAVA技术”课程的学生学号、姓名、性别和专业号
  2. select distinct sc_id '学号',st_name '姓名',st_sex '性别',tc_mj '专业号' from select_course,student where sc_id = st_id and sc_id not in(select sc_id from select_course where sc_num = 100)
复制代码

6. 查询未选修任何课程的学生学号和姓名

  1. -- 查询未选修任何课程的学生学号和姓名
  2. select sc_id,st_id from select_course,student where sc_id = st_id and sc_id not in (select sc_id from select_course where sc_num is not null)
复制代码

7. 查询未被学生选修的课程号、课程名、先修课

  1. -- 查询未被学生选修的课程号、课程名、先修课
  2. select cs_id,cs_name,cs_prerequisite from course,select_course where sc_id not in (select distinct sc_id from select_course)
复制代码

8. 用派生关系查询平均成绩达到90分的学生学号、姓名和平均成绩

  1. -- 用派生关系查询平均成绩达到90分的学生学号、姓名和平均成绩
  2. select st_id '学号',st_name '姓名',avg_grade '平均成绩' from student,(select sc_id,avg(sc_grade) as avg_grade from select_course group by sc_id) as sc_avg where avg_grade>90 and sc_avg.sc_id = student.st_id
复制代码

9. 查询学生的学号,只要先修课是“90”的课程他们都选修了

  1. -- 查询学生的学号,只要先修课是“90”的课程他们都选修了
  2. -- 查询不存在先选课为90的课程没有选修的学生学号
  3. select sc_id
  4. from
  5. select_course sc1
  6. where not exists
  7. (select *
  8. from course
  9. where cs_prerequisite = 90 and not exists
  10. (select * from
  11. select_course sc2
  12. where sc2.sc_num = course.cs_id and sc2.sc_id = sc1.sc_id))
复制代码

10. 查询未参加课程“100”考试的学生名单(学号、姓名、专业号)

  1. -- 查询未参加课程“100”考试的学生名单(学号、姓名、专业号)
  2. select distinct sc_id,st_name,tc_mj from select_course,student where sc_id = st_id and sc_id not in (select sc_id from select_course where sc_num = 100);
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行