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