Previous
WHERE is the new if 
Next
LIMIT Command 
SQL(Structured Query Language) Tutorial
Sorting the Results using ORDER BY

Sorting the Results using ORDER BY

Often getting the data is not enough - you have to sort the data also. This is made easy by the ORDER BY clause. For example, this is how you find the clever people...

SELECT name,iq FROM Character WHERE iq>100 ORDER BY iq

So was your name in the list? No? Hehe. Anyway, back to the tutorial - the problem with the query is that this shows the cleverest at the bottom - because by default SQL uses ascending order. The above query is same as...

SELECT name,iq FROM Character WHERE iq>100 ORDER BY iq ASC

To invert the results, try...

SELECT name,iq FROM Character WHERE iq>100 ORDER BY iq DESC

Sorting works for strings as well.

SELECT name FROM Character ORDER BY name

You can use multiple columns to sort the data...

SELECT name,iq FROM Character ORDER BY iq DESC, name ASC
Previous
WHERE is the new if 
Next
LIMIT Command 

Comments

Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed