表結構
SQL> desc emp
Name Type Nullable Default Comments
-------- ------------ -------- ------- --------
EMPNO NUMBER(4)
ENAME VARCHAR2(10) Y 員工姓名
JOB VARCHAR2(9) Y
MGR NUMBER(4) Y
HIREDATE DATE Y
SAL NUMBER(7,2) Y
COMM NUMBER(7,2) Y
DEPTNO NUMBER(2) Y
查詢語句
--查詢EMP表顯示所有雇員名及其全年收入(月收入=工資+補助),處理NULL行,
--並指定列別名為"年收入"。(NVL(comm,0) comm取空值時用0替代)
select ename,12*(sal+nvl(comm,0)) 年收入 from emp;
--)查詢EMP表顯示工資超過2850的雇員姓名和工資
select ename,sal from emp where sal > 2850;
--查詢EMP表顯示工資不在1500~2850之間的所有雇員及工資
select ename,sal from emp where sal not between 1500 and 2850;
--查詢EMP表顯示代碼為7566的雇員姓名及所在部門代碼
select ename,deptno from emp where mgr=7566;
--查詢EMP表顯示部門10和30中工資超過1500的雇員名及工資
select ename,sal,deptno from emp where deptno in(10,30) and sal > 1500;
select ename,sal,deptno from emp where (deptno=10 or deptno=30) and sal>1500;
--查詢EMP表顯示第2個字符為"A"的所有雇員名其工資。
select ename,sal from emp where ename like '_A%';
select * from emp;
insert into emp(empno,ename,sal,deptno) values(8888,'zhang%san',8888,30);
select ename from emp where ename like '%x%%' escape('x');
--查詢EMP表顯示補助非空的所有雇員名及其補助。
select ename,comm from emp where comm is not null;
--查詢EMP表顯示所有雇員名、工資、雇佣日期,並以雇員名的升序進行排序。
select ename,sal,hiredate from emp order by ename asc;
--查詢EMP表顯示在1981年2月1日到1981年5月1日之間雇佣的雇員名、崗位及雇佣日期,並
--以雇佣日期進行排序。
select ename, job, hiredate
from emp
where hiredate between to_date('1981-2-1', 'yyyy-mm-dd') and
to_date('1981-5-1', 'yyyy-mm-dd')
order by hiredate desc;
--查詢EMP表顯示獲得補助的所有雇員名、工資及補助,並以工資升序和補助降序排序
select ename,sal,comm from emp where comm is not null order by sal asc,comm desc;
--查詢82年員工
select ename, hiredate
from emp
where hiredate between to_date('1982-1-1', 'yy-mm-dd') and
to_date('1982-12-31', 'yy-mm-dd');
--查詢32年工齡的人員
select ename,hiredate,Months_between(sysdate,hiredate)/12 from emp where Months_between(sysdate,hiredate)/12 between 31 and 32;
--顯示員工雇佣期 6 個月后下一個星期一的日期
select ename,hiredate,next_day(add_months(hiredate,6),'星期一') from emp;
--找沒有上級的員工,把mgr的字段信息輸出為 "boss"
select mgr,nvl(to_char(mgr),'boss') from emp;
--為所有人長工資,標准是:10部門長10%;20部門長15%;30
--部門長20%其他部門長18%
select ename,
sal,
deptno,
case
when deptno = 10 then
(sal * 0.1 + sal)
when deptno = 20 then
(sal * 0.15 + sal)
when deptno = 30 then
(sal * 0.2 + sal)
else
(sal * 0.18 + sal)
end
from emp;
--查詢10號部門中編號最新入職的員工,工齡最長的員工的個人信息。
select *from emp where hiredate in
((select max(hiredate)from emp where deptno = 10),
(select min(hiredate) from emp where deptno = 10
));
select * from emp where deptno =10;
--從“software”找到‘f’的位置,用‘*’左右填充到15位,去除其中的‘a’
select instr('software','f') from dual;
select lpad('software',15,'*') from dual;
select rpad('software',15,'*') from dual;
select replace('software','a') from dual;
--查詢員工的獎金,如果獎金不為NULL顯示‘有獎金’,為null則顯示無獎金
select ename,comm,decode(comm,null,'無獎金','有獎金') from emp;
--寫一個查詢顯示當前日期,列標題顯示為Date。再顯示六個月后的日期,下一個
--星期 日的日期,該月最后一天的日期。
select sysdate "Date",add_Months(sysdate,6),next_day(sysdate,'星期日'),last_day(sysdate) from dual;
-- 查詢EMP表按管理者編號升序排列,如果管理者編號為空則把為空的在最前顯示
select * from emp order by mgr asc nulls first;
--求部門平均薪水
select deptno,avg(sal) from emp group by deptno;
--找出每個部門的平均、最小、最大薪水
select deptno,avg(sal),max(sal),min(sal) from emp group by deptno;
select * from emp;
--按部門求出工資大於1300人員的 部門編號、平均工資、最小佣金、最大佣金,並
--且最大佣金大於100
select deptno,avg(sal),min(comm),max(comm) from emp where sal>1300 group by deptno having max(nvl(comm,0))>100;
select e.deptno,avg(e.sal),min(e.comm),max(e.comm) from emp e where e.sal>1300 group by e.deptno having max(nvl(e.comm,0))>100;
--查詢出雇員名,雇員所在部門名稱, 工資等級。
select * from emp e ,dept d where e.deptno = d.deptno;
select * from salgrade;
select e.ename, d.dname, sg.grade
from emp e, dept d, salgrade sg
where e.deptno = d.deptno
and e.sal between sg.losal and sg.hisal;
