Oracle基础业务语句
- 查询员工表所有数据, 并说明使用*的缺点
答案:
select * from emp;
使用*的缺点有:查询出了不必要的列;效率上不如直接指定列名。
- 查询职位(JOB)为’PRESIDENT’的员工的工资
答案:
select * from emp where job = ‘PRESIDENT’;
- 查询佣金(COMM)为0或为NULL的员工信息
答案:
select * from emp where comm = 0 or comm is null;
- 查询入职日期在1981-5-1 到1981-12-31之间的所有员工信息
答案:
select * from emp where hiredate
between to_date(‘1981-5-1’,‘yyyy-mm-dd’) and to_date(‘1981-12-31’,‘yyyy-mm-dd’);
- 查询所有名字长度为4 的员工的员工编号,姓名
答案:
select * from emp where length(ename) = 4;
- 显示10 号部门的所有经理(‘MANAGER’)和20号部门的所有职员(‘CLERK’)的详细信息
答案:
select * from emp where deptno = 10 and job = ‘MANAGER’ or deptno = 20 and job=‘CLERK’;
- 显示姓名中没有’L’字的员工的详细信息或含有’SM’字的员工信息
答案:
select * from emp where ename not like ‘%L%’ or ename like ‘%SM%’;
- 显示各个部门经理(‘MANAGER’)的工资
答案:
select sal from emp where job = ‘MANAGER’;
- 显示佣金(COMM)收入比工资(SAL)高的员工的详细信息
答案:
select * from emp where comm > sal;
- 把hiredate列看做是员工的生日,求本月过生日的员工
答案:
select * from emp where to_char(hiredate, ‘mm’) = to_char(sysdate , ‘mm’);
- 把hiredate列看做是员工的生日,求下月过生日的员工
答案:
select * from emp where to_char(hiredate, ‘mm’) = to_char(add_months(sysdate,1) ,‘mm’);
- 求1982年入职的员工
答案:
select * from emp where to_char(hiredate,‘yyyy’) = ‘1982’;
- 求1981年下半年入职的员工
答案:
select * from emp where hiredate
between to_date(‘1981-7-1’,‘yyyy-mm-dd’) and to_date(‘1982-1-1’,‘yyyy-mm-dd’) - 1;
- 求1981年各个月入职的的员工个数
答案:
select count(*), to_char(trunc(hiredate,‘month’),‘yyyy-mm’)
from emp where to_char(hiredate,‘yyyy’)=‘1981’
group by trunc(hiredate,‘month’)
order by trunc(hiredate,‘month’);
- 查询各个部门的平均工资
答案:
select deptno,avg(sal) from emp group by deptno;
- 显示各种职位的最低工资
答案:
select job,min(sal) from emp group by job;
- 按照入职日期由新到旧排列员工信息
答案:
select * from emp order by hiredate desc;
- 查询员工的基本信息,附加其上级的姓名
答案:
select e.*, e2.ename from emp e, emp e2 where e.mgr = e2.empno;
- 显示工资比’ALLEN’高的所有员工的姓名和工资
答案:
select * from emp where sal > (select sal from emp where ename=‘ALLEN’);
- 显示与’SCOTT’从事相同工作的员工的详细信息
答案:
select * from emp where job = (select * from emp where ename=‘SCOTT’);
- 显示销售部(‘SALES’)员工的姓名
答案:
select ename from emp e, dept d where e.deptno = d.deptno and d.dname=‘SALES’;
- 显示与30号部门’MARTIN’员工工资相同的员工的姓名和工资
答案:
select ename, sal from emp
where sal = (select sal from emp where deptno=30 and ename=‘MARTIN’);
- 查询所有工资高于平均工资(平均工资包括所有员工)的销售人员(‘SALESMAN’)
答案:
select * from emp where job=‘SALESMAN’ and sal > (select avg(sal) from emp);
- 显示所有职员的姓名及其所在部门的名称和工资
答案:
select ename, job, dname from emp e, dept d where e.deptno = d.deptno;
- 查询在研发部(‘RESEARCH’)工作员工的编号,姓名,工作部门,工作所在地
答案:
select empno,ename,dname,loc from emp e, dept d
where e.deptno = d.deptno and danme=‘RESEARCH’;
- 查询各个部门的名称和员工人数
答案:
select * from (select count(*) c, deptno from emp group by deptno) e
inner join dept d on e.deptno = d.deptno;
- 查询各个职位员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位
答案:
select job, count(*) from emp where sal > (select avg(sal) from emp) group by job;
- 查询工资相同的员工的工资和姓名
答案:
select * from emp e where (select count(*) from emp where sal = e.sal group by sal)> 1;
- 查询工资最高的3名员工信息
答案:
select * from (select * from emp order by sal desc) where rownum <= 3;
- 按工资进行排名,排名从1开始,工资相同排名相同(如果两人并列第1则没有第2名,从第三名继续排)
答案:
select e., (select count() from emp where sal > e.sal)+1 rank from emp e order byrank;
- 求入职日期相同的(年月日相同)的员工
答案:
select * from emp e where (select count(*) from emp where e.hiredate=hiredate)>1;
- 查询每个部门的最高工资
答案:
select deptno, max(sal) maxsal from emp group by deptno order by deptno;
- 查询每个部门,每种职位的最高工资
答案:
select deptno, job, max(sal) from emp group by deptno, job order by deptno, job;
- 查询每个员工的信息及工资级别
答案:
select e.*, sg.grade from emp e, salgrade sg where sal between losal and hisal;
- 查询工资最高的第6-10名员工
答案:
select * from (
select e.*,rownum rn from
(select * from emp order by sal desc) e
where rownum <=10)
where rn > 5;
- 查询各部门工资最高的员工信息
答案:
select * from emp e where e.sal = (select max(sal) from emp where (deptno =e.deptno));
- 查询每个部门工资最高的前2名员工
答案:
select * from emp e where
(select count(*) from emp where sal > e.sal and e.deptno = deptno) < 2
order by deptno, sal desc;
- 查询出有3个以上下属的员工信息
答案:
select * from emp e where
(select count(*) from emp where e.empno = mgr) > 2;
- 查询所有大于本部门平均工资的员工信息
答案:
select * from emp e where sal >
(select avg(sal) from emp where (deptno = e.deptno))
order by deptno;
- 查询平均工资最高的部门信息
答案:
select d.*, avgsal from dept d, (select avg(sal) avgsal, deptno from emp group bydeptno) se
where avgsal = (select max(avg(sal)) from emp group by deptno) and d.deptno =se.deptno;
- 查询大于各部门总工资的平均值的部门信息
答案:
select d.*,sumsal from dept d, (select sum(sal) sumsal, deptno from emp group bydeptno) se
where sumsal >(select avg(sum(sal)) from emp group by deptno) and se.deptno =d.deptno;
- 查询大于各部门总工资的平均值的部门下的员工信息
答案:
select e.*,sumsal from emp e, (select sum(sal) sumsal, deptno from emp group bydeptno) se
where sumsal >(select avg(sum(sal)) from emp group by deptno) and se.deptno =e.deptno;
- 查询没有员工的部门信息
答案:
select d.* from dept d left join emp e on (e.deptno = d.deptno) where empno is null;
- 查询当前月有多少天
答案:
select trunc(add_months(sysdate,1),‘month’) - trunc(sysdate,‘month’) from dual;
- 列出最低薪金大于1500的各种工作及此从事此工作的全部雇员人数
答案:
SELECT job,COUNT(empno)
FROM emp
GROUP BY job HAVING MIN(sal)>1500 ;
- 列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级
答案:
SELECT e.empno,e.ename,d.dname,m.ename,s.grade
FROM emp e,dept d,emp m,salgrade s
WHERE sal>(SELECT AVG(sal) FROM emp) AND e.mgr=m.empno AND d.deptno=e.deptno(+)AND e.sal BETWEEN s.losal AND s.hisal ;
- 列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门名称
答案:
SELECT e.ename,e.sal,d.dname FROM emp e,dept d
WHERE sal > ALL (SELECT sal FROM emp WHERE deptno=30) AND e.deptno=d.deptno;
- 列出所有部门的详细信息和部门人数
答案:
SELECT d.dname,d.loc,dt.count
FROM dept d,(SELECT deptno,COUNT(*) count FROM emp GROUP BY deptno) dt
WHERE d.deptno=dt.deptno ;
- 显示非销售人员工作名称以及从事同一工作雇员的月工资的总和,并且要满足从事同一工作的雇员的月工资合计大于$5000,输出结果按月工资的合计升序排列
答案:
SELECT job,SUM(sal) sum
FROM emp
WHERE job<>‘SALESMAN’
GROUP BY job HAVING sum>5000
ORDER BY sum ;
- 客户表a(id name address) 登陆流水表b(id time) 购物流水表c(id time productid productnum)
1.求每个客户的最新登陆时间time,姓名name,客户id?
答案:
select a.id,a.name,d.time as time
from a left join (select id,max(time) as time from b group by id) d
on a.id =d.id ;
2.查最新登陆并且已经购买商品的客户id,name,登陆的时间time(一条sql语句)
答案:
select a.id,a.name,d.time as time
from a,(select id,max(time) as time from b group by id) d
where a.id =d.id
and exists (select * from c where id = a.id);
- 点赞
- 收藏
- 分享
- 文章举报
- Oracle基础:6:通过sqlplus执行sql语句后的结果判断
- 数据库oracle基础语句
- 【笔记】Oracle SQL语句 | 基础篇
- Oracle—SQL基础语句
- oracle基础数据库语句
- Oracle基础多条sql执行在中间的语句出现错误时的控制方式
- Oracle PL/SQL语言基础_控制语句
- 数据库基础sql语句总结(以Oracle为主介绍,并与mysql,sql server做对比)二
- Oracle PL/SQL语句基础学习笔记(下)
- oracle语句基础
- Oracle基础语句
- Oracle中的SQL基础查询语句--下
- Oracle 基础 —SQL语句优化的途径
- oracle学习笔记(一)------oracle基础知识和基本sql语句
- ORACLE基础SQL,常用语句大集合
- Oracle基础sql语句
- Oracle 10g数据库基础之基本DDL和DML语句
- (十九)oracle 基础使用以及sql语句基础
- oracle sql语句优化基础
- oracle基础语句练习