Oracle中有几个原始的表,我们可以以其中的三个表为例,来学习一下Oracle中SQL语句的使用。先看下这几个表的结构:
假设该表中的数据为:
select * from emp where deptno = 20;
select empno,ename,dname from emp,dept where job like 'CLERK' and dept.deptno=emp.deptno;
select * from emp where comm > sal;
select * from emp where comm > (sal*0.2);
select * from emp where (deptno = 10 and job like 'MANAGER') or (deptno = 20 and job like 'CLERK');
select * from emp where job not in ('MANAGER','CLERK') and sal >= 2000 ;
select distinct job from emp where comm is not null;
select ename,(sal+nvl(comm,0)) salcomm from emp;
select * from emp where (comm is null or comm < 100) ;
select * from emp
where hiredate in (select (last_day(hiredate)-1) from emp);
select * from emp where (sysdate - hiredate)/365 >= 10 ;
查询员工信息,要求以首字母大写的方式显示所有员工的姓名
函数INITCAP()是将每个单词的第一个字母大写,其它字母变为小写返回。
select initcap(ename) from emp;
select * from emp where length(ename)= 6 ;
select * from emp where ename not in (select ename from emp where ename like '%S%') ;
select * from emp where ename not like "%S%";
select ename,job,sal from emp where length(ename)- length(replace(ename,'S'))=0;
select ename,job,sal from emp where instr(ename,'S',1)=0;
select * from emp where ename like '_M%';
select ename,job,sal from emp where substr(ename,2,1)='M';
select ename,job from emp where instr(ename,'M')=2;
select substr(ename,1,3) from emp ;
select replace(ename,'s','S') from emp ;
select ename,hiredate from emp order by hiredate asc ;
select ename,job,sal,comm from emp order by job desc,sal asc ;
select ename,to_char(hiredate,'yyyy')||'-'||to_char(hiredate,'mm')
from emp order by to_char(hiredate,'mm'),to_char(hiredate,'yyyy');
select * from emp where to_char(hiredate,'mm') ='02' ;
select ename,floor((sysdate-hiredate)/365)||'年'
||floor(mod((sysdate-hiredate),365)/30)||'月'
||ceil(mod(mod((sysdate-hiredate),365),30))||'天'
from emp ;
select * from dept
where deptno in (select deptno from emp);
–先查出SMITH工资,作为一个临时查询结果
select sal from emp where ename like 'SMITH'
select * from emp
where sal > (select sal from emp where ename like 'SMITH') ;
–先创建一个临时查询的表,用以表示直接上级
select ename staname,mgr from emp
–然后再将两张表进行连接,分别查询其中的一个字段
select staname,ename
from (select ename staname,mgr from emp) t join emp on t.mgr = emp.empno;
select emp1.ename,emp2.ename from emp emp1 join emp emp2 on emp1.empno=emp2.mgr;
select yg.ename,(select ld.ename from emp ld where yg.mgr=ld.empno) from emp yg;
select * from dept
left join emp on emp.deptno = dept.deptno ;
select dept.dname,emp.ename
from emp
left join dept on emp.deptno = dept.deptno
where job='CLERK';
--这种多重条件的,可以按步骤拆分,第一个条件是最低工资
select min(sal) min_sal,job from emp group by job
--然后将第一步骤中生成的临时字段用于条件判断
select job from (select min(sal) min_sal,job from emp group by job) t
where min_sal > 2500 ;
select job from emp group by job having min(sal) >2500;
--第一步,按部门求最低工资
select min(sal) min_sal,deptno from emp group by deptno
--第二步,将第一步的查询结果作为临时表,然后加上<2000的条件
select deptno from
(select min(sal) min_sal,deptno from emp group by deptno)
where min_sal < '2000'
--第三步,第二步已经求出了想要的部门,在emp中查询对应的信息即可
select * from emp where deptno
in (select deptno
from (select min(sal) min_sal,deptno from emp group by deptno)
where min_sal < '2000') ;
select dept.dname,dept.loc,emp.* from dept join emp on dept.deptno = emp.deptno where dept.deptno in(select deptno from emp group by deptno having min(sal) < 2000);
select ename,dname from emp,dept where emp.deptno = dept.deptno and dname = 'SALES';
select ename from emp
where deptno = (select deptno from dept where dname like 'SALES');
select * from emp
where sal > (select avg(sal) from emp) ;
select * from emp
where job in (select job from emp where ename like 'SMITH')
and ename not like 'SMITH';
--先求出30号部门中所有员工的工资
select ename,sal from emp where
sal = any (select sal from emp where deptno = 30) ;
--先求出30号部门中所有员工的工资
select ename,sal from emp
where sal >all (select sal from emp where deptno = 30) ;
select dname,count,avg_sal,avg_date
from dept join (select count(*) count,avg(sal) avg_sal,avg((sysdate-hiredate)/365) avg_date,deptno from emp group by deptno) t
on dept.deptno = t.deptno ;
--第一步,创建一张临时表,其实是给自身tmp表起个别名
select empno staempno,hiredate stahiredate,mgr from emp
--第二步,将实际tmp表和临时表连接,并加入入职时间的判断
select * from emp
where empno in (select staempno from
(select empno staempno,hiredate stahiredate,mgr from emp) t
join emp on t.mgr = emp.empno and stahiredate < hiredate) ;
select distinct emp1.empno,emp1.ename,emp1.deptno
from emp emp1 join emp emp2
on emp1.job=emp2.job and emp1.deptno!=emp2.deptno;
Select dept.*,person_num,avg_sal
from dept,(select count(*) person_num,avg(sal) avg_sal,deptno from emp group by deptno) t
where dept.deptno = t.deptno ;
select dept.*,count(*) dept_num,avg(sal) acg_sal
from dept join emp on dept.deptno=emp.deptno
group by deptno;
select job,min(sal) from emp group by job ;
select max(sal),job,deptno
from emp
group by deptno,job
order by deptno,job ;
select * from emp where deptno=10
or empno in(select mgr from emp where deptno=10);
select deptno,count(*),avg(sal) from emp group by deptno ;
select * from emp where sal in
(select avg(sal) avg_sal from emp group by deptno) ;
select emp.* from emp
join (select deptno,avg(sal) avg_sal from emp group by deptno) t
on emp.deptno=t.deptno
where emp.sal>t.avg_sal;
select emp.*,avg_sal from emp
join (select deptno,avg(sal) avg_sal from emp group by deptno) t
on emp.deptno=t.deptno and sal>avg_sal ;
select * from emp
where sal >any(select sal from emp where deptno=20);
select job,count(*),avg(sal) from emp group by job ;
select deptno,job,count(*),avg(sal)
from emp group by deptno,job
order by deptno,job;
select emp.* from emp
join (select sal,comm from emp where deptno = 10) t
on emp.sal=t.sal
and nvl(emp.comm,0)=nvl(t.comm,0)
and emp.deptno != 10;
select * from emp where deptno
in (select deptno from emp group by deptno having count(*)>5);
select * from dept where deptno in (
select distinct deptno from emp
where deptno not in
(select distinct deptno from emp where sal < 1000)) ;
select * from dept where deptno in (
select distinct deptno from emp group by deptno having min(sal) >1000) ;
select * from emp join dept
on dept.deptno
in (select distinct deptno from emp
where deptno not in (select distinct deptno from emp where sal < 1000))
and dept.deptno=emp.deptno;
select * from dept d left join emp e
on d.deptno=e.deptno
where e.deptno in(
select deptno from emp group by deptno having min(sal)>1000);
select * from dept
where deptno in (
select distinct deptno from emp group by deptno
having min(sal)>900 and max(sal)<3000);
select * from dept
where deptno in (select distinct deptno from emp
where deptno not in (select distinct deptno from emp
where sal not between 900 and 3000)) ;
select * from emp
where deptno in (
select distinct deptno from emp group by deptno
having min(sal)>900 and max(sal)<3000);
select * from emp
where deptno in (select distinct deptno from emp
where deptno not in (select distinct deptno from emp
where sal not between 900 and 3000)) ;
select * from
(select e1.empno,e1.ename,e1.mgr mno, e2.ename mname,e2.deptno
from emp e1 join emp e2 on e1.mgr = e2.empno) t
join dept on t.deptno = dept.deptno ;
select * from dept where deptno in (select deptno from (select count(*) count,deptno from emp group by deptno) where count in (select max(count) from (select count(*) count,deptno from emp group by deptno)));
select * from emp
where empno in (select empno from (
select empno,sal from emp where deptno=30 order by sal desc)
where rownum < 4) ;
select * from emp
where empno in (
select empno from (select empno,rownum num
from (select empno,sal from emp order by sal desc))
where num between 5 and 10 ) ;
insert into emp(empno,ename,sal,deptno,hiredate) values (1357,'oracle',2050,20,to_date('2002年5月10日','yyyy"年"mm"月"dd"日"')) ;
update emp b set sal=(
select sal from (select deptno,avg(sal)+1000 sal from emp group by deptno) a
where a.deptno=b.deptno);
select emp.* from emp where to_char(emp.hiredate, 'yy') like '82';
select emp.* from emp where to_char(emp.hiredate,'yyyy')='1982';
select round(sysdate-e.hiredate)/365, e.ename,e.hiredate
from emp e where round((sysdate-e.hiredate)/365)=32;
select next_day(add_months(e.hiredate,6),2) from emp e ;
select decode(e.mgr,null,'boss','中国好声音') from emp e;
select decode(e.deptno,10,e.sal*1.1,20,e.sal*1.15, e.sal*1.18) 涨工资 ,e.deptno, e.sal
from emp e ;
select ename,sal,emp.deptno from emp
join (select deptno,max(sal) max_sal from emp group by deptno) t
on (emp.deptno = t.deptno and emp.sal = t.max_sal);