数据查询

数据查询

仅供参考

  1. 检索dept中所有的记录,并按部门名称排序。
create view v01 as
select * from univ.dept order by dname;
  1. 列出工资(SAL)在15000到25000之间的所有员工的ENAME,SAL,并按ENAME排序。
create or replace view v02 as 
select ename,sal from univ.emp
where sal between 15000 and 25000 
order by ename;
  1. 显示DEPT表中的部门编号和部门名称,并按部门名称排序。
create view v03 as
select deptno,dname from univ.dept
order by dname;
  1. 显示所有不同的工作类型,并降序排序。
create view v04 as
select distinct job from univ.emp
order by job desc;
  1. 列出没有提成(comm)的所有职工的姓名与工资,并按姓名排序。
create view v05 as
select ename,sal from univ.emp
where comm is null order by ename;
  1. 列出部门编号是20,工作是“clerk”(办事员)的职工信息,并按姓名排序。
create view v06 as
select * from univ.emp
where job='clerk' and deptno=20 order by ename;
  1. 显示姓名中包含AR或IN的职工姓名,并按姓名排序。
create view v07 as 
select ename from univ.emp
where ename like '%AR%' or ename like '%IN%'  
order by ename;
  1. 显示姓名中包含AR和IN的职工姓名,并按姓名排序。
create view v08 as 
select ename from univ.emp
where ename like '%AR%' and ename like '%IN%'
order by ename;
  1. 显示所有职工的姓名和各项收入总和(sal+comm),并按姓名排序。
create view v09 as 
select ename, sal+nvl(comm,0)as incomm
from univ.emp order by ename;
  1. 查询每个部门的平均工资(用round函数取整),并按部门编号排序。
create view v10 as
select deptno,round(avg(sal)) as avgsal
from univ.emp group by deptno
order by deptno;
  1. 查询出每个部门中工资最高的职工,并按部门编号排序。若同一部门有多个最高工资者,再按职工号排序。
create view v11 as 
select a.*
from univ.emp a,(select max(sal) as maxsal from univ.emp group by deptno) b
where a.sal=b.maxsal order by deptno,empno;
create view v11 as
select * 
from univ.emp where sal in (select max(sal) from univ.emp group by deptno)
order by deptno,empno;
  1. 查询出每个部门比本部门平均工资高的职工人数,并按部门编号排序。
create view v12 as
select a.deptno,count(*) as num
from univ.emp a,(select avg(sal) as avgsal,deptno from univ.emp group by deptno) b
where a.sal>b.avgsal and a.deptno=b.deptno group by a.deptno
order by a.deptno;
  1. 列出至少有一个职工的所有部门,并按部门编号排序。
create view v13 as 
select a.* 
from univ.dept a , (select deptno from univ.emp group by deptno having count(deptno) >=1 ) b
where a.deptno=b.deptno
order by a.deptno;

create view v13 as 
select *
from univ.dept 
where deptno in(select deptno from univ.emp group by deptno having count(deptno)>=1)
order by deptno;
  1. 列出工资比“JONES”多的所有职工,并按职工编号排序。
create or replace view v14 as
select a.* 
from univ.emp a,(select sal from univ.emp where ename='JONES') b
where a.sal>b.sal
order by empno;

create view v14 as
select empno,ename 
from univ.emp
where sal>(select sal from univ.emp where ename='JONES')
order by empno;
  1. 列出所有职工的姓名及其直接上级的姓名,并按职工姓名排序。
create view v15 as
select a.ename,b.ename as boss
from univ.emp a,(select empno,ename from univ.emp) b
where a.mgr=b.empno
order by a.ename;
  1. 列出受雇日期早于其直接上级的所有职工,并按姓名排序。
create or replace view v16 as
select  * 
from univ.emp a
where a.hiredate<(select hiredate from univ.emp where a.mgr=univ.emp.empno)
order by ename;
  1. 列出部门名称和这些部门职工的编号、姓名,要求同时列出那些没有职工的部门,并按部门名称和职工编号排序。
create view v17 as
select dname,empno,ename
from univ.dept natural left outer join univ.emp
order by dname,empno;
  1. 列出所有办事员的姓名及其部门名称,并按姓名排序。
create view v18 as
select ename,dname
from univ.emp natural join univ.dept
where job='clerk';
  1. 列出最低工资大于35000的各种工作类型,并降序排序。
create view V19 as
select job
from univ.EMP
where sal>35000
order by job desc;
  1. 列出在销售部(“sales”)工作的职工的姓名,并按姓名排序。
