Oracle 数据库基础

create table student(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5)
);

create table teacher(
tno varchar2(10) primary key,
tname varchar2(20)
);

create table course(
cno varchar2(10),
cname varchar2(20),
tno varchar2(20),
constraint pk_course primary key (cno,tno)
);

create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2), 
constraint pk_sc primary key (sno,cno)
);




/*******初始化学生表的数据******/
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
insert into student values ('s011','杨超越',18,'女');
insert into student values ('s012','孙超越',21,'女');

/******************初始化教师表***********************/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');

/***************初始化课程表****************************/
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS ','t001');
insert into course values ('c009','PHP ','t003');
insert into course values ('c010','EJB3.0','t002');

/***************初始化成绩表***********************/
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c001',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003','59');


/*
Question:

0.   查询所有表中的信息
*/
select * from student;
select * from teacher;
select * from course;
select * from sc;




--1、查询姓“李”的学生的详细信息
-- 这是一个模糊查询
-- 精确查询select * from student where sname ='李**'
select * from student where sname like '李%';
--% 是通配符


--2、查询20-23岁的男同学有多少个
/*   分解一下查询条件
     年龄   20~23
     性别是 男


     查询的结果是一个统计结果
*/
select count(*) from student where ssex='男' and sage>=20 and sage<=23;
/*
统计函数 count(*)
and  同时要求满足
*/

--3、查询姓“刘”的老师有多少个
select count(*) from teacher where tname like '刘%';

--4、查询男生、女生人数(一条SQL)
select count(*) from student where ssex ='男';
select count(*) from student where ssex ='女';

/*
分组查询  group by
你要查什么  在那个地方查  你分组条件是什么
*/

select count(*) from student group by ssex;



--5、查询'刘阳'老师所教的课程
--通过表的查询结果来查询
--连表查询  join
-- in  包含于()
select * from course where tno in  (select tno from teacher where tname='刘阳');


--6、查找学员编号为's004','s007','s008'的学生信息
--精确查找
select * from student where sno ='s004';
select * from student where sno in  ('s004','s007','s008');


--7、查找教'Oracle'课程的老师
select * from teacher where tno in (select tno from course where cname='SSH');

--8、查找成绩低于60分的课程名称
select cname from course where cno in ( select cno from sc where score<60) ;

--9、查找成绩良好(分数>80)的学员名称和课程名称
-- select sname from student  where ?
-- select cname from course   where ?
-- select ? from sc where score >80
select 
(
    select sname 
    from student  
    where student.sno=sc.sno) as '姓名',
(    select  
    from course   
    where course.cno=sc.cno
) as '科目'
from sc 
where score >80;
-- 上面这种是最老土的做法

select student.sname ,course.cname from student,course,sc 
where score >80 and student.sno=sc.sno and course.cno=sc.cno;

--10、查询平均成绩大于60 分的同学的学号和平均成绩
/*
avg() 求平均值
*/

select sno,avg(score) 
from  sc
group by sno having avg(score)>60;

/*
根据分组查询学号平均成绩大于60分 
再来查对应的信息

*/

--11、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
/*
max() 求最大值
min() 求最小值
*/

select cno ,max(score),min(score) 
from sc
group by cno


--12、查询同名同姓学生名单,并统计同名人数
select  sname,count(*) 
from student
group by sname;

--13、1999 年出生的学生名单(注:Student 表中Sage 列的类型是number)
select sname from student where 2020-sage=1999;
--select sname from student where YEAR(getdate())-sage=1999;
/*
where 两种写法  1 写死 
               2 获取当前时间
*/

14、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cno , avg(score)
from sc
group by cno 
order by avg(score),cno desc;

/*
1 做一个分组查询 group by
2 做一个排序 order by

*/

--15、查询课程名称为“Oracle”,且分数低于60 的学生姓名和分数
--select * from course where cname='Oracle'
--select * from  sc where score <60

select (select sname from student  where student.sno=sc.sno), score
from  sc
where  cno in (select cno  from course where cname='Oracle') and score<60;


16、查询所有学生的选课情况;

select (select sname from student where student.sno=sc.sno),
        (select cname from course where course.cno=sc.cno)
from sc;


17、查询不及格的课程,并按课程号从大到小排列

select sno from sc where score<60 order by sno DESC;

18、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;

select sno,sname from student 
where sno in(
    select sno 
    from sc 
    where cno='c001' 
    and
    score >=80
      )
    ;

19、求选了课程的学生人数

select count(*) from student where exists(
select * from sc where student.sno=sc.sno);

20、查询各个课程及相应的选修人数
select cno ,count(*) from sc group by cno;

21、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select a.* from sc a ,sc b where a.score=b.score and a.cno<>b.cno
22、检索“c001”课程分数小于100,按分数降序排列的同学学号
select  sno from sc  where cno='c001' and score<100 order by score; 
23、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select * from student st
join sc a on st.sno=a.sno
join sc b on st.sno=b.sno
where a.cno='c002' and b.cno='c001' and a.score < b.score;
24、查询没学过“谌燕”老师课的同学的学号、姓名;
select sno ,sname from student where sno not in (select sno from sc where cno in
(select cno from course where tno in (select tno from teacher where tname='谌燕') ));

25、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select cno,avg(score),sum(case when score>=60 then 1 else 0 end)/count(*)
as 及格率
from sc group by cno
order by avg(score) , 及格率 desc;
每个人都是在努力的路上,别因为别人的误解而放弃,,术业有专攻,如是而已。
原文地址:https://www.cnblogs.com/16699qq/p/13153410.html