| 0 comments ]

Ordering Information :

1. The order of rows returned in the result of a query undefined.
2. The ORDER BY clause can be used to sort the rows in the required order.
3. The ORDER BY clause should be the last clause in the order of all clauses in the SELECT statement.
4. An expression or an alias can be specified to ORDER BY clause for sorting.
5. Default ordering of data is ascending

  • Number -> 0-9
  • Dates     -> Earliest - Latest
  • Strings   ->  A-Z
  • NULLs -> Last.
Syntax :
 
SELECT [DISTINCT] [*] {Column1 [Alias],.....}
FROM Table_Name
[WHERE Condition(s)]
[ORDER BY {Column, Expr}[ASC?DESC]];

  • The default ordering upon a column is ASCENDING, to change the default ordering DESC should be used after the column name.
  • Sorting can be implemented on column Aliases, and can also be implemented upon multiple columns.
  • The controversy of sorting is broken only when there arises a conflict of consistency upon the data in a column.
Examples :

1.
SELECT Ename, Job, Deptno, Hiredate
FROM Emp
ORDER BY Hiredate;

2.
SELECT Ename, Job, Deptno, Hiredate
FROM Emp
ORDER BY Hiredate DESC;


3.
SELECT Ename, Job, Sal
FROM Emp
WHERE Job = 'MANAGER'
ORDER BY Sal;

4.
SELECT Ename , Job, Sal
FROM Emp
WHERE Sal >=2500
ORDER BY Job, Ename DESC;

5.
SELECT Empno, Ename, Sal, Sal * 12 Annsal
FROM Emp
ORDER BY Annsal;;

6.
SELECT Empno, Ename, Sal
FROM Emp
ORDER BY Deptno, Sal, Hiredate;

7.
SELECT Empno, Ename, Sal
FROM Emp
WHERE Sal >=2000
ORDER BY BY Hiredate, Sal DESC;

0 comments

AddThis

| More
Widget By Devils Workshop