create view v20 as
select ename
from univ.emp natural join univ.dept
where dname='SALES'
order by ename;
  1. 列出工资高于公司平均工资的所有职工,并按姓名排序。
create or replace view v21 as
Select a.*
From univ.emp a,(select avg(sal) as avgsal from univ.emp) b
Where sal>avgsal
Order by a.ename;
  1. 列出与“SCOTT”从事相同工作的所有职工,并按姓名排序。
create or replace view v22 as
select * 
from univ.emp
where job=(select job from univ.emp where ename='SCOTT')
order by ename;
  1. 列出工资高于“30号部门的最高工资”的职工姓名和工资,并按姓名排序。
create view v23 as
select ename,sal
from univ.emp
where sal>(select max(sal) from univ.emp where deptno=30)
order by ename;
  1. 列出工资高于在部门30工作的所有职工的工资的职工姓名和工资,并按姓名排序。
create view v24 as
Select ename,sal
From univ.emp a,(select max(sal) as msal from univ.emp where deptno=30) b
Where a.sal>b.msal
Order by ename;
  1. 列出在每个部门工作的职工数量、平均工资(用round函数取整),并按部门编号排序。
create view v25 as
select deptno,count(*) as num,round(avg(sal)) as avgsal 
from univ.emp 
group by deptno
order by deptno;
  1. 列出所有职工的姓名、部门名称和工资,并按姓名排序。
create view v26 as
select ename,dname,sal
from univ.emp natural join univ.dept
order by ename;
  1. 列出所有部门的详细信息和部门人数,并按部门编号排序。
create or replace view v27 as
select univ.dept.*,nvl((select count(EMPNO)
from univ.EMP
group by DEPTNO
having univ.EMP.DEPTNO = univ.DEPT.DEPTNO),0) RS
from univ.DEPT 
order by deptno;
  1. 列出职工中各种工作类型的最低工资,并按工作类型排序。
create view v28 as
select job,min(sal) as minsal
from univ.emp
group by job
order by job;
  1. 列出各个部门的经理(“manager”)的最低工资。
create or replace view V29 as
select min(SAL) as min_sal
from univ.EMP natural join univ.DEPT
where SAL in(select SAL from univ.EMP where job='manager');
  1. 列出平均工资最高的部门的名称。
create view v30 as
select dname
from univ.dept a, (select deptno from univ.emp group by deptno 
having avg(sal)>=all(select avg(sal) from univ.emp group by deptno)) b
where a.deptno=b.deptno;
  1. 列出不在北美工作职工的姓名,并按姓名排序。
create view V31 as
select ENAME
from univ.EMP natural join univ.countries natural join univ.DEPT
where REGION!='North America'
order by ENAME;
  1. 列出在亚洲工作且工资最高的职工的姓名,并按姓名排序。
create view v32 as
select ename,sal
from univ.emp natural left join univ.dept natural left join univ.countries
where deptno=20 and sal in (select max(sal) from univ.emp group by deptno);
  1. 列出总裁(president)工作部门所在的国家。
create view v33 as
select cname
from univ.countries natural join univ.dept natural join univ.emp
where job='president';
  1. 列出平均工资最高的国家。
create or replace view V34 as
select cname
from univ.countries natural join univ.dept,
(select deptno from univ.emp group by deptno 
having avg(sal)>=all(select avg(sal) from univ.emp group by deptno)) b
where univ.dept.deptno=b.deptno;
  1. 列出各部门名称及其人数,含没有职工的部门,并按部门名称排序。
create view V35 as
select DNAME,nvl((select count(EMPNO)
from univ.EMP
group by DEPTNO
having univ.EMP.DEPTNO = univ.DEPT.DEPTNO),0) EMPS
from univ.DEPT
order by dname;
  1. 查询2016年考试成绩不到20分的学生的学号,按学号排列。
create view v36 as
select id from univ.takes
where year =2016 and grade<20
order by id;
  1. 查询到2018年1月该毕业却没有毕业的学生的学号,按学号排列。
create view V37 as
select ID
from univ.student
where status_date<=2013
and status =0
order by ID;
  1. 列出英语系与数学系的教师的姓名与 工资,并按姓名排序。
create view v38 as
select name,salary from univ.instructor
where dept_name in('数学','英语') order by name;
  1. 列出英语系与数学系外其他系的姓“李”的教师姓名与工资,并按姓名排序。
create view v39 as
select name, salary
from univ.instructor
where name like '李%' and dept_name not in('数学','英语')
order by name;
  1. 列出名称中含有"计算机"的课程的名称与学分,并按课程名排序
create view v40 as
select title,credits
from univ.course
where title like '%计算机%'
order by title;
  1. 列出所有姓名以"武"开头且只有3个字的学生的学号、姓名与所在班级,并按姓名排序
