oracle 基本操作和小技巧

--模糊查询
--1.查询住址为“山东”的学生姓名、电话、住址
select studentname,phone,address
from student
where address like '山东%';

--2.查询名称中含有“数据库”字样科目信息
select *
from subject
where subjectname like '%数据库%';


--3.查询电话中以“1387”开头的学生信息
select *
from student
where phone like '1387%';

--4.查询姓姜的,单名的学生信息
select *
from student
where studentname like '姜_';

--5.查询学号为1004的学生参加的科目编号为1、2、3的考试成绩信息
select *
from result
where studentno=1004 and subjectno in(1,2,3);


--6.查询出生日期在1980-1-1到1989-12-31之间学生信息
select *
from student
where borndate between to_date('1980-1-1','YYYY-MM-DD') and to_date('1989-12-31','YYYY-MM-DD');

--分组统计:
--1.查询每个学期/年级学时数超过50的课程数
select gradeid 年级,count(*) 课程数
from subject
where classhour>50
group by gradeid;


--2.查询每学期学生的平均年龄
select gradeid 年级,avg(months_between(sysdate,borndate)/12) 平均年龄
from student
group by gradeid;

--3.查询北京地区的每学期学生人数
select gradeid 年级,count(*) 人数
from student
where address like '北京%'
group by gradeid;

--4.查询参加考试的学生中,平均分及格的学生记录,并按照成绩降序排列
select studentno 学号,avg(studentresult) 平均成绩
from result
group by studentno
having avg(studentresult)>=60
order by 2 desc;


--5.查询考试日期为2010年3月22日的课程的及格平均分
select subjectno 课程编号, avg(studentresult) 平均分
from result
where examdate=to_date('2010-03-22','YYYY-MM-DD') and studentresult>=60
group by subjectno;


--6.查询至少2次考试不及格的学生学号、不及格次数
select studentno 学号,count(studentno) 不及格的次数
from result
where studentresult<60
group by studentno
having count(studentno)>=2;

猜你喜欢

转载自www.cnblogs.com/javahua/p/11563278.html
今日推荐