SQL排序(升序,降序)

排序(升序,降序)

select 	语句
from		表名
order by	

//order by是最后执行的

1.按照工资升序,找出员工名和薪资?



	select 
		ename,sal
	from
		emp
	order by
		sal;					//排序默认的是升序
	+--------+---------+
	| ename  | sal     |
	+--------+---------+
	| SMITH  |  800.00 |
	| JAMES  |  950.00 |
	| ADAMS  | 1100.00 |
	| WARD   | 1250.00 |
	| MARTIN | 1250.00 |
	| MILLER | 1300.00 |
	| TURNER | 1500.00 |
	| ALLEN  | 1600.00 |
	| CLARK  | 2450.00 |
	| BLAKE  | 2850.00 |
	| JONES  | 2975.00 |
	| FORD   | 3000.00 |
	| SCOTT  | 3000.00 |
	| KING   | 5000.00 |
	+--------+---------+
	
	注意:默认是升序。怎么指定升序或者降序呢?asc表示升序,desc表示降序。
	select ename,sal from emp order by sal asc;	//升序排
	select ename,sal from emp order by sal desc; //降序排

	que:按照工资的降序排列,当工资相同的时候再按照名字的升序排列。
	select ename,sal from emp order by sal desc,ename asc;	多个字段排序时,先满足靠前的,在满	足靠前的顺序前提下,按照后续的排序

	当order by 后面跟的时数字,是按照查询的字段的列数
	select ename,sal from emp order by 1;     //就是排ename sal 中的第一个,

找出工作岗位是SALESMAN的员工,并且要求按照薪资的降序排列

select 
	ename,job,sal
from 
	emp
where
	job = 'SALESMAN'
order by
	sal desc;
	+--------+----------+---------+
	| ename  | job      | sal     |
	+--------+----------+---------+
	| ALLEN  | SALESMAN | 1600.00 |
	| TURNER | SALESMAN | 1500.00 |
	| WARD   | SALESMAN | 1250.00 |
	| MARTIN | SALESMAN | 1250.00 |
	+--------+----------+---------+
		


	
	
	

你可能感兴趣的:(数据库)