数据库实验4 SQL语言-SELECT查询操作
1.首先按照第三章的jxgl数据库的模板创建jxgl数据库并插入数据:
创建数据库jxgl:
create database jxgl;
创建相应的表:
创建student表,student(sno,sname,ssex,sage,sdept):以sno为主键primary key:
create table student
( sno CHAR(7) primary key,
sname varchar(20),
sage INT,
ssex varchar(2),
sdept varchar(2)
);
创建course表,course(cno,cname,cpno,ccredit): 以cno为主键primary key,cpno默认为NULL:
create table course
( cno CHAR(2) primary key,
cname varchar(20),
cpno char(2) default NULL,
ccredit INT
);
创建sc表,sc(sno,cno,grade):
以(sno,cno)为主键,sno为外码参照student表中的sno,cno为外码参照course表中的cno :
create table sc
( sno CHAR(7) ,
cno CHAR(2),
grade INT,
primary key(sno,cno),
foreign key(sno) references student,
foreign key(cno) references course
)
插入数据:
插入student的数据:
insert into student
values('2005001','钱横',18,'男','Cs'),
('2005002','王林',19,'女','Cs'),
('2005003','李民',20,'男','Is'),
('2005004','赵欣然',16,'女','Ma');
插入course的数据:
insert into course
values('1','数据库系统','5',4),
('2','数学分析',null,2),
('3','信息系统导论','1',3),
('4','操作系统原理','6',3),
('5','数据结构','7',4),
('6','数据处理基础',null,4),
('7','C语言','6',3);
插入表sc的数据:
insert into sc
values('2005001','1',87),
('2005001','2',67),
('2005001','3',90),
('2005002','2',95),
('2005003','3',88);
1.检索年龄大于23岁的男学生的学号和姓名
select sno,sname
from student
where ssex='男' and sage>23;
2.检索至少选修一门课的女学生姓名
select sname
from student natural join sc
where ssex='女';
3.检索王林不学的课程的课程号
select cno
from course
where cno not in
(select cno
from student natural join sc
where sname='王林');
4.检索至少选修两门课的学生学号
select distinct sno
from sc
group by sno
having count(sno)>1;
5.检索全部学生都选修的课程的课程号和课程名
select cno,cname
from sc natural join course
group by cno
having count(cno)=(select count(*)from student);
6.检索选修了所有3学分课程的学生平均成绩
with tgc(cno,nums) as
(select cno,count(cno)
from course
where ccredit=3)
select avg(grade)
from sc
where sno in
(select sno
from sc natural join tgc
group by sno
having count(cno)=(select count(*) from course where ccredit=3))
group by sno;
1.统计有学生选修的课程门数
select count(distinct cno)
from sc;
2.求选修4号课程的学生的平均年龄
select avg(sage)
from student natural join sc
where cno='4' ;
3.求学分为3的每门课程的学生平均成绩
select avg(grade)
from sc natural join course
where ccredit=3
group by cno;
4.统计每门课程的学生选修人数,要求超过三人的课程才统计,要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno,count(cno)
from sc
group by cno
having count(cno)>3
order by count(cno) desc ,cno asc;
5.检索学号比王林同学大而年龄比王林同学小的学生姓名
with wanglin(age,no) as
(select sage,sno from student where sname="王林")
select sname
from student,wanglin
where sno>no and sage<age;
6.检索姓名以’王’开头的所有学生的姓名和年龄
select sname,sage
from student
where sname like '王%';
7.在sc表中检索成绩为空值的学生的学号和课程号
select sno,cno
from sc
where grade is null;
8.求年龄大于女学生平均年龄的男同学的姓名和年龄
with girl_age(age) as
(select avg(sage)
from student
where ssex="女")
select sname,sage
from student,girl_age
where ssex='男' and sage>age;
9.求年龄大于所有女学生年龄的男学生的姓名和年龄
with girl_max_age(age) as
(select max(sage)
from student
where ssex="女")
select sname,sage
from student,girl_max_age
where ssex='男' and sage>age;
10.检索选修了4门以上课程的学生总成绩(不统计不及格课程),并要求按总成绩的降序排列出来。
select sum(grade)
from sc
group by sno
having count(cno)>4
ORDER BY sum(grade) DESC;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)