--1.简单查询
--1.语法
select [distinct]*|列名称[别名],列名称[别名]... from 表名 [别名]
--执行顺序:
1.from子句,然后select子句;
distinct 如果查询的是多个列,只有在多个列都完全重复的时候才可以消除重复;
--1.查询emp中的所有数据
select * from emp;
--2.查询emp表中的雇员的编号,姓名,职位,工资;
select e.empno,e.ename,e.job,e.sal from emp e;
--3.消除名字相同的数据
select distinct(ename) from emp;
select distinct from emp;--查询所以列都不相同的数据
select distinct ename from emp;--查询名字不同的员工的名字
select distinct(ename),sal from emp;---查询不同名字的员工的姓名和工资;
select e.empno,e.ename from emp e;
--4查询每个雇员的编号、姓名、基本年薪(13薪,每年领5个月的高温补贴1000,每个月领500交通补贴)
select e.empno,e.ename,sal*13+1000+500*12 sal from emp e;
--5.常量
1.字符串 用","声明
2.数字 直接编写
3.日期 使用日期格式
--5.1直接查询常量
select '雇员',empno,ename from emp;
--5.2"||" 连接
select '姓名 : '||ename ||',工资:' ||sal say from emp;select * from emp;
--6.四则运算
---查询员工的姓名,年薪(15薪+每个月500交通补贴+每个月300的饭补)
select e.ename,sal*12+500*12+300*12 sal
from emp e;
--限定查询
select [distinct]*|列名称[别名],列名称[别名]... from 表名 [别名] [where] 条件
1.确定数据来源 from
2.确定满足数据的数据行 where
3.控制要显示的数据列 select
限定查询的符合
1.关系运算符:>,<,<=,>=,<>(!=);
2.逻辑运算符:AND,OR,NOT
3.范围运算符:BETWEEN...AND,IN,NOT IN;
4.空判断:IS NULL,IS NOT NULL
5.模糊查询:LIKE
"="可以在数字上使用,也可以在字符串中使用
--1.查询工资高于1500的员工信息
select * from emp c where c.sal>1500
--2. 查询工资高于1500低于3000的员工信息
select * from emp c where c.sal>1500 and c.sal<3000
--3.查询工作高于1500的员工的信息
select * from emp c where not c.sal<1500
--4.范围查询(数字,日期)
between... and...
1.包含最大值和最小值
--5.查询所以在1981年雇佣的员工
select * from emp e where e.hiredate between '01-1月1981' and '31-12月1981'
--空判断:is null ;is not null
select * from emp e where comm is not null;---查询由佣金的员工信息
select * from emp e where comm is null;--查询没有佣金的员工的信息
NOT IN和NULL的问题
1.在使用NOT IN 进行范围判断的时候,如果范围里面包含了NULL,不会有任何的结果返回;
--6.模糊查询:LIKE
'-':匹配任意的一个字符;
'%':匹配任意的零位、一位或多位字符;
like 可以应用在各种类型上;
like 如果不设置查询关键字,那么表示查询所以
select * from emp WHERE ename like '%%';
select * from emp c where c.ename like '%wang%';
--7.order by 分组查询
select [distinct]*|列名称[别名],列名称[别名]... from 表名 [别名] [where] 条件 order by 字段[ASC(默认)/DESC,字段[ASC/DESC],....]
1.确定数据来源 from
2.确定满足数据的数据行 where
3.控制要显示的数据列 select
4.针对查询结果排序 可以使用Select语句中的别名;order by
---查询雇员的年薪按年薪做升序
select ename,job,sal*12 sal from emp order by sal;
--连接字符
select '姓名:'||ename||'职位:'||job say from emp;