select empno, ename, sal*12 from emp;
select empno as id, ename name, sal*12 as totalSal from emp;
select empno as "id", ename "name", sal*12 as "年薪" from emp;
select * from...
select id, name,age as 别名 from ....
(4)where子句 where子句用于在查询中做条件限制的 (where子句可用用在select、update、delete) 强调: ① 在where子句中,如果条件是和数字比较,可以使用单 引号,也可以不用; ② 在where子句中,如果条件是和字符串以及日期类型的数 据进行比较,则必须用单引号引起来。
查询在部门10下面的所有员工信息
select * from emp where deptno=10;
select * from emp where deptno='10';
查询员工表中职位是SALESMAN的职员
select * from emp where job='SALESMAN';
select * from emp where job=SALESMAN; ORA-00904: "SALESMAN": 标识符无效
2)Oracle中的查询条件 ---- where子句中的
(1)使用 > < >= <= = !=等价于<>
查询员工表薪水低于2000的员工信息
select * from emp where sal<2000;
select * from emp where sal+nvl(comm,0)<2000;
查询员工表中不属于部门10的员工信息
select * from emp where deptno!=10;
select * from emp where deptno<>10;
查询员工表中在2002年1月1日以后入职的员工信息,比较日期类型数据
SELECT ENAME,SAL,HIREDATE FROM emp
WHERE HIREDATE > TO_DATE('2002-1-1','YYYY-MM-DD');
查询底薪大于1000并且职位是'CLERK'的员工信息
select * from emp where sal>1000 and job='CLERK'; 正确
SELECT * FROM EMP WHERE SAL>1000 AND JOB='CLERK'; 正确
select * from emp where sal>1000 and job='clerk'; 错误
select * from emp where sal>1000 and lower(job)='clerk'; 正确
select * from emp where sal>1000 and initcap(job)='Clerk'; 正确
select job, upper(job), lower(job), initcap(job) from emp;
查询底薪大于1000或者职位是'CLERK'的员工信息
select * from emp where sal>1000 or lower(job)='clerk';
select * from emp where sal>1000 or job='CLERK';
select * from emp where ename='SMITH'; 精确值查询
select * from emp where ename like '_A%'; 员工姓名中第二个字母是A的 模糊匹配查询
模糊匹配查询 查询员工姓名中包含A的所有信息
select * from emp where ename like '%A%';
模糊匹配查询 查询员工姓名中是S结尾的员工信息
select * from emp where ename like '%S';
(4)使用in和not in 语法:in(list1, list2,list3,…)取出等于列表中的值 not in(list1,list2,list3…)取出不等于此列表中的数据 参数list表示列表值,每一个列表值用逗号分隔开
查询职位是MANAGER或者CLERK的员工信息
select * from emp where job='MANAGER' or job='CLERK';
select * from emp where job in('MANAGER', 'CLERK');
查询出不是部门10和不是部门20的员工信息
select * from emp where deptno !=10 and deptno !=20; 正确
SELECT * FROM emp WHERE DEPTNO NOT IN (10,20); 正确
select * from emp where deptno !=10 or deptno !=20; 错误
(5)使用between a1 and a2 用于查询符合某个值范围条件的数据(a1到a2之间的范围,包含a1和a2) 使用在数字类型、字符串类型和日期类型的数据范围上。
查询底薪在1500~3000之间的员工信息
select * from emp where sal between 1500 and 3000;
select * from emp where sal>=1500 and sal<=3000;
(6)使用is null和is not null 空值null是一个特殊值,比较的时候不能使用=。 是否等于空 is null 不等于空 is not null
查询提成为空的员工信息
select * from emp where comm is null;
查询提成不为空的员工信息
select * from emp where comm is not null;
select * from emp where sal>any(3500,4000,4500); 工资大于3500
select * from emp where sal<any(3500,4000,4500); 工资小于4500
select * from emp where sal>all(3500,4000,4500); 工资大于4500
select * from emp where sal<all(3500,4000,4500); 工资小于3500