Order By Clause

Ø  The order of rows returned in the result of a query undefined.
Ø  The order by clause can be used to sort the rows in the required order.
Ø  The order by clause should be the last clause in the order of all clauses in the select statement.
Ø  An expression or an alias can be specified to order by clauses for sorting.
Ø  Default ordering of data is ascending
o   Numbers          à 0 - 9
o   Dates               à Earliest – Latest
o   Strings             à A – Z
o   NULLS           à Last
Ex:

  • Select ename, job, deptno, hiredate from emp order by hiredate;
  • Select ename, job, deptno, hiredate from emp order by hiredate desc;
  • Select ename, job, sal from emp where job = ’MANAGER’ order by sal;
  • Select ename,job, sal from emp where sal >= 2500 order by job, ename desc;
  • Select empno, ename, sal, sal * 12 annsal from emp order by annsal;
  • Select empno, ename, sal from emp order by deptno, sal, hiredate;
  • Select empno, ename, sal from emp where sal >= 2000 order by hiredate, sal desc;