SQL的基本語法練習
阿新 • • 發佈:2017-07-26
col to_char 通用 模糊 工資 轉換成 截斷 使用 round
select EMPNO,SAL from emp where SAL BETWEEN 1000 and 2000--從enp中獲取sal中1000到2000之間的數據 select ENAME,SAL from emp where SAL >= 1000 and SAL <=2000--從enp中獲取sal中1000到2000之間的數據 select ENAME,SAL from emp where SAl IN(1000,2300); select EMPNO from emp ORDER BY EMPNO;--默認排序 select EMPNO from emp ORDER BY EMPNO ASC--升序 select EMPNO fromemp ORDER BY EMPNO DESC--降序 select LOWER( ENAME) from emp --大小寫的轉換 select * from emp;select * from emp; select trim(ename) from emp--去掉首尾的空格 select * from emp; select * from emp where ENAME = ‘KING‘--從中查找某個指定的 select * from emp; select sal,sal+300 from emp--使用數學運算符“+” select sal,sal*10 from emp--使用數學運算符“*” select sal,10*sal+300 from emp--運算符的優先級 select sal,10*(sal+300)from emp--帶括好的運算 select* from emp; select sal+20 AS sal_o from emp select * from emp; select ENAME || JOB from emp--連接符的運算 ------數值函數----- round--四舍五入 select round(412,-1) from dual--410 select round(412.777,2) from dual--412.78 trunc--截斷 select trunc(412.13,-1) from dual--410 MOD --求余 select MOd(200,11) from emp select Mod(135.23,15) from dual --日期函數 Months_between()---兩個日期相差的月數 select months_between(sysdate,hiredate) from emp Last_day---本月的最後一天 select last_day(sysdate) from dual Next_day()--指定日期的下個日期 select next_day(sysdate,‘星期一‘) from dual ------------------------------------------------- select * from emp order by sal DESC select sal from emp order by sal DESC -------------------------------------------------- Add_months()--向指定日期加上若幹月數 select Add_months(sysdate,0) from dual; -----轉換函數---- to_char--對數字的轉換 select to_char(sysdate,‘yyyy‘) from dual; select to_char(sysdate,‘fmyyyy-mm-dd‘) from dual; select to_char(sal,‘L999,999,999‘) from emp; select to_char(sysdate,‘D‘) from dual--返回星期 To_number--講字符轉換成數字 select to_number(‘13‘)+to_number(‘14‘) from dual---27 To_date Select to_date(‘20090210‘,‘yyyyMMdd‘)from dual ------通用函數-- NVL()函數 select nvl(comm,1) from emp ----------------------------------------------------------------- --模糊查詢like select * from emp select * from emp where job like ‘%*_%‘ -----邏輯運算符 --選擇在部門30中員工的所有信息 select * from emp where deptno=30; --列出職位為(MANAGER)的員工的編號,姓名 select EMPNO,ENAME from emp where job=‘MANAGER‘ --找出獎金高於工資的員工 select *from emp where comm>sal --找出每個員工獎金和工資的總和 select comm+sal,ename from emp --找出部門10中的經理(MANAGER)和部門20中的普通員工(CLERK) select * from emp where (deptno=10 and job=‘MANAGER‘)or(deptno=20 and job=‘CLERK‘) --找出部門10中既不是經理也不是普通員工,而且工資大於等於2000的員工 select *from emp where deptno=10 and job not in(‘MANAGER‘,‘CLERK‘) and sal>=2000; --找出有獎金的員工的不同工作 select distinct job from emp--distinct不同 where comm is not null and comm>0; --找出沒有獎金或獎金低於500的員工 select *from emp where comm <500 or comm is null; --顯示雇員信息姓名,根據其服務年限,將最老的雇員排在最前面 select ename from emp order by hiredate;
SQL的基本語法練習