三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC)

SQL数据库操作语句习题

三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC)

这是我课本上习题作业,没有参考答案,是我自己写的。语句可能有错误!!!
有误请指正,谢谢。
《数据库原理与应用教程》第4版 何玉洁 编著 机械工业出版社
下面两个地址数据库内容一样 百度网盘里有txt的数据
配套数据库sql server(主要是想赚积分)https://download.csdn.net/download/jivder/11162509
百度网盘链接(有txt数据文件): 链接:https://pan.baidu.com/s/14HiZPpX1lSljrTgmjQLinw
提取码:q72d

表结构

CREATE TABLE [dbo].[Course](
	[Cno] [nchar](4) NOT NULL,
	[Cname] [varchar](50) NOT NULL,
	[Credit] [tinyint] NULL,
	[Semester] [tinyint] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[SC](
	[Sno] [nchar](7) NOT NULL,
	[Cno] [nchar](4) NOT NULL,
	[Grade] [tinyint] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Student](
	[Sno] [nchar](7) NOT NULL,
	[Sname] [nchar](10) NOT NULL,
	[Sex] [nchar](2) NULL,
	[Sage] [tinyint] NULL,
	[Sdept] [nchar](20) NULL
) ON [PRIMARY]

问题

  1. 查询学生选课表中的全部数据
SELECT * FROM SC
  1. 查询计算机系学生的姓名、年龄
SELECT Sname, Sage FROM Student WHERE Sdept = '计算机系'
  1. 查询成绩在70-80分之间的学生的学号、课程号和成绩
select Sno, Cno, Grade from SC
where Grade between 70 and 80
  1. 查询计算机系年龄在18-20之间且性别为“男”的学生的姓名、年龄。
select Sname, Sage from Student
where Sage between 18 and 20 and Ssex = '男' 
and Sdept = '计算机系'
  1. 查询课程号为“c001”的课程的最高分数。
select top 1 Grade from SC
where Cno = 'c001'
  1. 查询计算机系学生的最大年龄和最小年龄。
select MAX(Sage) as 最大年龄, MIN(Sage) as 最小年龄 from Student
  1. 统计每个系的学生人数
select Sdept as 系别, COUNT(Sno) as 学生人数 from Student
 group by Sdept
  1. 统计每门课程的选课人数和考试最高分
select Cno as 课程号, COUNT(Sno) as 选课人数, MAX(Grade) as 最高分
from SC
 group by Cno
  1. 统计每个学生的选课门数和考试总成绩,并按选课门数升序显示结果。
select Student.Sno 学号, Student.Sname 姓名, count(Grade) 门数, sum(Grade) 总成绩 from SC
 join Student on Student.Sno = SC.Sno
  group by SC.Sno, Student.Sname, Student.Sno
   order by count(Grade)
  1. 查询总成绩超过200分的学生,要求列出学号、总成绩。
select Student.Sno 学号,sum(Grade) 总成绩 from SC
 join Student on Student.Sno = SC.Sno
  group by SC.Sno, Student.Sname, Student.Sno
   having sum(Grade) > 200
  1. 查询选修了“c002”号课程的学生的姓名和所在系。
select Sname, Sdept from Student S
join SC on S.Sno = SC.Sno
where Cno = 'c002'
  1. 查询成绩80分以上的学生的姓名、课程号和成绩,并按成绩降序排列结果。
select Student.Sname, Cno, Grade from Student
join SC on Student.Sno = SC.Sno
where Grade > 80
order by Grade desc
  1. 查询那些学生没有选课,要求列出学号、姓名、所在系。
select sno, Sname, Sdept from Student left join SC on student.sno= sc.sno
where sc.sno is null
  1. 查询与Java在同一学期开设课程的课程名和开课学期。
select Cname, Semester from Course
where Semester = (
select Semester from Course where Cname='Java')
  1. 查询与李勇年龄相同的学生的姓名、所在系和年龄。
select Sname 姓名, Sdept 所在系, Sage 年龄 from Student
where Sage = (select Sage from Student where Sname = '李勇')
  1. 用子查询实现如下查询。
    (1)查询选修了“c001”号课程的学生的姓名和所在系。
select Sname, Sdept from Student
	where Sno in (
		select Sno from SC 
			where Cno = 'c001')

(2)查询数学成绩80分以上的学生的学号、姓名、课程号和成绩。

select student.Sno, sname,Sdept, Cno, Grade from Student
 join SC on Student.Sno = SC.Sno
   where Sdept = '数学系' and Grade >= 80

(3)查询计算机系考试成绩最高的学生的姓名。

select Sname from Student x join SC on x.Sno = SC.Sno
 where Sdept = '计算机系' and Grade = (
  select MAX(Grade) from SC join Student y on y.Sno = SC.Sno
   where Sdept = '计算机系')

(4)查询数据结构考试成绩最高的学生的姓名、所在系、性别和成绩。

select Sname, Sdept, Ssex, Grade from Student s join SC on s.Sno = SC.Sno
 where Grade = (
  select MAX(Grade) from SC join Course c on c.Cno = SC.Cno
   where Cname = '数据结构')
  1. 查询没有选修Java课程的学生的姓名和所在系。
select sname,sdept from Student
 where sno in (
  select sno from sc
   where cno in (select cno from Course 
    where Cname != 'Java'))
  1. 查询计算机系没有选课的姓名和性别。
select Sname from Student 
left join SC on Student.Sno = SC.Sno
where Sdept = '计算机系' and SC.Sno is null 
  1. 创建一个新表,表名为text_t,其结构体为(COL1, COL2, COL3),其中:
    COL1:整型,允许空值
    COL2:字符型,长度为10,不允许空值
    COL3:字符型,长度为10,允许空值
    试写出按行插入如下数据的语句
COL1 COL 2 COL2
B1
1 B2 C2
2 B3
insert into test_t(COL2) values ('B1')
insert into test_t(COL1,COL2) values ('1','B1')
insert into test_t(COL1,COL2) values ('2','B3')
  1. 删除考试成绩低于50分的同学的选课记录。
delete from SC
 where Grade < 50 
  1. 删除没有人选的课程记录。
delete from Course
 where Cno not in (select Cno from SC)
  1. 删除计算机系Java成绩不及格学生的Java选课记录
    查找60分以下的课程名
select Sno, Cname, Grade from SC
 join Course on sc.Cno= Course.Cno
  where Grade <60

删除语句

delete from SC
 where Grade < 60 and Cno in (
  select Cno from Course
   where Cname = 'Java')
  1. 将第2学期开设的所有课程的学分增加2分。
update Course set Credit = Credit + 2
 where Semester = '2'
  1. 将Java课程学分改为3分。
update Course set Credit = 3
 where Cname = 'Java'
  1. 将计算机系学生的年龄增加1岁。
update Student set Sage = Sage + 1
 where Sdept = '计算机系'
  1. 将信息系学生的“计算机文化”课程的考试成绩加5分。
select sname, Sdept, grade from Student s
 join SC on s.Sno = sc.Sno
  where Sdept= '信息系' and Cno in (
   select Cno from Course
    where Cname = '计算机文化学')
  1. 查询每个系年龄大于等于20的学生人数,并将结果保存到一个新永久表Dept_Age中。
select Sdept 系名, COUNT(*) 人数 into Dept_Age
 from Student
  where Sage > = 20
   group by Sdept
  1. 查询计算机系每个学生的Java考试情况,列出学号、姓名、成绩和成绩情况,其中成绩显示规则为:>=90,好;80-89,较好;70-79,一般;60-69,较差,<60,差。
select student.Sno 学号, Sname 姓名,grade 成绩,
 case 
  when Grade >= 90 then '好'
  when Grade between 80 and 89 then '较好'
  when Grade between 70 and 79 then '一般'
  when Grade between 60 and 69 then '较差'
  when Grade < 60 then '差'
end as 考试情况
from Student
 join SC on Student.Sno = SC.Sno
  where Sdept = '计算机系'and Cno in (
   select Cno from Course where Cname='Java')
  1. 统计每个学生的选课门数(包括没有选课的学生),列出学号、选课门数和选课情况,其中选课情况显示规则为:>=6,多;3-5,一般;1-2,偏少;0,未选课。
select s.sno 学号, count(Sc.Cno) 选课门数,
 case
  when count(sc.Cno) > 4 then '多'
  when count(sc.Cno) between 2 and 4 then '一般'
  when count(sc.Cno) between 1 and 2 then '少'
  when count(sc.Cno) = 0 then '未选'
 end as 选课情况
from student s left join sc on s.sno = sc.sno
 group by s.Sno
 order by COUNT(sc.cno) desc
  1. 修改全部课程的学分。1-2学期,学分增加5;3-4学期,学分增加3;5-6学期,学分增加1;其他不变。
update Course set Credit = Credit +
  case 
when Semester between 1 and 2 then 5
when Semester between 3 and 4 then 3
when Semester between 5 and 6 then 1
else 0
  end
  1. 查询“李勇”“王大力”所选的全部课程,列出课程名、开课学期和学分,不重复出现结果。
select Cname 课程名, Credit 学分, Semester 开课学期
 from Student S join SC on S.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '李勇'
intersect
select cname 课程名, credit 学分, Semester 开课学期
 from Student s join sc on s.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '王大力'
  1. 查询在第3学期开设课程中,李勇、王大力没选的课程,列出课程名和学分。
select Cname 课程名, Credit 学分
 from Student S join SC on S.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '李勇' and Semester = 3
except
select cname 课程名, credit 学分
 from Student s join sc on s.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '王大力' and Semester = 3
  1. 查询在学分>3分的课程中,李勇、王大力所选的相同课程,列出课程名和学分。
select Cname 课程名, Credit 学分
 from Student S join SC on S.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '李勇' and Credit > 3
intersect
select cname 课程名, credit 学分
 from Student s join sc on s.Sno = sc.Sno
 join Course c on c.Cno = sc.Cno
  where Sname = '王大力' and Credit > 3

你可能感兴趣的:(三个基本表:学生表(Student)、课程表(Course)、学生选课表(SC))