Oracle中简单的SQL查询语句练习

#建学生信息表student
create table student
(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday DATE,
class varchar(20)
);
#建立教师表
create table teacher
(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday DATE,
prof varchar(20),
depart varchar(20) not null
);
#建立课程表course
create table course
(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);
#建立成绩表
create table score
(
sno varchar(20) not null,
foreign key(sno) references student(sno),
cno varchar(20) not null,
foreign key(cno) references course(cno),
degree decimal
);


#添加学生信息
insert into student values('04','曾华','男',to_date('1984-9-2','yyyy-mm-dd'),'95033');
insert into student values('05','匡明','男',to_date('1988-2-4','yyyy-mm-dd'),'95031');
insert into student values('07','王丽','女',to_date('1990-3-2','yyyy-mm-dd'),'95033');
insert into student values('01','李军','男',to_date('1990-4-23','yyyy-mm-dd'),'95033');
insert into student values('09','王芳','女',to_date('1990-5-29','yyyy-mm-dd'),'95031');
insert into student values('03','陆君','男',to_date('1990-8-9','yyyy-mm-dd'),'95031');




#添加教师表


insert into teacher values('804','李诚','男',to_date('1958-2-2','yyyy-mm-dd'),'副教授','计算机系');
insert into teacher values('856','张旭','男',to_date('1969-2-3','yyyy-mm-dd'),'讲师','电子工程系');
insert into teacher values('825','王萍','女',to_date('1972-5-5','yyyy-mm-dd'),'助教','计算机系');
insert into teacher values('831','刘冰','女',to_date('1977-8-4','yyyy-mm-dd'),'助教','电子工程系');




#添加课程表
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-661','数字电路','856');
insert into course values('9-888','高等数学','831');


#添加成绩表
insert into score values('03','3-245','86');
insert into score values('03','3-105','92');
insert into score values('03','3-105','64');
insert into score values('03','6-661','85');
insert into score values('05','3-245','75');
insert into score values('05','3-105','88');
insert into score values('05','3-105','91');
insert into score values('05','6-661','79');
insert into score values('09','3-245','68');
insert into score values('09','3-105','76');
insert into score values('09','3-105','78');

insert into score values('09','6-661','81');





--查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,Class from student;


 --查询教师所有的单位即不重复的Depart列。
 
 select distinct depart from teacher;
 
 --查询Student表的所有记录。
select * from student;


--查询Score表中成绩在60到80之间的所有记录。


select * from score where degree between 60 and 80;


--查询Score表中成绩为85,86或88的记录
select * from score where degree in (85,86,88);


--查询Student表中“9503”班或性别为“女”的同学记录


select * from student where class=95033 or ssex='女';


--以Class降序查询Student表的所有记录。
select * from student order by class desc; 


--以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc;


--查询“9503”班的学生人数
select count(*) from student where class like '9503%';




--查询Score表中的最高分的学生学号和课程号。
select sno,cno,degree from score where degree = (select max(degree) from score) ;


--查询每门课的平均成绩。


select cno,avg(degree) from score group by cno; 


--查询分数大于70,小于90的Sno列
select * from student;
select * from score;
select * from course;


select sno,degree from score where degree >70 and degree <90 ;


--查询所有学生的Sname、Cno和Degree列


select student.sname,score.cno,degree  from student,score where student.sno = score.sno;


--查询所有学生的Sno、Cname和Degree列


select sno,cname,degree from score,course where score.cno= course.cno;


--查询所有学生的Sname、Cname和Degree列


select sname ,cname,degree from student,score,course where student.sno=score.sno and score.cno =course.cno;


猜你喜欢

转载自blog.csdn.net/qq_42484700/article/details/80782052