Thursday, January 13, 2011

Sort the rows that are retrieved by a query

Oracle server does not store rows sequentially. It means there is no aphabetical order in a tables. In case we need to produce a report that has some order, we need to use ORDER BY clause in SELECT Statemen. The ORDER BY clause is aways last in a SELECT statement.
By default sorting when we use ORDER BY clause is the ascending sort. Have to remember that ascending order means:
  • by character data - alphabetical
  • by numbers - low to high
  • data - earliest to latest
We can sort data also by descending order. For example:

select first_name, last_name
from employees
where salary > 1000
order by last_name desc;

FIRST_NAME           LAST_NAME
-------------------- -------------------------
Eleni                Zlotkey
Matthew              Weiss
Shanta               Vollman
Clara                Vishney
Jose Manuel          Urman
Oliver               Tuvault
Peter                Tucker
Jonathon             Taylor
Patrick              Sully
Lindsey              Smith
William              Smith
FIRST_NAME           LAST_NAME
-------------------- -------------------------
Sarath               Sewall
Ismael               Sciarra....

We can sort instead of scpecifying column name, specifying the position of column as it ocures in select statement. It means that positional sortig is possible only for columns that are listet in select statement. For example:
select first_name, last_name
from employees
where salary between 5000 and 10000
and job_id like 'SA%'
order by 2;
FIRST_NAME           LAST_NAME
-------------------- -------------------------
Sundar               Ande
Amit                 Banda
Elizabeth            Bates
David                Bernstein
Harrison             Bloom
Nanette              Cambrault
Louise               Doran
Tayler               Fox
Kimberely            Grant
Danielle             Greene...

Composite sorting - when result of a query are sorted by more than one column. Each column will be listed in order by clause from left to right in order of importance. To separate the columns we are using commas. We can also mix literally and positional sorting together. For example:

select last_name, first_name, salary from employees
where salary>5000
order by last_name, 3;

LAST_NAME                 FIRST_NAME               SALARY
------------------------- -------------------- ----------
Abel                      Ellen                     11000
Ande                      Sundar                     6400
Baer                      Hermann                   10000
Banda                     Amit                       6200
Bates                     Elizabeth                  7300
Bernstein                 David                      9500
Bloom                     Harrison                  10000
Cambrault                 Nanette                    7500
Cambrault                 Gerald                    11000

Chen                      John                       8200
De Haan                   Lex                       17000
Doran                     Louise                     7500
Ernst                     Bruce                      6000
Errazuriz                 Alberto                   12000
Faviet                    Daniel                     9000
Fay                       Pat                        6000
Fox                       Tayler                     9600
Fripp                     Adam                       8200
Gietz                     William                    8300
Grant                     Kimberely                  7000...

All rows will be sort by last_name alphabetically. The rows with same last_name value will be sorted by salary in ascending order.

No comments:

Post a Comment