SQL操作範例

/*3.將所有員工的工資上浮10%.然後查詢員工姓名、薪水、補助。(emp.sal爲工資,emp.comm爲補助)*/
update EMP 
set
SAL=SAL*1.10
select ename,sal,comm
from EMP
/*4.查看EMP表中部門號爲10的員工的姓名,職位,參加工作時間,工資。
*/
select * from EMP
select ename,job,HIREDATE,sal from EMP
where DEPTNO=10
/*5.查所有已有的職位,要求去除重複項。*/
select job from EMP group by job
/*6.計算每個員工的年薪,並取列名爲Salary of Year
(emp.sal爲員工的月薪),要求輸出員工姓名,年薪。*/
select ename,sal*12 Salaryofyear from EMP
/*7.查詢每個員工每個月拿到的總金額(emp.sal爲工資,emp.comm爲補助)。
(提示:isnull(ex1,ex2)表示如果ex1爲空則返回ex2)*/
select ename,isNULL(sal,0)+ISNULL(comm,0) total from EMP
/*8.顯示職位是主管(manager)的員工的姓名,工資。*/
select ename,sal from EMP where job='manager'
/*9.顯示第3個字符爲大寫O的所有員工的姓名及工資。*/
select ename,sal from EMP where ename like '__O%'
/*10.顯示職位爲銷售員(SALESMAN)或主管(MANAGER)的員工的姓名,工資,職位。*/
select ename,sal,job from EMP where job='SALESMAN'or JOB='MANAGER'
/*11.顯示所有沒有補助的員工的姓名。*/
select ename from EMP where comm is null
/*12.顯示有補助的員工的姓名,工資,補助。*/
select ename,sal,comm from EMP where comm is not null
/*13排序顯示所有員工的姓名,工資(按工資降序方式)*/
select ename,sal from EMP order by sal  desc
/*14.顯示員工的最高工資和最低工資。*/
select max(SAL) 最高工資,min(SAL) 最低工資 from EMP 
/*15.顯示所有員工的平均工資和總計工資。*/
select AVG(sal) 平均工資,SUM(sal) 總計工資 from EMP
/*
16.顯示補助在員工中的發放比例、即有多少比例的員工有補助。
(此題需注意兩個問題:1.select語句中進行除法如何保留小數點後數據。
2.count函數如何處理null型數據。)
*/
select CAST(CAST(count(comm)as float)/CAST(count(*) as float) as numeric(13,12)) from EMP
/*17.顯示每種職業的平均工資。*/
select job,AVG(sal) as average from EMP group by job
/*18顯示每個部門每種崗位的平均工資和最高工資*/
select DEPTNO,job,AVG(sal),max(sal) from EMP group by DEPTNO,job
/*顯示平均工資低於

2500

的部門號,平均)工資及最高工資*/
select deptno,avg(sal),max(sal) from EMP
group by DEPTNO having AVG(sal)<2500
/*以平均工資升序排序*/
select deptno,avg(sal),max(sal) from EMP
group by DEPTNO having AVG(sal)<2500 order by avg(sal) asc
/*21.顯示工資高於2500或崗位爲MANAGER的所有員工的姓名,工資,職位,和部門號。*/
select ename,sal,job,DEPTNO from EMP where sal>2500 or job='manager'
/*22.21.顯示工資高於2500或崗位爲MANAGER的所有員工的姓名,工資,職位,和部門號。*/
select ename,sal,job,DEPTNO from EMP order by deptno asc,sal desc,hiredate asc
/*23*/
select dname,ename from emp,dept where dept.DEPTNO=EMP.DEPTNO
/*24*/
select a.ename
from EMP a join EMP b on b.DEPTNO=a.DEPTNO  where a.JOB='manager' and b.ename='scott'
   /*25*/
 select dname,ename from emp right join DEPT on emp.DEPTNO=dept.DEPTNO
 /*26*/
 select ename,sal,grade from emp join salgrade on sal between losal and 
hisal 
Order by grade 

select * from SALGRADE
/*28*/
select ename ,sal from emp join dept on emp.DEPTNO=DEPT.DEPTNO where DEPT.dname='accounting'
/*29*/
select ename,sal,deptno from emp where sal>(select max(SAL) from EMP where DEPTNO=30)
/*30*/
select ename,sal,deptno from emp where sal>30 union select ename,sal,deptno from emp where job='manager' 
/*31*/
select ename,sal,deptno from emp where sal>2500 intersect select ename,sal,deptno from emp where job='manager' 
/**32*/
select ename,sal,deptno from emp where sal>2500 except select ename,sal,deptno from emp where job='manager' 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章