数据库原理—数据查询

数据查询

  • 一.实验目标
  • 二.实验项目
  • 三.实验错误解决方案

一.实验目标

通过理论学习与实验设计,使自己具备如下能力:

  1. 具备分析不同用户的特性,设计用户的数据需求方案并实施的能力。
  2. 选用合适的数据库管理工具承担数据库系统的实施、运行、监控与维护工作的能力。

二.实验项目

  1. 检索dept中所有的记录,并按部门名称排序。
Select * from univ.dept order by dname; 
create view v1 as
select * from univ.dept order by dname;
  1. 列出工资(SAL)在15000到25000之间的所有员工的ENAME,SAL,并按ENAME排序。
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; 
  1. 显示DEPT表中的部门编号和部门名称,并按部门名称排序。
select deptno,dname from univ.dept order by dname; 
create view v3 as select deptno,dname from univ.dept order by dname; 
  1. 显示所有不同的工作类型,并降序排序。
select job from univ.emp order by job desc; 
create view v4 as 
select job from univ.emp order by job desc;
  1. 列出没有提成(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;

  2. 列出部门编号是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;
  1. 显示姓名中包含AR或IN的职工姓名,并按姓名排序。
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;
  1. 显示姓名中包含AR和IN的职工姓名,并按姓名排序。
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;
  1. 显示所有职工的姓名和各项收入总和(sal+comm),并按姓名排序。
 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;
  1. 查询每个部门的平均工资(用round函数取整),并按部门编号排序。
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;
  1. 查询出每个部门中工资最高的职工,并按部门编号排序。若同一部门有多个最高工资者,再按职工号排序。
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;
  1. 查询出每个部门比本部门平均工资高的职工人数,并按部门编号排序。
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;
  1. 列出至少有一个职工的所有部门,并按部门编号排序。
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;
  1. 列出工资比“JONES”多的所有职工,并按职工编号排序。
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;
  1. 列出所有职工的姓名及其直接上级的姓名,并按职工姓名排序。
select ename,mgr from univ.emp order by ename;
create view v15 as 
select ename,mgr from univ.emp order by ename;
  1. 列出受雇日期早于其直接上级的所有职工,并按姓名排序。
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));
  1. 列出部门名称和这些部门职工的编号、姓名,要求同时列出那些没有职工的部门,并按部门名称和职工编号排序。
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;
  1. 列出所有办事员的姓名及其部门名称,并按姓名排序。
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;
  1. 列出最低工资大于35000的各种工作类型,并降序排序。
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;
  1. 列出在销售部(“sales”)工作的职工的姓名,并按姓名排序。
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;
  1. 列出工资高于公司平均工资的所有职工,并按姓名排序。
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;
  1. 列出与“SCOTT”从事相同工作的所有职工,并按姓名排序。
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;
  1. 列出工资高于“30号部门的最高工资”的职工姓名和工资,并按姓名排序。
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;
  1. 列出工资高于在部门30工作的所有职工的工资的职工姓名和工资,并按姓名排序。
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;
  1. 列出在每个部门工作的职工数量、平均工资(用round函数取整),并按部门编号排序。
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;
  1. 列出所有职工的姓名、部门名称和工资,并按姓名排序。
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;
  1. 列出所有部门的详细信息和部门人数,并按部门编号排序。
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;
  1. 列出职工中各种工作类型的最低工资,并按工作类型排序。
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;
  1. 列出各个部门的经理(“manager”)的最低工资。
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);
  1. 列出平均工资最高的部门的名称。
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));
  1. 列出不在北美工作职工的姓名,并按姓名排序。
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;
  1. 列出在亚洲工作且工资最高的职工的姓名,并按姓名排序。
(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;
  1. 列出总裁(president)工作部门所在的国家。
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';
  1. 列出平均工资最高的国家。
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);
  1. 列出各部门名称及其人数,含没有职工的部门,并按部门名称排序。
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;
  1. 查询2016年考试成绩不到20分的学生的学号,按学号排列。
