SQL ORDER BY

Learn how to use SQL ORDER BY clause to sort data in ascending and descending order. Find out how to order the filtered result using ORDER BY clause.

The WHERE clause also comes before the ORDER BY clause to apply the filter and sort the filtered result. However, if you forget to give attributes ASC|DSC after the ORDER BY clause, the SQL takes ASC as the default attribute.

The WHERE clause and the ORDER BY clause are optional to use in the SQL query. You can order one column or you can order more than one column using the syntax given below.

How to Use SQL ORDER BY Clause and Sort the Filtered Result

The syntax to use and order the result is given below. You have to use the ORDER BY clause after the WHERE clause. In addition to this, you have to use either ASC or DSC attribute to sort the data of the filtered result.

Here, one thing you should notice that all the data gets filtered first then after it will use the ORDER BY clause to sort the data at the end. That way, we have to use the ORDER BY clause at the end of the query statement.

Syntax1: ORDER BY Clause to Sort Single Column

The above syntax contains the SELECT statement with the name of the column and the table. You can use the WHERE clause to filter the result. In addition to this, you have to use the ORDER BY clause to sort the filtered result for one column using the above syntax.

You can order the result in ascending or descending using the attribute ASC|DESC.

Syntax2: ORDER BY Clause to Sort Multiple Column

If you want to order multiple columns, you have to use the above syntax. The above syntax contains more than one column in ORDER BY clause with attribute option ASC or DESC for all the specified columns.

Parameter Descriptions

Sr.No Parameter Name Description
1 column_name Enter the column name you want to select for in the SELECT statement and another column in the ORDER BY clause to sort the data in order with this column.
2 tablename Specify the table name from which you want to filter the result and sort order the data.
3 condition Specify the condition in the WHERE clause with the operator, columns, and values to filter the result and order using the ORDER BY clause.

You must also learn.

Reference