Oracle基础(简单查询-select)

(因为本篇中的SQL语句都是我查询过的,为了方便,我就不截图了,直接附上SQL语句就行。如果有想要学习的朋友或者想要拿这些题目练练手的都可以看看。如果有不对的地方,希望大家多多指点)

SQL语句的特点:
1)是SQL92/99的ANSI官方标准,只能按照该标准来写,在任何关系型数据库中都可以直接执行
2)SQL语句的关键字不能简写,例如:select,update,where, from
3)大小写不敏感(小写直观,但是建议使用大写,为了方便也可以先写小写再转换成大写)
4)能够对表数据进行增删改查操作
5)必须以分号结束
6)通常称做语句

注释:

使用--符号,设置单号注释
--select * from emp;

使用/* */符号,设置多行注释
/*
select 
* 
from 
emp;
*/

--1、查询emp表的所有内容,*号表示通配符,表示该表中的所有字段,但*号不能和具体字段一起使用
select * from emp;   --(一般不建议使用*,就算查询全部字段,一般也是不用*号)
select empno,ename,sal,deptno from emp;

--2、查询表的时候,大小写不敏感,但提倡大写
SelEct empno,ename,sal,depTno fRom emp;--(可行,但是不建议使用)
--列别名
select empno"编号",ename"姓名",sal"工资",deptno"部门号" from emp;--(两种写法一样,as可以省略)
select empno as "编号",ename as "姓名",sal as "工资",deptno as "部门号" from emp;
--3、查询emp表的不重复工作
select job from emp;--(有重复)
select distinct job from emp;--(不重复 distinct关键字)

--4、查询员工的编号,姓名,月薪,年薪 (月薪*12)
select empno,ename,sal,sal*12 "年薪" from emp;

--5、查询员工的编号,姓名,入职时间,月薪,年薪,年输入(年薪+奖金)
select empno "编号" ,ename "姓名", hiredate "入职时间", sal "月薪", sal*12 "年薪", sal*12+comm "年收入" from emp; 
 
--6、解决null的问题,使用NVL()函数,NVL(a,b):如果a是null,用b替代,如果a是非null,就不用b替代,直接返回a的值(注意:null与具体数字运算时,结果为null)
select NVL(null,10) from emp;      --结果有14行记录
select NVL(null,10) from dual;    -- 结果为1行记录(dual只有一个字段一条记录的表,习惯上称为"伪表")
select empno "编号" ,ename "姓名", hiredate "入职时间", sal "月薪", sal*12 "年薪", sal*12+NVL(comm,0) "年收入" from emp;
--7、使用列别名,查询员工的编号,姓名,月薪,年薪,年收入 (年薪+奖金) (注意:as大小写都可且可以省略AS,别名用双引号)
select empno as "编号" ,ename AS "姓名", hiredate "入职时间", sal "月薪", sal*12 "年薪", sal*12+NVL(comm,0) "年收入" from emp;
(引号可以加可以不加,区别:
	不加双引号的别名不能有空格,加了双引号的别名可以有空格
要加只能加双引号,不能加单引号,因为在oracle中单引号表示字符串类型或者是日期类型)

--8、使用dual哑表或者伪表,使用字符串连接符号||,输出"hello world".在oracle中form是必须写的。
select 'hello' || 'world' "结果" from dual;

--9、使用sysdate,显示系统当前时间,在默认情况下,oracle只显示日期,不显示时间, 格式: 05-2月 -18   (2018/2/05)
select sysdate from dual;

--10、使用字符串连接符号||,显示如下格式信息:****的薪水是****美元
select ename || '的薪水是' || sal || '美元' from emp; 
--带where的查询语句:
--11、查询emp表中20号部门的员工信息
select * from emp where deptno=20;

--12、查询姓名是SMITH的员工,字符串使用''(注意:内容大小写敏感)
select * from emp where ename = 'SMITH';

--13、查询1980年12月17日入职的员工(注意:oracle默认日期格式:DD-MON-RR表示2位的年)
select * from emp where hiredate='17-12月-80';

--14、查询工资大于1500的员工
select * from emp where sal > 1500;

--15、查询工资不等于1500的员工(注意:不等号有两种 !=或<>)
select * from emp where sal != 1500;

--16、查询薪水在1300到1600之间的员工,包括1300和1600
select * from emp where (sal>=1300) and (sal<=1600);  (注意:括号可以省略,添加括号看起来更加直观)
--或 
select * from emp where sal between 1300 and 1600;

