scott登陸查詢經常使用語句

1、簡單查詢oracle

1.簡單查詢
select * from emp;--查詢表emp中的全部數據
select empno as id,ename as name from emp;--查詢表emp中的empno顯示爲id,ename顯示爲name函數

2.去除重複
select distinct job from emp;--將表emp中的job去重
select distinct job,deptno from emp;--將表emp中的job.deptno去重code

3.字符串的鏈接
select '員工編號是' ||empno || '姓名是' ||ename ||'工做是'||job from emp;--用||能夠將字符之間進行鏈接

4.乘法
select ename,sal *12 from emp;--查詢表emp中的ename,薪資*12
--加減乘除都相似orm

2、限定查詢
1.獎金大於1500的
select * from emp where sal>1500;--用where限定sal大於1500
2.有獎金的
select *from emp where comm is not null;--顯示comm不爲空值的
3.沒有獎金的
select *from emp where comm is null;--顯示comm爲空值的
4.有獎金且大於1500的
select *from emp where sal>1500 and comm is not null;--且用and鏈接
5.工資大於1500或者有獎金的
select *from emp where sal>1500 or comm is not null;--或用or鏈接
6.工資不大於1500且沒獎金的
select *from emp where sal<=1500 and comm is null;
select *from emp where not (sal >1500 or comm is not null);
7.工資大於1500可是小於3000的
select *from emp where sal>1500 and sal<3000;
select *from emp where sal between 1500 and 3000; --between是閉區間,是包含1500和3000的
8.時間區間
select *from emp where hiredate between to_date('1981-01-01','yyyy-MM-dd') and to_date('1981-12-31','yyyy-MM-dd');
9.查詢僱員名字
select *from emp where ename='SMITH';
10.查詢員工編號
select *from emp where empno=7369 or empno=7499 or empno=7521;
select *from emp where empno in(7369,7499,7521);--查詢在()內的數值
select *from emp where empno not in(7369,7499,7521); --not是排除這3個,其餘的均可以查
11.模糊查詢
select *from emp where ename like '_M%'; --第2個字母爲M的
select *from emp where ename like '%M%';--包含字母M的
select *from emp where ename like '%%'; --全查詢
12.不等號的用法
select * from emp where empno !=7369;
select *from emp where empno<> 7369;

3、對結果集排序
1.查詢工資從低到高
select *from emp order by sal asc;--按升序排序時asc可省略
select *from emp order by sal desc,hiredate desc; --當sal列相同時就按hiredate來排序
2.字符函數
select *from dual;--僞表
select 2*3 from dual;
select sysdate from dual;
3.變成大寫
select upper('smith') from dual;
4.變成小寫
select lower('SMITH') from dual;
5.首字母大寫
select initcap('smith') from dual;
6.鏈接字符串
select concat('jr','smith') from dual; --只能在oracle中使用
select 'jr' ||'smith' from dual; --推薦使用
7.截取字符串
select substr('hello',1,3) from dual; --索引從1開始
8.獲取字符串長度
select length('hello') from dual;
9.字符串替換
select replace('hello','l','x') from dual; --把l替換爲x

4、數值函數排序

1.取整
select round(12.234) from dual;--取整的四捨五入 12
select round (12.657,2) from dual; --保留2位小數
select trunc(12.48) from dual;--取整
select trunc(12.48675,2) from dual; --保留2位小數
2.取餘
select mod(10,3) from dual;--10/3取餘 =1
4.日期函數
--日期-數字=日期 日期+數字=日期 日期-日期=數字
5.查詢員工進入公司的週數
select ename,round((sysdate -hiredate)/7) weeks from emp;
6.查詢全部員工進入公司的月數
select ename,round(months_between(sysdate,hiredate)) months from emp;
7.求三個月後的日期
select add_months(sysdate,6) from dual;
select next_day(sysdate,'星期一') from dual; --下星期
select last_day(sysdate) from dual; --本月最後一天
select last_day(to_date('1997-1-23','yyyy-MM-dd')) from dual;
8.轉換函數
select ename ,
to_char(hiredate,'yyyy') 年,
to_char(hiredate,'mm')月,
to_char(hiredate,'dd') 日
from emp;

