通过理论学习与实验设计,使自己具备如下能力:
Select * from univ.dept order by dname;
create view v1 as
select * from univ.dept order by dname;
select ename,sal from univ.emp where sal between 15000 and 25000 order by ename;
create view v2 as
select ename,sal from univ.emp where sal between 15000 and 25000 order by ename;
select deptno,dname from univ.dept order by dname;
create view v3 as select deptno,dname from univ.dept order by dname;
select job from univ.emp order by job desc;
create view v4 as
select job from univ.emp order by job desc;
列出没有提成(comm)的所有职工的姓名与工资,并按姓名排序。
select ename,sal from univ.emp where(comm=0 or comm is null) order by ename;
create view v5 as
select ename,sal from univ.emp where(comm=0 or comm is null) order by ename;
列出部门编号是20,工作是“clerk”(办事员)的职工信息,并按姓名排序。
select * from univ.emp where (job='clerk'and deptno=20)order by ename;
create view v6 as
select * from univ.emp where (job='clerk'and deptno=20)order by ename;
select ename from univ.emp where (ename like'%AR%' or ename like'%IN%')order by ename;
create view v7 as
select ename from univ.emp where (ename like'%AR%' or ename like'%IN%')order by ename;
select ename from univ.emp where (ename like'%AR%' and ename like'%IN%')order by ename;
create view v8 as
select ename from univ.emp where (ename like'%AR%' and ename like'%IN%')order by ename;
select ename,sum(sal)+sum(case when comm is null then 0 else comm end) from univ.emp group by ename order by ename;
create view v9 as
select ename,(sum(sal)+sum(case when comm is null then 0 else comm end))as allsal from univ.emp group by ename order by ename;
select deptno ,(round(avg(sal)))as avgsal from univ.emp group by deptno;
create view v10 as
select deptno ,(round(avg(sal)))as avgsal from univ.emp group by deptno;
select deptno,max(sal)as maxsal from univ.emp group by deptno order by deptno;
create view v11 as
select deptno,max(sal)as maxsal from univ.emp group by deptno order by deptno;
select deptno,count(*)as count from univ.emp where sal>(select avg(sal) from univ.emp) group by deptno order by deptno;
create view v12 as
select deptno,count(*)as count from univ.emp
where sal>(select avg(sal) from univ.emp) group by deptno order by deptno;
select deptno from univ.emp where(select count(*)from univ.emp)>1 order by deptno;
create view v13 as
select deptno from univ.emp where(select count(*)from univ.emp)>1 order by deptno;
select * from univ.emp where sal>(select sal from univ.emp where ename = 'JONES')order by empno;
create view v14 as
select * from univ.emp where sal>(select sal from univ.emp where ename = 'JONES')order by empno;
select ename,mgr from univ.emp order by ename;
create view v15 as
select ename,mgr from univ.emp order by ename;
select * from univ.emp a where a.hiredate<(select hiredate from univ.emp b where(b.empno=a.mgr));
create view v16 as
select * from univ.emp a where a.hiredate<(select hiredate from univ.emp b where(b.empno=a.mgr));
select b.dname,a.empno,a.ename from univ.dept b left outer join univ.emp a on a.deptno=b.deptno order by b.dname,a.empno;
create view v17 as
select b.dname,a.empno,a.ename from univ.dept b left outer join univ.emp a on a.deptno=b.deptno order by b.dname,a.empno;
select a.ename,b.dname from univ.emp a left outer join univ.dept b
on a.deptno=b.deptno where job='clerk' order by a.ename;
create view v18 as
select a.ename,b.dname from univ.emp a
left outer join univ.dept b on a.deptno=b.deptno where job='clerk' order by a.ename;
select job,sal from univ.emp where sal>35000 order by job desc;
create view v19 as
select job,sal from univ.emp where sal>35000 order by job desc;
select b.dname,a.empno,a.ename from univ.dept b left outer join univ.emp a on b.deptno=a.deptno where b.dname='SALES' order by ename;
create view v20 as
select b.dname,a.empno,a.ename from univ.dept b left outer join univ.emp a on b.deptno=a.deptno where b.dname='SALES' order by ename;
select * from univ.emp where sal>(select avg(sal) from univ.emp) order by ename;
create view v21 as
select * from univ.emp where sal>(select avg(sal) from univ.emp) order by ename;
select * from univ.emp where job=(select job from univ.emp where ename='SCOTT') order by ename;
create view v22 as
select * from univ.emp where job=(select job from univ.emp where ename='SCOTT') order by ename;
select * from univ.emp where sal>(select max(sal) from univ.emp where deptno=30) order by ename;
create view v23 as
select * from univ.emp where sal>(select max(sal) from univ.emp where deptno=30) order by ename;
select * from univ.emp where sal>all(select sal from univ.emp where deptno=30) order by ename;
create view v24 as
select * from univ.emp where sal>all(select sal from univ.emp where deptno=30) order by ename;
select dname 部门名称,count(ename) 部门人数,round(avg(sal)) 平均工资 from univ.dept d left outer join univ.emp e on d.deptno=e.deptno group by d.dname order create view v25 as
select dname 部门名称,count(ename) 部门人数,round(avg(sal)) 平均工资 from univ.dept d left outer join univ.emp e on d.deptno=e.deptno group by d.dname order by dname;
select d.dname,e.ename,e.sal from univ.dept d left outer join univ.emp e on
e.deptno=d.deptno order by ename;
create view v26 as
select d.dname,e.ename,e.sal from univ.dept d left outer join univ.emp e on
e.deptno=d.deptno order by ename;
select d.dname,d.LOC,e.* from(select deptno,count(ename) emps,sum(sal)+sum(case when comm is null then 0 else comm end) incomm from univ.emp group by deptno) e,univ.dept d where e.deptno(+)=d.deptno;
create view v27 as
select d.dname,d.LOC,e.* from(select deptno,count(ename) emps,sum(sal)+sum(case when comm is null then 0 else comm end) incomm from univ.emp group by deptno) e,univ.dept d where e.deptno(+)=d.deptno;
select job,min(sal) 最低工资 from univ.emp group by job order by job;
create view v28 as
select job,min(sal) 最低工资 from univ.emp group by job order by job;
select ename,sal,deptno from univ.emp where sal in(select min(sal) from univ.emp where job='manager' group by deptno);
create view v29 as
select ename,sal,deptno from univ.emp where sal in(select min(sal) from univ.emp where job='manager' group by deptno);
select dname,deptno from univ.dept where deptno=(select deptno from univ.emp group by deptno having avg(sal)>=all(select avg(sal) from univ.emp group by deptno));
create view v30 as
select dname,deptno from univ.dept where deptno=(select deptno from univ.emp group by deptno having avg(sal)>=all(select avg(sal) from univ.emp group by deptno));
select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno) where c.REGION !='North America'order by ename;
create view v31 as
select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where c.REGION !='North America'order by ename;
(select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where c.REGION='East Asia')
intersect
(select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where e.sal>=all(select e.sal from univ.emp))order by ename;
create view v32 as
(select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where c.REGION='East Asia')
intersect
(select e.ename from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where e.sal>=all(select e.sal from univ.emp))order by ename;
select c.CID from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where e.job='president';
create view v33 as
select c.CID from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where e.job='president';
select c.cname from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where avg(e.sal)>=all(select avg(e.sal) from univ.emp group by deptno);
create view v34 as
select c.cname from univ.dept d left outer join univ.countries c on (d.CID=c.CID)left join univ.emp e on(d.deptno=e.deptno)
where avg(e.sal)>=all(select avg(e.sal) from univ.emp group by deptno);
select dname 部门名称,count(ename) 部门人数 from univ.dept d left outer join univ.emp e on d.deptno=e.deptno group by d.dname order by dname;
create view v35 as
select dname 部门名称,count(ename) 部门人数 from univ.dept d left outer join univ.emp e on d.deptno=e.deptno group by d.dname order by dname;
(select s.ID,t.grade,s.name from univ.student s left outer join univ.takes t on(s.ID=t.ID)
where grade<20)
intersect
(select s.ID,t.grade,s.name from univ.student s left outer join univ.takes t on(s.ID=t.ID)
where year=2016)order by ID;
create view v36 as
(select s.ID,t.grade,s.name from univ.student s left outer join univ.takes t on(s.ID=t.ID)
where grade<20)
intersect
(select s.ID,t.grade,s.name from univ.student s left outer join univ.takes t on(s.ID=t.ID)
where year=2016)order by ID;
create view v37 as
select ID,name from univ.student where status_date=2013 and status=0 order by ID;
38. 列出英语系与数学系的教师的姓名与 工资,并按姓名排序。(三种方式)
(select name,salary,dept_name from univ.instructor where dept_name='英语')
union
(select name,salary,dept_name from univ.instructor where dept_name='数学')order by name;
create view v38_1 as
(select name,salary from univ.instructor where dept_name='英语')
union
(select name,salary from univ.instructor where dept_name='数学')order by name;
select name,salary from univ.instructor where dept_name='英语' or dept_name='数学'order by name;
create view v38_2 as
select name,salary from univ.instructor where dept_name='英语' or dept_name='数学'order by name;
(select name,salary,dept_name from univ.instructor where dept_name='英语')
union all
(select name,salary,dept_name from univ.instructor where dept_name='数学')order by name;
create view v38_3 as
(select name,salary,dept_name from univ.instructor where dept_name='英语')
union all
(select name,salary,dept_name from univ.instructor where dept_name='数学')order by name;
select name,salary,dept_name from univ.instructor where(dept_name !='英语' and dept_name !='数学' and name like '李%' )order by name;
create view v39 as
select name,salary,dept_name from univ.instructor where(dept_name !='英语' and dept_name !='数学' and name like '李%' )order by name;
select title,credits from univ.course where title like '%计算机%' order by title;
create view v40 as
select title,credits from univ.course where title like '%计算机%' order by title;
select ID,name,CLASS_NAME from univ.student where (name like '武%' and length(name)=3)order by name;
create view v41 as
select ID,name,CLASS_NAME from univ.student where (name like '武%' and length(name)=3)order by name;
(select ID,name,class_name from univ.student where (name like '武%' or name like '%军'))
intersect
(select ID,name,class_name from univ.student where length(name)=3)
order by name;
create view v42 as
(select ID,name,class_name from univ.student where (name like '武%' or name like '%军'))
intersect
(select ID,name,class_name from univ.student where length(name)=3)
order by name;
select name,class_name from univ.student where name like '_日%' order by name;
create view v43 as
select name,class_name from univ.student where name like '_日%' order by name;
select ID,grade from univ.takes where(course_id ='CS013' and grade < 60) order by ID;
create view v44 as
select ID,grade from univ.takes where(course_id ='CS013' and grade < 60) order by ID;
select * from univ.course where credits=5 order by title;
create view v45 as
select * from univ.course where credits=5 order by title;
select dept_name from univ.department where(select count(*) from univ.student)>0
order by dept_name desc;
create view v46 as
select dept_name from univ.department where(select count(*) from univ.student)>0
order by dept_name desc;
select * from univ.student where (class_name like '2012级软件工程%' and CCREDIT=99)order by ID;
create view v47 as
select * from univ.student where (class_name like '2012级软件工程%' and CCREDIT=99) order by ID;
create or replace view v48 as
select dept_name
from univ.class,univ.major
where class_name='2017级软件工程1班'
and univ.class.major_name=univ.major.major_name;
select s.ID,s.name,year,t.course_id from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='CS013' and s.name like '李%' and year =2015)order by s.ID;
create view v49 as
select s.ID,s.name,year,t.course_id from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='CS013' and s.name like '李%' and year =2015)order by s.ID;
select s.ID,s.name,year,t.course_id,t.course_id,t.grade from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='CS001' and s.ID like '%27' and year =2017)order by s.ID;
create view v50 as
select t.ID,s.name,year,t.course_id,t.grade from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='CS001' and s.ID like '%27' and year =2017)order by s.ID;
select t.ID,c.title,t.course_id,t.grade,c.credits from univ.takes t left outer join univ.course c on(t.course_id=c.course_id)
where(c.title='C Programming' and t.ID like '%9' and year =2016)order by t.ID;
create view v51 as
select t.ID,c.title,t.course_id,t.grade,c.credits from univ.takes t left outer join univ.course c on(t.course_id=c.course_id)
where(c.title='C Programming' and t.ID like '%9' and year =2016)order by t.ID;
select t.ID,s.name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='SE002' and grade<60 and class_name like '2017级软件工程%')order by s.ID;
create view v52 as
select t.ID,s.name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='SE002' and grade<60 and class_name like '2017级软件工程%')order by s.ID;
select t.ID,s.name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='PH001' and grade<60 and year =2018 and semester='上')order by s.ID;
create view v53 as
select t.ID,s.name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='PH001' and grade<60 and year =2018 and semester='上')order by s.ID;
select i.ID,i.name,year,t.course_id,c.title
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(c.title='数据结构' and year =2018 and semester='上')order by i.ID;
create view v54 as
select i.ID,i.name,year,t.course_id,c.title
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(c.title='数据结构' and year =2018 and semester='上')order by i.ID;
select i.ID,i.name,year,t.course_id,c.title
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(i.name='叶映岚' and year =2018 and semester='上')order by c.title;
create view v55 as
select i.ID,i.name,year,t.course_id,c.title
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(i.name='叶映岚' and year =2018 and semester='上')order by c.title;
(select distinct i.ID,i.name,year
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
minus
(select distinct i.ID,i.name,year
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(i.dept_name='计算机' and year =2018 and semester='上'))) order by name;
create view v56 as
(select distinct i.ID,i.name,year
from univ.teaches t left outer join univ.instructor i on(t.ID=i.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(i.dept_name='计算机' and year =2018 and semester='上')) order by i.ID;
(select t.ID,s.name,t.course_id
from univ.takes t join univ.student s on(t.ID=s.ID)
where t.course_id='CS013')
minus
(select t.ID,s.name,t.course_id
from univ.takes t join univ.student s on(t.ID=s.ID)
where t.course_id='CS021')
order by name;
create view v57 as
(select t.ID,s.name,t.course_id
from univ.takes t join univ.student s on(t.ID=s.ID)
where t.course_id='CS013')
minus
(select t.ID,s.name,t.course_id
from univ.takes t join univ.student s on(t.ID=s.ID)
where t.course_id='CS021')
order by name;
select t.ID,s.name,s.class_name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='G0001' and year =2018 and semester='上')order by s.ID;
create view v58 as
select t.ID,s.name,s.class_name,year,t.course_id,t.grade
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
where(t.course_id='G0001' and year =2018 and semester='上')order by s.ID;
create or replace view v59 as
select distinct id,class_name
from univ.student natural join univ.takes
where course_id='G0001'
and id in (select id
from univ.takes
where course_id='PH001')
and year=2018
and semester ='上'
order by id;
select distinct t.ID,s.name,s.class_name
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(c.MAJOR_NAME='化学' and s.class_name like '2013级%' and s.name like '陈%')order by ID;
create view v60 as
select distinct t.ID,s.name,s.class_name
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(c.MAJOR_NAME='化学' and s.class_name like '2013级%' and s.name like '陈%')order by ID;
(select distinct t.ID,s.name,s.class_name
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(t.ID like '%09'and class_name like '%计算机%'))
intersect
(select distinct t.ID,s.name,s.class_name
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where t.course_id in(select t.course_id from univ.takes where t.ID =2015020101));
create view v61 as
select distinct t.ID,s.name,s.class_name
from univ.takes t left outer join univ.student s on(t.ID=s.ID)
left outer join univ.course c on(t.course_id=c.course_id)
where(t.ID like '%09'and class_name like '%计算机%');
select count(name)from univ.student;
create view v62 as
select count(name) 学生总数 from univ.student;
select count(ID) G学生人数 from univ.takes where course_id='G0001'
create view v63 as
select count(ID) G学生人数 from univ.takes where course_id='G0001';
select max(count(ID)) 最多选课人数 from univ.takes group by ID ;
create view v62 as
select max(count(course_id)) 最多选课人数 from univ.takes group by course_id ;
select * from univ.student
where ID in (select ID from univ.takes group by ID having avg(grade)>85) order by ID;
create view v65 as
select * from univ.student
where ID in (select ID from univ.takes group by ID having avg(grade)>85) order by ID;
select sum(credits) 已获学分 from univ.course
where course_id in(
select course_id from univ.takes where ID=2017010101);
create view v66 as
select sum(credits) 已获学分 from univ.course
where course_id in(
select course_id from univ.takes where ID=2017010101);
select year 年份,count(*) 选修CS013人数 from univ.takes where course_id='CS013' group by year order by year desc;
create view v69 as
select year 年份,count(*) 选修CS013人数 from univ.takes where course_id='CS013' group by year order by year desc;
select year 年份,count(*) 选修人数 from univ.takes group by year order by year desc;
create view v68 as
select year 年份,count(*) 选修人数 from univ.takes group by year order by year desc;
查询每年选修了CS013号课程的学生人数,列出年份与人数,按年份升序排列。
查询2010年5月以来的招生情况,列出年份与人数,按年份升序排列。学号的前4位为入学年份。
drop view v71;
create or replace view v71 as
select year,semester,count(distinct id) as rs
from univ.takes
where year>2015
group by year,semester;
select year 年份,semester,count(*) 选修人数 from univ.takes where year> 2015 group by year,semester order by year,semester desc;
create view v71 as
select year 年份,semester,count(*) 选修人数 from univ.takes where year> 2015 group by year,semester order by year,semester desc;
select name 姓名,count(i.ID) 授课门数
from univ.teaches t left join univ.instructor i on (i.ID=t.ID)
where(year=2008 and dept_name='计算机')group by name order by name;
create view v72 as
select name 姓名,count(i.ID) 授课门数
from univ.teaches t left join univ.instructor i on (i.ID=t.ID)
where(year=2008 and dept_name='计算机')group by name order by name;
select name 姓名,count(i.ID) 学生人数
from univ.teaches t left join univ.instructor i on (i.ID=t.ID)
where(year>2014 and dept_name='计算机')group by name order by name;
select class_name 班级,count(t.ID) 班级人数
from univ.takes t left join univ.student s on (t.ID=s.ID)
left join univ.course c on(t.course_id=c.course_id)
where (t.year=2018 and title like '大学物理%') group by class_name order by class_name;
create view v74 as
select class_name 班级,count(t.ID) 班级人数
from univ.takes t left join univ.student s on (t.ID=s.ID)
left join univ.course c on(t.course_id=c.course_id)
where (t.year=2018 and title like '大学物理%') group by class_name order by class_name;
select class_name 班级,count(t.ID) 班级人数
from univ.takes t left join univ.student s on (t.ID=s.ID)
where (t.year=2017 and course_id='G0001') group by class_name order by class_name;
create view v75 as
select class_name 班级,count(t.ID) 班级人数
from univ.takes t left join univ.student s on (t.ID=s.ID)
where (t.year=2017 and course_id='G0001') group by class_name order by class_name;