SQL 综合练习一


use test2
create table student(
sno char(9) primary key,
sname char(20) not null,
Sex char(2) not null,
Sage int not null,
Sdept char(20)
)

create table Course(
Cno  char(4)  primary key ,
Cname char(40) not null,
Cpno  char(40) ,
Ccredit  Int not null
)

create table SC(
Sno  char(9) foreign key references  student(sno) not null,
Cno  char(4) foreign key references  Course(cno) not null,
Grade int
)

insert into Student
select '20215121','李勇','男',20,'CS' union
select '20215122','刘晨','男',19,'CS' union
select '20215123','王敏','女',18,'MA' union
select '20215125','张立','男',19,'IS'

insert into Course
select '1','数据库',NULL,4 union
select '2','数学',NULL,2 union
select '3','信息系统',NULL,4 union
select '4','操作系统',NULL,3 union
select '5','数据结构',NULL,4 union
select '6','数据处理',NULL,2 union
select '7','PASCAL语言',NULL,4

insert into SC
select '20215121','1',92 union
select '20215121','2',85 union
select '20215121','3',88 union
select '20215122','2',90 union
select '20215122','3',80 


select * from student
select * from course
select * from sc

--1、    查询学生表中的所有信息
select * from student
--2、    查询19岁以下年龄的学生
select * from student where sage < 19
--3、    查询出所有学生的名字,考试课程号,分数
select a.sname '学生姓名',c.cno '课程编号',c.grade '课程分数'
from student a
inner join sc c on  a.sno=c.sno
--4、    查询出所有学生的选课情况,未参加选课的人员需要显示
select a.sname '学生姓名',b.cno '课程编号',c.grade '课程分数',b.cname '课程名称'
from student a
left join sc c on  a.sno=c.sno
left join course b on b.cno=c.cno

--5、    查询出参加选课的学生及选课信息,未参加选课的人员不显示
select a.sname '学生姓名',b.cno '课程编号',c.grade '课程分数',b.cname '课程名称'
from student a
inner join sc c on  a.sno=c.sno
inner join course b on b.cno=c.cno
--6、    查询学生的成绩,并显示其名字和学号
select a.sno '学号',a.sname '学生姓名',c.grade '课程分数',b.cname '课程名称'
from student a
inner join sc c on  a.sno=c.sno
inner join course b on b.cno=c.cno
--7、    查询考试成绩有80分以上的学生学号(以4种不同的方法完成本题目)
--方法一:
select a.sno '学号',a.* from student a where a.sno in (
select b.sno from sc b where  grade>80)
--方法二:
select distinct(a.sno) '学号' from student a,sc b where a.sno=b.sno and grade>80
--方法三:
select distinct(a.sno) '学号' from student a
inner join sc b on (a.sno=b.sno) where grade>80
--方法四:
select distinct(a.sno) '学号' from student a
right join sc b on (a.sno=b.sno) where grade>80

--8、    查询出选择了1号课程的学生姓名及成绩,并按成绩降序排列
select a.sname '学生姓名',c.grade '成绩'
from student a,SC c
where a.sno=c.sno and  cno =1
order by grade desc
--9、    查询出不姓刘的学生姓名
select sname '学生姓名' from student where sname not like '%刘%'
--10、    查询出不是计算机系(CS),数学系(MA),IS(信息系)的学生姓名
select sname '学生姓名' from student where sdept not in('CS','MA','IS') or sdept is NULL
--11、    查询出每个系有多少人
select sum(sno) from student
group by sdept


--12、    查询出学生为‘20215121’的选修课程的总学数分
select sum(a.ccredit) '总学分' from course a
inner join sc c on a.sno=c.sno
where a.sno='20215121' 


--13、    查询出2号课程取得最高分数的学生姓名及分数
select max(c.grade) '数学分数',a.sname '学生姓名' from student a,course b ,sc c 
where  a.sno=c.sno and c.cno=b.cno and c.cno=2
group by a.sno,a.sname,c.grade

select * from student
select * from course
select * from sc

猜你喜欢

转载自blog.csdn.net/m0_64351096/article/details/128035556