Sql Order By

How to make results more readable by using Sql Order By.

It sorts results by any column in the select list. It can be ascending or descending order.

The syntax:

SELECT  column_list
FROM table_name
ORDER BY column_name [ASC | DESC]

By default the records are sorted in ascending order so we can omit the ASC keyword.

Example: Let's sort the Contact table by the Person's Surname in ascending order.

CPIdSurnameFirstnameAddressSuburbStatePostcode
1WhiteAlan210 George StSydneyNSW2000
2Johnson Benjamin 11 Cooper St Surry Hill NSW 2010
3Brown Peter 72 Haymarket St Haymarket NSW 2000
4Anderson Thomas 33 Collins St Melbourne VIC 3000
5Gomdas Tony 78 Marine Pde Coolangatta QLD 4225
6GreenTony 123 Second AveLiverpoolNULL2170
7GutmanPhilip27 Swanson StMelbourneVIC3000

SQL statement:

SELECT  Surname,Firstname,Address
FROM Contacts
ORDER BY Surname

The result:

SurnameFirstnameAddress
Anderson Thomas 33 Collins St
Brown Peter 72 Haymarket St
Gomdas Tony 78 Marine Pde
GreenTony 123 Second Ave
GutmanPhilip27 Swanson St
Johnson Benjamin 11 Cooper St
WhiteAlan210 George St

Tips:You can also do nested sorts, i.e. by adding one more column to the ORDER BY clause. In nested sorts, results ordered by first column, second column and so on. As many as you like, though some systems require that each sort element appears in the select list.

Sorting Null Values
Depending on the system, it will be either greater than or less than all non Null values.

Quick link. From here, you can learn the following:

Sql Select
The core of Sql, selecting data from table.

Sql Where
Conditionally selecting rows from table.

Sql Distinct
Eliminating duplicate values in the output.

Sql Count
Counting number of rows.

Sql Sum
Getting the sum of a field

Sql Max
This function will return the highest value.

Sql Min
This function will return the lowest value.

Sql Avg
This function will return the average value.

Sql And Or
Connecting two or more conditions in the Where or Having clause.

Sql Group By
Grouping rows into sets.

Sql Having
To filter the result of aggregation

Sql Like
Pattern searching for conditions in Where or Having clause

Sql Between
Added to conditions to show results between two values.

Perseverance: Cliffhanger
Buy at AllPosters.com
What the mind can conceive and believe, it can achieve.

Positive Resources

All of us have at least two or three things that excite us. We could talk about these subjects for hours. Good news!. Did you know that Site Built It! can turn this type of deep interest and enthusiasm into a revenue-generating business?

Review some of the Site Build It! case studies and you'll absolutely discover something that ignites more positive ideas into your mind.

SBI! Video Tour


Invest the most
important 30 minutes
you will spend online.

Click, sit back, and
understand why SBI!
makes you succeed.