Ordering By Multiple Columns in an SQL Query

July 26th, 2012 - Posted by Steve Marks to MySQL, Web Development.

I remember when I started coding and writing SQL, one of the very first things I had to look up was how to order an SQL resultset by multiple columns. Let me dive right in and provide some examples…

Ordering by a single column

SELECT `col1` FROM `tablename` ORDER BY `col1`

Ordering by two columns

SELECT `col1` FROM `tablename` ORDER BY `col1`, `col2`

Ordering by two columns in different directions

SELECT `col1` FROM `tablename` ORDER BY `col1` DESC, `col2`
This entry was posted on Thursday, July 26th, 2012 at 6:28 pm by +Steve Marks and is filed under MySQL, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first