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 
blog comments powered by Disqus
Subscribe to Feed