SQL 基础操作

select *from emp;
select ename from emp;
select ename,*from emp;
select distinct  sal from emp;
select ename as "姓名",sal from emp;
select ename,15 from emp;
select distinct deptno,sal from emp; --distinct 去除重复的元素,<>表示不等于

select * from emp
 where sal>=1500and sal<=3000;

 select * from emp
 where sal between 1500and 3000;

 --in 孤立的值
 select *from emp where sal in(1500,3000);

 select top 15 percent * from emp; --top 用来分页 不能省
 
 select top 6 *
 from emp
 where sal between 1500 and 3000
 order by sal desc -- order by 排序通过sal方式为降序desc方式,asc 升序
 
 select * from emp
 where comm is null; --is null,is not null;null不参与=,!=,> <等运算,null 与任意值进行这些运算都为null;

 select ename as "姓名",sal*12+12*comm as "年薪" from emp

 select ename,sal * 12+isnull(comm,0) as "年薪" from emp; --isnull如果comm是null 就返回0,否则返回comm;

 select *from emp order by deptno,sal;--先按deptno 排序,如果deptno相等再按工作排

 select * from emp order by deptno,sal desc;--先按deptno 升序,再按sal降序;

 --模糊查询
 select * from emp 
 where ename like '_A%';

 select * from emp 
 where ename like '_[A-F]%'; -- 把ename中第2个字符为A到F的记录输出;ps. [a,f]指a或f, [ ^a-f]指不是a到f中的任意一个;

 --聚合函数
 --avg() 平均值, count()求个数;

 select lower(ename) from emp; --lower 返回时是14行,lower()是单行函数;

 select max(sal) from emp; --max 返回时是1行,max()是多行函数;

 select count(*) from emp; --返回emp 表中记录个数;

 select   count( distinct deptno) from emp; --返回不重复的 deptno个数;

 select count(comm) from emp; --count()不统计null 个数;

 select max(sal) as "最高工资",min(sal) as "最低工资" ,count(*) as"员工人数" from emp;
 --where deptno=20;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值