--17、查询薪水不在1300到1600之间的员工,不包括1300和1600(not 取相反)
select * from emp where sal not between 1300 and 1600;

--18、查询入职时间在'1981-2月-20'到'1982-1月-23'之间的员工
select * from emp where hiredate between '20-2月-81' and '23-1月-82';
--注意:
--1)对于数值型,小数值在前,大数值在后
--2)对于日期型,(年份、月份、日期)数值小的在前,大的在后


--19、查询20号或30号部门的员工,例如:根据id号,选中的员工,批量删除
select * from emp where (deptno = 20) or (deptno = 30);
--或
select * from emp where deptno in(20,30);

--20、查询不是20号或30号部门的员工
select * from emp where deptno not in(20,30);

--21、查询姓名以大写字母S开头的员工,使用%表示0个,1个或多个字符
select * from emp where ename like 'S%';

--注意:
--凡是精确查询用=符号
--凡是不精确查询用like符号,我们通常叫模糊查询

--22、查询姓名以大写字母N结束的员工
select * from emp where ename like '%N';

--23、查询姓名第一个字母是T,最后一个字母是R的员工
select * from emp where ename like 'T%R';

--24、查询姓名是4个字符的员工,且第二个字母是I,使用_只能表示1个字符,不能表示0个或多个字符
select * from emp where ename like '_I__';

--25、插入一条姓名为'T_TM'的员工,薪水是1200
insert into emp(empno,ename,sal) values(11,'T_TM',1200);

--26、查询员工姓名中含有'_'的员工,使用\转义符,让其后的字符回归本来的意思(like '%\_%' escape '\');
select * from emp where ename like '%\_%' escape '\';

--27、插入一个姓名叫'的员工
insert into emp(empno,ename) values(2222,'''');--(注意:可以自己用select查询 )
--28、插入一个姓名叫''的员工
insert into emp(empno,ename) values(3333,'''''');

--29、查询佣金为null的员工
select * from emp where comm is null;

--注意:null不能参数=运算
--      null能用在number/date/varchar2类型运算

--30、查询佣金为非null的员工
select * from emp where comm is not null;

--31、查询无佣金且工资大于1500的员工
select * from emp where (comm is null) and (sal>1500);

--32、查询工资是1500或3000或5000的员工
select *  from emp where sal in(1500,3000,5000);

--33、查询值为是"MANAGER"或职位不是"ANALYST"的员工(方式一:使用!=或<>)
select * from emp
where (job='MANAGER') or (job<>'ANALYST');

--34、查询值为是"MANAGER"或职位不是"ANALYST"的员工(方式二:使用not)
select * from emp
where (job='MANAGER') or (not(job='ANALYST'));
--order by语句:
--35、查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序
select empno,ename,sal,sal*12
from emp
order by sal asc;

--36、查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序
select empno,ename,sal,sal*12
from emp
order by sal desc; 

--37、查询员工信息,按入职日期降序排序,使用列名
select empno,ename,sal,sal*12,hiredate 
from emp 
order by hiredate desc;

--order by后面可以跟列名、别名、表达式、列号(从1开始,在select子句中的列号)
列名:
select empno,ename,sal,sal*12,hiredate 
from emp 
order by hiredate desc;

--别名: 
select empno,ename,sal,sal*12 "年薪",hiredate 
from emp 
order by "年薪" desc;

--表达式:
select empno,ename,sal,sal*12 "年薪",hiredate 
from emp 
order by sal*12 desc;

--列号,从1开始:
select empno,ename,sal,sal*12 "年薪",hiredate 
from emp 
order by 4 desc;

--38、查询员工信息,按佣金升序或降序排列(null值看成最大值)
select * from emp order by comm desc;

--39、查询员工信息,对有佣金的员工,按佣金降序排列,当order by 和 where 同时出现时,order by 在最后
select * 
from emp
where comm is not null 
order by comm desc;

--40、查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列
select * 
from emp
order by sal desc,hiredate desc;

select * 
from emp
order by sal desc,hiredate asc;

--注意:只有当sal相同的情况下,hiredate排序才有作用

--41、查询20号部门,且工资大于1500,按入职时间降序排列
select * 
from emp
where (deptno=20) and (sal>1500)
order by hiredate desc;

发布了39 篇原创文章 · 获赞 157 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_34417749/article/details/79265804
今日推荐