查询1

查询里面最关键的是顺序()

1、计算列

select * from emp;
-- * 表示所有
-- from emp 表示从emp表查询
select empno, ename from emp;
select ename, sal from emp;
select ename, sal*12 from emp;
select ename, sal*12 as "年薪" from emp;
-- as可以省略年薪必须用双引号
select ename, sal*12 "年薪" from emp;
select ename, sal*12 "年薪" ,sal "月薪" from emp;
注意:
orale中的字段别名不允许用单引号括起来,但sql允许
为了兼容性别名统一用双引号括起。
2、distinct(不允许重复的)

select deptno from emp;--14条记录不是三条
		select distinct deptno from emp; --三条记录distinct deptno会过滤到重复的deptno
		select distinct comm from emp;--distinct也会过滤掉重复的null
		select distinct comm, deptno from emp;--把comm和deptno的组合进行过滤
		select comm, distinct deptno from emp;--逻辑上有冲突

3、between

select *from emp
	where sal =5000;
select *from emp
	where sal >=1500 and sal <= 3000;
--等价于
select *from emp
select *from emp
	where sal in (1500,3000);
--等价于
select *from emp
	where sal =1500 or sal = 3000;
select *from emp
	where sal not in (1500,3000);
--等价于
select *from emp
	where sal !=1500 and sal != 3000;
-- 数据库中不等号用    !=  或 <>  推荐用<> 

where sal between 1500 and 3000;--查找工资小于或大于的员工的所有信息select *from empwhere sal <1500 or sal > 3000;--等价于select *from empwhere sal not between 1500 and 3000;

4、in(属于若干个孤立的值)

select *from emp
	where sal in (1500,3000);
--等价于
select *from emp
	where sal =1500 or sal = 3000;
select *from emp
	where sal not in (1500,3000);
--等价于
select *from emp
	where sal !=1500 and sal != 3000;
-- 数据库中不等号用    !=  或 <>  推荐用<> 

5、top(最前面的若干个记录)

select top 5 * from emp;
		select top 15 percent * from emp; -- 输出的是三个不是两个
		--把工资在到(包括、)之间的员工中工资最高的前三个员工所有信息输出
		select top 3 * from emp
			where sal between 1500 and 3000
			order by sal desc; --desc降序不写默认升序


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值