scott登錄查詢常用語句

一、簡單查詢

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去重

3.字符串的連接
select '員工編號是' ||empno || '姓名是' ||ename ||'工作是'||job from emp;--用||可以將字符之間進行連接

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

二、限定查詢
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;

三、對結果集排序
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

四、數值函數

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;

五、多表查詢
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;

?

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的注釋(這里的表明必須大寫);

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

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 ename
from ( select *
from emp
where deptno = ( select deptno
from emp
where ename = 'KING' )
order by hiredate )
where rownum = 1 ;
--12.查詢出工資成本最高的部門的部門號和部門名稱
select deptno,dname
from dept
where 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 ename
from emp
where 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)) day
from emp ;
--20.顯示員工的姓名和受雇日期,根據其服務年限,將最老的員工排在最前面
select ename, hiredate
from emp
order by hiredate ;

轉載于:https://www.cnblogs.com/lixun-x/p/8597379.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390040.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390040.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390040.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CSS結構的基礎認知

css的屬性值與html的屬性值用法不相上下&#xff0c;但是css主要分為內聯樣式表和外聯樣式表。 內聯樣式表用法&#xff1a;在html文件中的《head》頭文件中添加<style></style>標簽&#xff0c;在標簽內添加所需的屬性值&#xff0c;例如&#xff1a;<!DOCTYPE…

BZOJ1453: [Wc]Dface雙面棋盤

Time Limit: 10 Sec Memory Limit: 64 MB Submit: 784 Solved: 422 [Submit][Status][Discuss] Description 佳佳有一個 nnn 行 nnn 列的黑白棋盤&#xff0c;每個格子都有兩面&#xff0c;一面白色&#xff0c;一面黑色。佳佳把棋盤平放在桌子上&#xff0c;因此每個格子恰好一…

用戶體驗數據分析 書單_如何使用數據改善用戶體驗設計

用戶體驗數據分析 書單In the current age of technology, if an entrepreneur comes up with a grand idea, chances are they’ll need a pretty sweet website to go along with it. And if they want their idea to really sell, they will also need a website that reall…

推薦11個實用的JavaScript庫

2019獨角獸企業重金招聘Python工程師標準>>> JavaScript 仍然是 2018 年最受歡迎和使用最為廣泛的編程語言&#xff0c;因此 JavaScript 生態系統也會繼續發展壯大。 然而&#xff0c;JavaScript 的標準庫仍然繼續保持“短小精悍”的身材。為了填補標準庫功能方面的…

371. 兩整數之和

371. 兩整數之和 給你兩個整數 a 和 b &#xff0c;不使用 運算符 和 - ???????&#xff0c;計算并返回兩整數之和。 示例 1&#xff1a; 輸入&#xff1a;a 1, b 2 輸出&#xff1a;3 示例 2&#xff1a; 輸入&#xff1a;a 2, b 3 輸出&#xff1a;5 提示&a…

【福利】微信小程序精選Demo合集

小編最近在開發小程序&#xff0c;也讀到了不少優秀的小程序源碼&#xff0c;項目中有些需求可以直接從源碼里粘貼復制過來&#xff0c;雖然這樣做不利于自己獨立編寫代碼&#xff0c;但比較是給公司做項目啊&#xff0c;秉著效率第一的原則&#xff0c;簡直沒有什么比ctrlc,ct…

639. 解碼方法 II

639. 解碼方法 II 一條包含字母 A-Z 的消息通過以下的方式進行了編碼&#xff1a; A -> 1 B -> 2 ... Z -> 26要 解碼 一條已編碼的消息&#xff0c;所有的數字都必須分組&#xff0c;然后按原來的編碼方案反向映射回字母&#xff08;可能存在多種方式&#xff09;。…

[cpyhon源代碼]dict對象原理學習

Cpython 2.7 分支中&#xff0c;dict 對象的源代碼 lookdict 搜索算法 1 static PyDictEntry *2 lookdict(PyDictObject *mp, PyObject *key, register long hash)3 {4 register size_t i;5 register size_t perturb;6 register PyDictEntry *freeslot;7 regis…

熊貓數據集_熊貓邁向數據科學的第一步

熊貓數據集I started learning Data Science like everyone else by creating my first model using some machine learning technique. My first line of code was :通過使用某種機器學習技術創建我的第一個模型&#xff0c;我開始像其他所有人一樣學習數據科學。 我的第一行代…

