How to Order By Multiple Columns in SQL
How can we order by two or more columns in MySQL, or any other SQL database framework?
We can simply list out the columns after the
ORDER BY column1, column2
This will sort everything by
column1 is the same for two rows, then it’ll sort by
Descending Order with Two Columns
We can also sort in descending order for any of the columns.
ORDER BY column1 DESC, column2
Example: Ordering by Two Columns
Suppose we have this table
Let’s execute the query below.
SELECT * FROM dogs ORDER BY val DESC, dog ASC
The resulting set will look like this.
More SQL Articles
- How to Modify Table to Use Composite Primary Key in MySQL
- How to Format a Number with Two Decimal Places in SQLite
- How to Count the Number of Rows with the Same Value
- How to Order a SQL String Column By Its Numeric Value
- How to Check if a Column is Null in MySQL
- How to Combine Columns Values Into a New Column in MySQL
- How to Drop Multiple Columns with ALTER TABLE in SQL
- What Stripe Data Do I Store in My Database?
- How To Query JSON Object with Unknown Keys in PostgreSQL
- How To Convert an Escaped JSON String in PostgreSQL to a JSON Object