select to_char(10000000,'$999,999,999') from emp;
select to_number('20')+to_number('80') from dual; --數字相加
9.查詢員工年薪
select ename,(sal*12+nvl(comm,0)) yearsal from emp; --空和任何數計算都是空
10.Decode函數,相似if else if (經常使用)
select decode(1,1,'one',2,'two','no name') from dual;

5、多表查詢
1.直接查詢
select *from dept;
select *from emp,dept order by emp.deptno;
select *from emp e,dept d where e.deptno=d.deptno;
select e.*,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

2.查詢出僱員的編號,姓名,部門編號,和名稱,地址
select e.empno,e.ename,e.deptno,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

3.查詢出每一個員工的上級領導
select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr=e1.empno;

select e.empno,e.ename,d.dname
from emp e,dept d ,salgrade s, emp e1
where e.deptno=d.deptno
and e.sal between s.losal
and s.hisal
and e.mgr=e1.empno;

select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr=e1.empno(+) ;
4.外鏈接索引

select *from emp order by deptno;
--查詢出每一個部門的員工。部門表是全量表,員工表示非全量表,在作鏈接條件時,全量表在非全量表的哪端,那麼鏈接時全量表的鏈接條件就在等號哪端
5.左鏈接
select * from dept d,emp e where d.deptno=e.deptno(+) order by e.deptno;
6.右鏈接
select * from emp e,dept d where e.deptno(+)=d.deptno order by e.deptno;字符串

 

insert into table1(least1,least2) valuse (a,b)--插入a,b到表1的列1列2;
updata table1 set least1=a where least2=b--將表1中的b修改成a;
delete from table1 where least1=a--刪除表1中的數據a;
savepoint a--設置節點a;
rollback to a--回到節點a;it

 

alter table dept rename column name to dname;--修改表dept中的列名name爲dname;
select * from emp for update;--手動修改表格;
drop table dept;--刪除表dept;
rename emp to emp1;--將emp名字修改成emp1;
comment on table emp is 'enployee information';--爲表emp添加註釋「enployee information」;
select * from user_tab_comments where table_name = 'STATUS2';--查看錶STATUS2的註釋(這裏的代表必須大寫);io

create table employees
(ID INTEGER not null,
constraint employees_id_pk primary key (id));
--建立表,列id,主鍵約束table

drop table employees ;--刪除表

alter table order_status2
add constraint order_status2_id_fk
foreign key (id)
references employees(id);--增長外鍵約束

alter table order_status2
drop constraint ORDER_STATUS2_ID_FK ;--刪除約束

create table emp1
as select * from emp ;--建立表emp1使用emp的所有內容

alter table order_status2
add constraint order_status2_status_ck
check ( status in ('Male','Female'));--建立check約束

 

練習題