SQLServer鎖的機制

SQLServer鎖的機制&#xff1a;共享鎖(S)排它鎖(X)更新鎖(U)意向共享 (IS)意向排它 (IX) 意向排它共享 (SIX)架構修改(Sch-M) 架構穩定性(Sch-S)大容量更新&#xff08;BU&#xff09;轉載于:https://www.cnblogs.com/yldIndex/p/8603902.html

你是否具有價值

一個有價值的人往往受歡迎的程度才會高。白天上午花了兩個多小時的時間幫前同事遠程解決了服務器部署時由于防火墻機制問題引起的系統功能失敗的問題。解決完這個問題之后&#xff0c;同事的心情很愉悅&#xff0c;其實我自己的心情也很愉悅&#xff0c;看來人都有幫助別人和被…

為什么選擇做班級管理系統_為什么即使在平衡的班級下準確性也很麻煩

為什么選擇做班級管理系統Accuracy is a go-to metric because it’s highly interpretable and low-cost to evaluate. For this reason, accuracy — perhaps the most simple of machine learning metrics — is (rightfully) commonplace. However, it’s also true that m…

使用Chrome開發者工具調試Android端內網頁(微信,QQ,UC,App內嵌頁等)

使用Chrome開發者工具調試Android端內網頁(微信&#xff0c;QQ&#xff0c;UC&#xff0c;App內嵌頁等) 傳送門轉載于:https://www.cnblogs.com/momozjm/p/9389912.html

517. 超級洗衣機

517. 超級洗衣機 假設有 n 臺超級洗衣機放在同一排上。開始的時候&#xff0c;每臺洗衣機內可能有一定量的衣服&#xff0c;也可能是空的。 在每一步操作中&#xff0c;你可以選擇任意 m (1 < m < n) 臺洗衣機&#xff0c;與此同時將每臺洗衣機的一件衣服送到相鄰的一臺…

netflix的準實驗面臨的主要挑戰

重點 (Top highlight)Kamer Toker-Yildiz, Colin McFarland, Julia GlickKAMER Toker-耶爾德茲 &#xff0c; 科林麥克法蘭 &#xff0c; Julia格里克 At Netflix, when we can’t run A/B experiments we run quasi experiments! We run quasi experiments with various obje…

網站漏洞檢測針對區塊鏈網站安全分析

2019獨角獸企業重金招聘Python工程師標準>>> 目前移動互聯網中&#xff0c;區塊鏈的網站越來越多&#xff0c;在區塊鏈安全上&#xff0c;很多都存在著網站漏洞&#xff0c;區塊鏈的充值&#xff0c;會員賬號的存儲性XSS竊取漏洞&#xff0c;賬號安全&#xff0c;等…

223. 矩形面積

223. 矩形面積 給你 二維 平面上兩個 由直線構成的 矩形&#xff0c;請你計算并返回兩個矩形覆蓋的總面積。 每個矩形由其 左下 頂點和 右上 頂點坐標表示&#xff1a; 第一個矩形由其左下頂點 (ax1, ay1) 和右上頂點 (ax2, ay2) 定義。 第二個矩形由其左下頂點 (bx1, by1) …

微觀計量經濟學_微觀經濟學與數據科學

微觀計量經濟學什么是經濟學和微觀經濟學&#xff1f; (What are Economics and Microeconomics?) Economics is a social science concerned with the production, distribution, and consumption of goods and services. It studies how individuals, businesses, governmen…

NPM 重新回爐

官方教程傳送門( 英文 ) 本文主要是官方文章的精煉,適合想了解一些常用操作的同學們 NPM 是 基于node的一個包管理工具 , 安裝node環境時會自帶安裝NPM. NPM版本管理 查看現有版本 npm -v 安裝最新的穩定版本 npm install npmlatest -g 安裝最新的測試版本 npm install npmn…

1436. 旅行終點站

1436. 旅行終點站 給你一份旅游線路圖&#xff0c;該線路圖中的旅行線路用數組 paths 表示&#xff0c;其中 paths[i] [cityAi, cityBi] 表示該線路將會從 cityAi 直接前往 cityBi 。請你找出這次旅行的終點站&#xff0c;即沒有任何可以通往其他城市的線路的城市。 題目數據…