create view v41 as
select id,name,class_name from univ.student
where name like '武__'
order by name; 
  1. 列出所有姓名以"李"开头、以"军"结束且只有3个字的学生的学号、姓名,并按姓名排序
create view v42 as
select id,name from univ.student
where name like '李_军'
order by name;
  1. 查询所有姓名中第2个字为"日"的学生的姓名与所在班级,并按姓名排序。
create or replace view v43 as
select name,class_name from univ.student
where name like '_日%'
order by name;
  1. 列出2017年下学期选修了CS013号课程却没有及格的学生的学号及其成绩,并按学号排序。
create view v44 as
select id,grade from univ.takes
where course_id='CS013' and semester='下' and year=2017 and grade <60
order by id;
  1. 给出有5个学分的课程的名称,并按名称排序。
create view v45 as 
select title from univ.course
where credits=5
order by title;
  1. 查询专业表中有哪些系,给出系名,按升序排列
create view v46 as
select dept_name
from univ.major
order by dept_name asc;
  1. 查询软件工程专业2016年毕业的学生的学号和姓名,并按学号排序。
create view v47 as
select id,name 
from univ.student
where status=1 and status_date=2016 and
class_name in(select class_name from univ.class where major_name='软件工程')
order by id;
  1. 查询2017级软件工程1班是哪个系的,给出系名。
create view v48 as
select dept_name
from univ.class natural join univ.major
where class_name='2017级软件工程1班';
  1. 查询2015年选修了CS013号课程且姓“李”的学生的学号与姓名,并按学号排序。
create  view v49 as
select id,name
from univ.takes natural join univ.student
where year=2015 and course_id='CS013' and name like '李%'
order by id;
  1. 查询2017年选修了CS001号课程且学号最后两位为“27”的学生的学号、姓名,以及课程名称及成绩,按学号排序。
create  or replace view v50 as
select id,name,title as course_name,grade
from univ.takes natural join univ.student natural join univ.course
where year=2017 and course_id='CS001' and id like '%27'
order by id;
  1. 查询2016年选修课程名为"C Programming"且学号最后一位为“9”的学生的学号、成绩以及该门课获得的学分,按学号排序。
create view v51 as
select id,grade,credits
from univ.takes natural join univ.course
where year=2016 and title='C Programming' and id like'%9'
order by id;
  1. 查询软件工程专业2017级还没有获得SE002号课程学分的学生的学号与姓名,按学号排序。
create or replace view v52 as
select distinct id,name
from univ.student natural join univ.course natural join univ.takes
where course_id='SE002' and class_name like '2017级软件工程%' and grade<60
order by id;
  1. 查询2018年上学期PH001号课程考试成绩不及格的学生的学号与姓名,按学号排序。
create view v53 as
select id,name
from univ.student natural join univ.takes
where year=2018 and semester='上' and course_id='PH001' and grade<60
order by id;
  1. 查询2018年上学期“数据结构”课程的任课教师名单,按姓名排序。
create view v54 as
select name
from univ.course natural join univ.teaches natural join univ.instructor
where year=2018 and semester='上' and title='数据结构'
order by name;
  1. 查询2018年上学期叶映岚老师所授的课程,按课程名排序。
create view v55 as
select title 
from univ.course natural join univ.teaches natural join univ.instructor
where year=2018 and name='叶映岚'and semester='上'
order by title;
  1. 查询2018年上学期计算机系没有上课的教师名单,按姓名排序。
create view v56 as
select name
from univ.instructor
where name not in
(select name from univ.instructor natural join univ.teaches
where year=2018 and semester='上')  and dept_name='计算机'
order by name; 
  1. 查询选修了CS013号课程但没有选修CS021号两门课程的学生的姓名,按姓名排序。
create view v57 as
select name 
from univ.student natural join univ.takes
where course_id='CS013' and id not in
(select id 
from  univ.takes
where course_id='CS021')
order by name;
  1. 查询2018年上学期选修了G0001号课程的学生的学号和班级,按学号排序。
create view v58 as
select id,class_name
from univ.student natural join univ.takes
where year=2018 and semester='上' and course_id='G0001'
order by id;
  1. 查询2018年上学期同时选修了G0001号和PH001课程的学生的学号和班级,按学号排序。
create view v59 as
select id,class_name
from univ.student natural join univ.takes
where year=2018 and semester='上' and course_id ='G0001' and id in 
(select id 
from univ.student natural join univ.takes
where course_id='PH001' and year=2018 and semester='上')
order by id;
  1. 查询选修了化学专业所开设的全部课程、2017年已经毕业的姓陈的学生的学号、姓名与班级,按学号排序。