--1.獲得平均工資大於2000的工做職種
select job,avg(sal)
from emp
group by job
having avg(sal) > 2000
--2.分部門獲得工資大於2000的全部員工的平均工資,
--而且平均工資還要大於2500
select deptno,avg(sal)
from emp
where sal > 2000
group by deptno
having avg ( sal ) > 2500
--3.獲得每月工資總數最少的那個部門的部門編號,部門名稱,部門位置
select dept.deptno,dept.dname,dept.loc,dept1.sal1
from (select * from
(select deptno,sum(sal) sal1
from emp
group by deptno
order by sum(sal) )
where rownum=1) dept1, dept
where dept1.deptno=dept.deptno ;
--4.分部門獲得平均工資等級爲2級(等級表)的部門編號
select *
from ( select *
from (select deptno , avg(sal) sal1
from emp
group by deptno ) depno_avgsal , salgrade
where depno_avgsal.sal1
between salgrade.losal and salgrade.hisal )
where grade = 2 ;
--5.查找出部門10和部門20中,
--工資最高第3名到工資第5名的
--員工的員工名字,部門名字,部門位置
select e.ename,d.dname,d.loc,rno
from(select ename,deptno,rno
from(select ename,deptno,rownum rno
from(select *
from emp
where deptno in (10,20)
order by sal desc))
where rno>=3 and rno<=5) e,dept d
where e.deptno=d.deptno;
--6.查找出收入(工資加上獎金),
--下級比本身上級還高的員工編號,員工名字,員工收入
select e.empno,e.ename,e.sal1
from ( select empno,ename,mgr,sal+nvl(comm,0) sal1
from emp ) e ,
( select empno,ename,sal+nvl(comm,0) sal2
from emp ) b
where e.mgr = b.empno and e.sal1 > b.sal2 ;
--7.查找出職位和'MARTIN' 或者'SMITH'同樣的員工的平均工資
select avg(sal)
from emp
where job in (select job
from emp
where ename = 'MARTIN' or ename = 'SMITH') ;
--8.查找出不屬於任何部門的員工
select *
from emp
where deptno is null ;
--9.按部門統計員工數,
--查處員工數最多的部門的第二名到第五名
select *
from (select b.* , rownum rno
from (select *
from emp
where deptno in ( select deptno
from ( select deptno , count ( empno ) dnum
from emp
group by deptno
order by dnum desc ) a
where rownum =1 )) b
where rownum < = 5 ) c
where c.rno > = 3 ;
--10.查詢出king所在部門的部門號\部門名稱\部門人數
select emp.deptno,dname,count(emp.ename)
from emp inner join dept on emp.deptno=dept.deptno
group by emp.deptno,dname
having emp.deptno=(select deptno
from emp
where ename='KING');
select a.deptno , b.dname , a.donum
from (select deptno,count(deptno) donum
from emp
where deptno = ( select deptno
from emp
where ename = 'KING' )
group by deptno ) a ,dept b
where a.deptno = b.deptno ;

--11.查詢出king所在部門的工做年限最大的員工名字
create view king_dept as
select * from emp
where deptno in(select deptno from emp where ename='KING');
select ename from king_dept
where months_between(sysdate,hiredate)=(select max(months_between(sysdate,hiredate)) from king_dept);

select enamefrom ( select * from emp where deptno = ( select deptno from emp where ename = 'KING' ) order by hiredate )where rownum = 1 ;--12.查詢出工資成本最高的部門的部門號和部門名稱select deptno,dnamefrom deptwhere deptno in ( select deptno from emp group by deptno having sum(sal) in (select max(sum(sal)) from emp group by deptno));--13.顯示全部員工的姓名、工做和薪金,--按工做的降序排序,若工做相同則按薪金排序select ename , job , sal from emp order by job desc , sal ;--14.顯示全部員工的姓名、加入公司的年份和月份,--按受僱日期所在月排序,若月份相同則將最先年份的員工排在最前面select ename 姓名 , to_char(hiredate,'yyyy') 年份 , to_char(hiredate,'mm') 月份 from emp order by 月份 , 年份 ;--15.顯示在一個月爲30天的狀況全部員工的日薪金,忽略餘數select ename , round((sal+nvl(comm,0))/30) 日薪from emp ;--16.找出在(任何年份的)2月受聘的全部員工select ename from emp where to_char(hiredate,'mm') = 2 ;--17.對於每一個員工,顯示其加入公司的天數select ename , round ( sysdate-hiredate)from emp ;--18.顯示姓名字段的任何位置包含"A"的全部員工的姓名select enamefrom empwhere ename like'%A%' ;--19.以年月日的方式顯示全部員工的服務年限select ename,trunc((sysdate-hiredate)/365) year , trunc(mod(sysdate-hiredate,365)/30) mon , trunc(mod(mod(sysdate-hiredate,365),30)) dayfrom emp ;--20.顯示員工的姓名和受僱日期,根據其服務年限,將最老的員工排在最前面select ename, hiredatefrom emp order by hiredate ;

相關文章
相關標籤/搜索