以上的表为查询所用的表
--between的用法,限制
select * from emp
where sal<1000 and sal>100;--
select *from emp
where sal between 100 and 1000;
--in的用法:IN 操作符允许我们在 WHERE 子句中规定多个值。属于若干个特定的值
select * from emp
where sal=1500 or sal=5000 or sal=3000;
select * from emp
where sal in(1500,3000,5000);
select * from emp
where sal not in(1500,3000,5000);--选择既不是1500,又不是3000也不是5000的字段
select * from emp
where sal!=1500 and sal!=3000 and sal!=5000;
select * from emp
where sal<>1500 and sal<>3000 and sal<>5000;--<>表示不等
--distinct
select * from emp
--
select ename from emp
select ename, sal+12 as "年薪" from emp;--输出以计算后的字段
select distinct ename from emp;--不允许重复
select distinct deptno from emp;--不允许重复
select deptno ,ename from emp;--列出所有的
select distinct deptno ,ename from emp;--列出以部门和名字不重复的记录
--select deptno ,distinct ename from emp;--顺序错误
select deptno ,ename from emp;
select ename from emp;
--top 的用法 tOP 子句用于规定要返回的记录的数目
select top 5 *from emp;
select top 10 percent *from emp;
select top 4 * from emp--把工资从1000到5000的人中取前四个(从大到小)
where sal between 1000 and 5000
order by sal desc;--降序 默认升序
select top 4 * from emp--把工资从1000到5000的人中取后四个(从小到大)
where sal between 1000 and 5000
order by sal ;
--nul
select * from emp where comm is null;--奖金为空的
select * from emp where comm is not null;--奖金不为空
--输出每个员工的姓名和工资(包括奖金)
select ename,sal*12+comm as "年薪" from emp;
--空不能参与任何数学运算,否则为空
select ename,sal*12+isnull(comm,0) as "年薪" from emp;
--isnull 函数
select* from emp order by sal
---按照sal升排序
select* from emp order by sal desc
---降序
select * from emp order by deptno,sal
--组合查询,先按第一个变量排序,在按第二个排序...
select * from emp order by deptno,sal desc
--组合排序,第一个升序,第二个降序
select * from emp order by deptno desc,sal desc
--desc 限制前面一个变
--like的应用
--通配符
-- % 表示0个或任意个字符
-- _ 占位符
-- [] [a-f],[a,f,g]
SELECT * from emp where ename like '%a%'
--查询名字中含有A的字符
SELECT * FROM emp where ename like'a%'
--查询首字母为a的字符
select * from emp where ename like '%d'
--查询尾字母为d的字段
select * from emp where ename like '_a%'
--查询第二个字母为a的字段
select * from emp where ename like '__a%'
--查询第三个字母为a的字段
select * from emp where ename like '%[a-f]%'
--[]表示所选字符数组,
select * from emp where ename like '[a,b,s]%'
--查询首字母为a,b,s
select * from emp where ename like '[^a,b,s]%'
--查询首字母既不是a,也不是b,