create or replace view V60 as 
select ID,NAME,CLASS_NAME
from univ.STUDENT a natural join univ.CLASS b
where not exists
(select * 
from univ.COURSE c
where MAJOR_NAME='化学'
AND not exists
(select * 
from univ.TAKES
where COURSE_ID=c.COURSE_ID
and id=a.id))
and STATUS=1
and STATUS_DATE<=2017
and NAME like '陈%'
order by ID;
  1. 查询学号最后两位为09且至少选修了2015020101(学号)选修的全部课程的学生的学号与姓名,按学号排序。
create view v61 as
select id,name
from univ.student where not exists(
(select course_id from univ.takes where id='2015020101')
minus
(select course_id from univ.takes where student.id=takes.id)) and  id like '%09'
order by id;
  1. 查询学生总人数。
create view v62 as
select count(id) as tatal_student
from univ.student;
  1. 查询选修了G0001号课程的学生人数。
create view v63 as
select count(id) as num
from univ.takes
where course_id='G0001';
  1. 统计选课人数最多的课程有多少人
create view v64 as
select max(id_sum) as num
from (select course_id,count(id) as id_sum from univ.takes group by course_id);
  1. 查询平均成绩大于84的学生学号与姓名,按学号排序。
create view v65 as 
select distinct id,name
from univ.student natural join univ.takes natural join
(select id,avg(grade)as avg_grade from univ.takes group by id)
where avg_grade >84
order by id;
select id,name
from univ.takes natural join univ.student
group by id,name
having avg(grade)>84
order by id;
  1. 查询到目前为止学号为2017010101的学生已经获得的学分数。
create view v66 as
select sum(credits) as sum_credits
from univ.takes natural join univ.course
where ID='2017010101';
  1. 查询2017年CS001号课程成绩超过该课程平均成绩20分的学生的学号与姓名,按学号排序。
create or replace view v67 as
select distinct id,name
from univ.student natural join univ.takes
where year=2017 and course_id='CS001' and
grade>(select avg(grade)+20 from univ.takes where course_id='CS001' and year=2017)
order by id;
  1. 查询每年都修了课程的学生人数,列出年份与人数,按年份升序排列。
create or replace view v68 as
select year,count(distinct id) as total_num
from univ.takes
group by year
order by year asc;
  1. 查询每年都修了CS013号课程的学生人数,列出年份与人数,按年份升序排列。
create view v69 as
select year,count(*) as total_num
from univ.takes
where course_id='CS013'
group by year
order by year asc;
  1. 查询2010年5月以来的招生情况,列出年份与人数,按年份升序排列。学号的前4位为入学年份(使用substr函数去子串)。
create or replace view V70 as
select substr(ID,1,4) as study_year,count(*) as ttotal
from univ.student
group by substr(ID,1,4)
having substr(ID,1,4)>'2009'
order by substr(ID,1,4);
  1. 查询从2016年开始,各个学期选修了课程的学生人数,列出年份、学期与人数,按年份与学期排序。
create or replace view v71 as
select year,semester,count(distinct id) as num
from univ.takes
group by year,semester
having year>=2016
order by year,semester;
  1. 查询2008年计算机系各位教师的姓名(含没有授课任务的教师)及其授课门数,按姓名排序。
create or replace view v72 as
select NAME, nvl(count(COURSE_ID), 0) as RS
from univ.INSTRUCTOR a left join (select ID,COURSE_ID 
from univ.TEACHES 
where YEAR=2008) b
on a.ID=b.ID
where DEPT_NAME = '计算机'
group by a.ID,NAME
order by NAME;
  1. 查询2015年-2018年选修“人工智能”课程的学生人数,列出年份与人数,按年份排序。
create view V73 as
select year,count(*) as ttotal
from univ.takes natural join univ.course
where (year between 2015 and 2018) and title='人工智能'
group by year
order by year;
  1. 查询2018年"大学物理"课程每个班及其平均成绩(用round函数取整),按班名排序。
create or replace view V74 as
select class_name, round(avg(grade),0) as grade_avg
from univ.takes natural join univ.student natural join univ.course
where year=2018 and title like '大学物理%'
group by class_name
order by class_name;
  1. 查询2017年全校性通识课程G0001各专业的选课情况,列出专业名与选修学生人数,按专业名称排序。
create or replace view V75 as
select major_name,count(*) as ttotal
from univ.takes natural join univ.student natural join univ.class
where year=2017 and course_id='G0001'
group by major_name
order by major_name;
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值