Ordering By Multiple Columns in an SQL Query

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`
Tags:
This entry was posted on 9 months ago at 9 months ago by Steve Marks+ and is filed under MySQL, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
C'mon. Say Something...

Fear not, we won't publish this
Comments (0)

No comments have been left yet. Be the first