使用 T-SQL 语句完成单表数据检索

内容:

1. 使用SELECT 语句进行基本表查询;
2. 利用查询条件表达式进行较高级的查询;
3. 使用GROUP BY 子句进行分组,注意搭配使用HAVING 子句;
4. 使用ORDER BY 子句进行排序。

实验三数据为基础,使用 T-SQL 语句实现以下操作:

  1. 查询院系名称为“计算机系”的学生的基本信息(学号、姓名、性别、年龄)
select * from student
where sdept='计算机系'
  1. 查询学号为 201715008 的学生的姓名。
select sname from student
where snum='201715008'
  1. 查询成绩在 75-85 之间的学生的学号。
select sno from cj
where score between 75 and 85
  1. 查询所有姓王,并且姓名为两个字的学生的信息。
select * from student
where sname like '王_'
  1. 查询选修课程号为‘c001’且成绩非空的学生学号和成绩,成绩按 150分制输出(每个成绩乘以系数1.5)。
select sno,score*1.5 from cj
where cno='c001' and score is not null
  1. 查询有选课记录的所有学生的学号,用 distinct 限制结果中学号不重复。
select distinct sno from cj
  1. 查询选修课程‘c001’的学生学号和成绩,结果按成绩的升序排列,如果成绩相同则按学号的降序排列。
select sno,score from cj
where cno='c001'
order by score asc,sno desc
  1. 列出所有不姓刘的所有学生。
select sname from student
where sname not like '刘%'
  1. 显示在 1994 年以后出生的学生的基本信息。
select * from student
where 2020-sage>1994
  1. 查询出课程名含有“数据”字串的所有课程基本信息。
select * from course
where cname like '%数据%'
  1. 列出选修了‘c001’课程的学生成绩,按成绩的降序排列。
select score from cj
where cno='c001'
order by score desc
  1. 列出同时选修“c001”号课程和“c002”号课程的所有学生的学号。
select sno from cj
where sno in (select  sno from cj where cno='c001') and cno='c002'
  1. 列出年龄超过平均值的所有学生名单(学号、姓名、性别、院系),按年龄的降序显示。
select snum,sname,ssex,sdept from student
where sage>(select AVG(sage) from student)
order by sage desc
  1. 按照出生年份升序显示所有学生的学号、姓名、性别、出生年份及院系。
select * from student
order by sage desc
  1. 按照课程号、成绩降序显示课程成绩在 70-80 之间的学生的学号、课程号及成绩。
select sno,cno,score from cj
where score between 70 and 80
order by cno desc,score desc
  1. 显示学生信息表中的学生总人数及平均年龄,在结果集中列标题分别指定为“学生总人数,平均年龄”。
select COUNT(snum) as '学生总人数',AVG(sage) as '平均年龄' 
from student

猜你喜欢

转载自blog.csdn.net/weixin_45104240/article/details/106333844