(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;
  1. 查询到2018年1月该毕业却没有毕业的学生的学号,按学号排列。
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;
  1. 列出英语系与数学系外其他系的姓“李”的教师姓名与工资,并按姓名排序。
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;
  1. 列出名称中含有"计算机"的课程的名称与学分,并按课程名排序
    列出名称中含有"计算机"的课程的名称与学分,并按课程名排序
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;
  1. 列出所有姓名以"武"开头且只有3个字的学生的学号、姓名与所在班级,并按姓名排序
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;
  1. 列出所有姓名以"李"开头、以"军"结束且只有3个字的学生的学号、姓名,并按姓名排序
(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;
  1. 查询所有姓名中第2个字为"日"的学生的姓名与所在班级,并按姓名排序。
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; 
  1. 列出2017年下学期选修了CS013号课程却没有及格的学生的学号及其成绩,并按学号排序。
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;
  1. 给出有5个学分的课程的名称,并按名称排序。
select * from univ.course where credits=5 order by title;
create view v45 as
select * from univ.course where credits=5 order by title;
  1. 给出有学生的系的系名,按升序排列
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;
  1. 查询软件工程专业2016年毕业的学生的学号,并按学号排序。
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;
  1. 查询2017级软件工程1班是哪个系的,给出系名。
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;
  1. 查询2015年选修了CS013号课程且姓“李”的学生的学号与姓名,并按学号排序。
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;
  1. 查询2017年选修了CS001号课程且学号最后两位为“27”的学生的学号、姓名,以及课程名称及成绩,按学号排序。
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;
  1. 查询2016年选修课程名为"C Programming"且学号最后一位为“9”的学生的学号、成绩以及该门课获得的学分,按学号排序。
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; 
  1. 查询软件工程专业2017级还没有获得SE002号课程学分的学生的学号与姓名,按学号排序。
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;
  1. 查询2018年上学期PH001号课程考试成绩不及格的学生的学号与姓名,按学号排序。
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;
  1. 查询2018年上学期“数据结构”课程的任课教师名单,按姓名排序。
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;
  1. 查询2018年上学期叶映岚老师所授的课程,按课程名排序。
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;
  1. 查询2018年上学期计算机系没有上课的教师名单,按姓名排序。
(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;
  1. 查询选修了CS013号课程但没有选修CS021号两门课程的学生的姓名,按姓名排序。
(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;
  1. 查询2018年上学期选修了G0001号课程的学生的学号和班级,按学号排序。
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;
  1. 查询选修了化学专业所开设的全部课程、2017年已经毕业的姓陈的学生的学号、姓名与班级,按学号排序。
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;
  1. 查询学号最后两位为09且至少选修了2015020101(学号)选修的全部课程的学生的学号与姓名,按学号排序。
(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 '%计算机%');
  1. 查询学生总人数。
select count(name)from univ.student;
create view v62 as
select count(name) 学生总数 from univ.student;
  1. 查询选修了G0001号课程的学生人数。
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';
  1. 统计选课人数最多的课程有多少人
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 ; 
  1. 查询平均成绩大于84的学生学号与姓名,按学号排序。
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;
  1. 查询到目前为止学号为2017010101的学生已经获得的学分数。
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);
  1. 查询2017年CS001号课程成绩超过该课程平均成绩20分的学生的学号与姓名,按学号排序。
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;
  1. 查询每年选修了课程的学生人数,列出年份与人数,按年份升序排列。
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;
  1. 查询每年选修了CS013号课程的学生人数,列出年份与人数,按年份升序排列。

  2. 查询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;
  1. 查询从2016年开始,各个学期选修了课程的学生人数,按年份与学期排序。
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;
  1. 查询2008年计算机系各位教师的姓名(含没有授课任务的教师)及其授课门数,按姓名排序。
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;
  1. 查询2015年-2018年选修“人工智能”课程的学生人数,按年份排序。
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;
  1. 查询2018年"大学物理"课程每个班及其平均成绩(用round函数取整),按班名排序。
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;
  1. 查询2017年全校性通识课程G0001各专业的选课情况,列出专业名与选修学生人数,按专业名称排序。
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;

三.实验错误解决方案

你可能感兴趣的:(数据库原理,sql,stu,sql,oracle,数据库)