Can we apply ORDER BY with multiple columns?


#1

Question

In SQL, can we apply ORDER BY with multiple columns?

Answer

Yes, following the ORDER BY, you can list more than one column for which to order the data by.

When ordering by more than one column, it will first order the data on the first column, then, keeping the previous column order, it will order on the next column, and so on.

You can also specify ascending or descending order for each listed column.

Example

/* 
This will order on the year, then order the 
names in reverse alphabetical 
order, preserving the order
of the year column.
*/

SELECT year, name
FROM movies
ORDER BY year ASC, name DESC;