Wednesday, January 30, 2008

SQL Basics - Sorting Your Results

To sort your results returned from a SELECT command,we use the ORDER BY clause.

SYNTAX

SELECT * FROM table_name [WHERE condition]

ORDER BY column-name(s)


EXAMPLES

SELECT * FROM TeethEyes

ORDER BY child_age

order by 1


You can also sort text in alphabetical order.

SELECT * FROM TeethEyes

ORDER BY child_Eyes


order by 2

How about if you want to sort in a descending order, such as the older child first. You use the DESC parameter.


SELECT * FROM TeethEyes

ORDER BY child_age DESC

order by 3


One last thing. You can sort by multiple columns, and have it sort first by one and then the other, for example:

SELECT * FROM TeethEyes WHERE child_teeth>3

ORDER BY child_eyes, child_age DESC

order by 4

First it sorted by the eye color in ascending order, then it sort by age in descending order.

We'll be in touch.

No comments: