Order by Clause:




The rows in the result set when returned from a query are not in order. If the user wants the result in some order then he should use the order by clause.

This will instruct the server to sort the result according to some order. The order by clause is a process of sorting the result set by using a raw column data or by using the expressions that are based on that data.




The “order by clause” takes the output from the SELECT statement and then processes it. And the “order by” clause also does not group the rows together; the grouping of the rows is done by the GROUP BY statement that will be discussed later.

The order by clause should be used with the SELECT statement to arrange the resulted data. The data resulted in by the order by clause will be in ascending order. This order is by default. The result set can also be sorted in the descending order by using the keyword DESC with the order by clause. To sort the result in ascending order you can also use the ASC keyword.

SYNTAX

The ORDER BY clause can be used by simply specifying one or more than one columns along with the optional ASC or DESC keywords. If the user did not specify the keyword then the order in which the result will be sorted will assume to be in ascending order. The syntax of the ORDER BY clause is as follows:

SELECT column_name * FROM Table_name order by asc/desc;

EXAMPLE

Suppose you have a table that is named STUDENT as follows:

STUD_ID NAME DEPT MONFEE CRSNO CDTE
100 Tim Marketing 1000 SPSS 19/06/96
100 Faddy Marketing 1000 SURVEYS 10/07/96
140 Jones Accounting 1200 TAXACCT 12/08/97
110 John Info. System 1100 SPSS 14/07/96
110 James Info. System 1100 COBOL 22/07/96
190 Abraham Finance 1200 INVESTMENT 20/06/97
150 Louis Marketing 1000 SPSS 19/06/97
150 Stuart Marketing 1000 SYSANAL 20/07/97

SELECT * FROM STUDENT Order by MONFEE;

When the above statement is run it will produce the following result:

STUD_ID NAME DEPT MONFEE CRSNO CDTE
100 Tim Marketing 1000 SPSS 19/06/96
100 Faddy Marketing 1000 SURVEYS 10/07/96
150 Louis Marketing 1000 SPSS 19/06/97
150 Stuart Marketing 1000 SYSANAL 20/07/97
110 James Info. System 1100 COBOL 22/07/96
110 John Info. system 1100 SPSS 14/07/96
140 Jones Accounting 1200 TAXACCT 12/08/97
190 Abraham Finance 1200 INVESTMENT 22/07/96




EXAMPLE Order By clause by DESC:

Suppose you have a table that is named STUDENT as follows:

STUD_ID NAME DEPT MONFEE CRSNO CDTE
100 Tim Marketing 1000 SPSS 19/06/96
100 Faddy Marketing 1000 SURVEYS 10/07/96
140 Jones Accounting 1200 TAXACCT 12/08/97
110 John Info. System 1100 SPSS 14/07/96
110 James Info. System 1100 COBOL 22/07/96
190 Abraham Finance 1200 INVESTMENT 20/06/97
150 Louis Marketing 1000 SPSS 19/06/97
150 Stuart Marketing 1000 SYSANAL 20/07/97

Let suppose that you to arrange the column of MONFEE in descending order then you will use the following statement:

SELECT * FROM Student order by MONFEE DESC;

The above statement will produce the following result:

STUD_ID NAME DEPT MONFEE CRSNO CDTE
190 Abraham Finance 1200 INVESTMENT 20/06/97
140 Jones Accounting 1200 TAXACCT 12/08/97
110 John Info. System 1100 SPSS 14/07/96
110 James Info. System 1100 COBOL 22/07/96
100 Tim Marketing 1000 SPSS 19/06/96
100 Faddy Marketing 1000 SURVEYS 10/07/96
150 Louis Marketing 1000 SPSS 19/06/97
150 Stuart Marketing 1000 SYSANAL

20/07/97