基本查询

--查询出雇员表中所有的信息:
 select * from emp;

--查询出雇员表中的雇员姓名、月薪和部门编号的信息:
 select ename,sal,deptno from emp;  --没有使用别名
 select e.ename,e.sal,e.deptno from emp e; --给表名取别名  不能加as关键字
 select e.ename 姓名,e.sal 工资,e.deptno 部门编号 from emp e; --列名取别名没有加as
  select e.ename as 姓名,e.sal as 工资,e.deptno  as 部门编号 from emp e;   --列名取别名加as
   select e.ename as "姓名",e.sal as 工资,e.deptno  as 部门编号 from emp e;   --列名取别名加as 加引号和没加引号
--注意:给某列取别名的时候,AS关键字可加可不加,别名可以不加引号,如果要加则必须加
--双引号。但是在给表取别名的时候,AS关键字一定不能加

--查询出雇员表中所有雇员的年薪:
 
select (e.sal*12) as 年薪 from emp e;
--查询出雇员表中所有雇员的奖金加500后的结果:

select e.ename as 姓名, e.comm+500 as 奖金   from emp e;
 
--注意:在对空值列进行算数运算时,空值列的结果仍然为空

--查询出所有雇员总共来自哪几个部门:
Select distinct e.deptno from emp e;
 
--判断某个部门中是否存在相同的月薪
 select distinct sal, deptno from emp;
/*注意:distinct关键字的作用范围是后面所接的所有列,判断时以列的组合
来区分是否重复
*/
/*对查询结果进行简单描述显示:*/
--例如:雇员姓名为SMITH的月薪为2000元
select '雇员姓名为'||ename||'SMITH的月薪为'||sal from emp;
/*将所有雇员的月薪进行顺序排序:*/
 select * from emp order by sal asc;
/*
注意:如果列名后不加关键字,则默认采用顺序排序的方式*/
 
/*
将所有雇员的月薪进行倒序排序:*/
 select * from emp order by sal desc;
/*
按雇员的奖金进行倒序排序
*/
select * from emp order by comm desc;
 
/*注意:对空值进行排序操作时,空值默认为无穷大,如果都为空值,则随机排序

在雇员表中首先对雇员的所在部门进行倒序排序,然后对月薪进行顺序排序,
如果月薪相等,然后按雇佣时间从晚到早进行排序*/
 select * from emp order by  deptno desc ,sal asc , hiredate desc;
 
/*注意:后面排序的字段是在前面排序的基础再进行排序

查询出月薪大于2000的所有雇员信息:*/
 
Select * from emp where sal>2000;
 

/*查询出月薪不等于1600的雇员信息:*/
select sal from emp where sal<1600 or sal >1600;
select sal from emp where sal !=1600;

/* 等同于:  */
select sal from emp where sal<>1600;
/*查询出月薪小于2000或者大于4000的雇员信息:*/
 
select * from emp where sal<2000 or sal >4000;

/*使用between and 查询出81年被雇佣的所有雇员信息*/
 
select * from emp where hiredate between '1-1月-81' and '31-12月-81';
/*查询出奖金不为空的所有用户信息*/
select * from emp where comm is not null;
select * from emp where comm is null --判断为空

--查询出姓名为SMITH、KING、FORD的所有信息
 Select * from emp where ename in('SMITH','KING','FORD');
查询出姓名中包含字母A的所有雇员信息

select * from emp where ename like '%A%'
 
注意:模糊查询使用的关键字为like,在进行匹配的时候,要使用通配符(%或_),其中
%可以匹配任意多个字符,_下划线只能匹配任意一个字符

查询出姓名中以S结尾的所有雇员信息
 select * from emp where ename like '%S';

查询出姓名中第二个字符为A的所有雇员信息
 select * from emp where ename like '_A%';

查询出81年被雇佣的所有雇员信息
 
select * from emp where hiredate between '1-1月-81' and '31-12月-81';
查询出姓名不为SMITH和KING,并且工资大于1500的所有雇员信息
 select * from emp where ename!='SMITH'and ename!='KING' and sal>1500;
 
 select * from emp where ename not in('SMITH','KING') and  sal>1500;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值