Wednesday, June 4, 2008

Using ORDER BY in SQL to sort data

Getting data sorted in advance is a wonderful feature of SQL. This means your program might not have to define it's own specific way to sort the information in your database, which can might life a lot simpler when programming in a complex language. Let's say we have a list of people.

Name,Age,Level,Job
"John Smith","39","Level 4","Computer Scientist"
"Jack Williams","22","Level 2","Mathemetician"
"John Black","29","Level 8","Artist"

You want to order them by age? Simple enough. SELECT Name,Age,Level,Job FROM People ORDER BY Age. This will return the lowest age first, and all the data with the person, etc, so you will get Williams, then Black, then Smith.