How to use SQL ORDER BY DESC, ASC Case Statement Multiple Column
Summary: In this article, you will learn what is SQL ORDER BY and how to use the ORDER BY clause to sort the result and set a query by one or more columns.
The SQL ORDER BY Clause is used to set the result-set a query by one or more columns. The ORDER BY SQL keyword sorts the records by default in ascending order. therefore, to sort the records in descending order, then you can use the DESC keyword.
ORDER BY keywords specifies that a SQL Server SELECT statement that returns the result in set data or records no particular order
Therefore, if you want to specify the order, then you can use the ORDER BY clause, allows sorting by one or more rows or columns which returned the records in descending and ascending order.
As a Result
In SQL Server, when you use the SELECT statement to query data from the table, the order of rows or columns in the result set is not guaranteed. Therefore, It means that SQL Server can return a result set with an unspecified order of rows or columns.
The only most preferred way for you to guarantee that the rows or columns in the result set are sorted is to use the SQL ORDER BY Keyword. There are the following illustrates the SQL ORDER BY clause syntax:
General syntax
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;
Explanation:
column_name | expression: AS you see in general Syntax, first, you will be specifying a column name or an expression on which to sort the result set of the query. furthermore, If you will specify multiple columns, then the result set is sorted by the first column and then that sorted result set is sorted by the second column, and so on.
The columns that appear in the SQL ORDER BY The keyword must correspond to either column in the select list or to columns defined in the table specified in the FROM clause.
ASC | DESC: The Second, You will be using the ASC or DESC to specify whether the values in the specified column should be sorted in descending or ascending order. The DESC sorts the result set from the highest value to the lowest one while The ASC sorts the result from the lowest value to the highest value. If you don’t explicitly specify DESC or ASC, The by default SQL Server uses ASC sort order. furthermore, also, SQL Server treats NULL as the lowest value.
Therefore, when you processing the SELECT statement that has an SQL ORDER BY keyword, the ORDER BY the keyword is the very last clause to be processed.
Demo Table: Employees
CREATE TABLE Employees (
EmployeeID INT,
EmployeeName VARCHAR(70),
FatherName VARCHAR(70),
City VARCHAR(70),
Country VARCHAR(70),
PostalCode INT,
Salary INT
)
| EmployeeID | EmployeeName | FatherName | City | Country | PostalCode | Salary |
|---|---|---|---|---|---|---|
| 101 | Leo Jason | Thomas sawyer | Hong Kong | Hong Kong | 237645 | 350000 |
| 102 | Nathan Miles | Asher Kayden | Bangkok | Thailand | 763423 | 450000 |
| 103 | Ryan parker | Caleb Everett | Kuala Lumpur | Malaysia | 130978 | 550000 |
| 104 | Andrew Bryson | Theodore Axel | New York City | USA | 116534 | 450000 |
| 105 | Christopher Leonardo | Joshua Jameson | Shenzhen | China | 121345 | 760000 |
| 106 | Jaxon Jose | Lincoln Jace | Paris | France | 987688 | 678000 |
| 107 | Anthony Xavier | Mateo Ezekiel | Antalya | Turkey | 10342 | 532000 |
| 108 | Gabriel Roman | Julian Evan | Istanbul | Turkey | 881122 | 883400 |
| 109 | Grayson Jordan | Dylan Santiago | Dubai | UAE | 800907 | 9567404 |
| 110 | Jack Adam | Luke Austin | Rome | Italy | 876590 | 7689490 |
| 111 | John Greyson | Owen Dominic | Guangzhou | China | 793456 | 778690 |
| 112 | Sebastian Colton | Jackson Elias | Mecca | Saudi Arabia | 698711 | 888534 |
| 113 | Henry Easton | Daniel Jeremiah | Brussels | Belgium | 239067 | 7785690 |
| 114 | Michael Nolan | Jacob Jonathan | Prague | Czech Republic | 654567 | 765900 |
| 115 | Ethan Adrian | Alexander Landon | Amsterdam | Netherlands | 112389 | 9123670 |
| 116 | Logan Aaron | Mason Ezra | Ho Chi Minh City | Vietnam | 390875 | 5567800 |
| 117 | Elijah Connor | Lucas Eli | Orlando | USA | 412345 | 6753490 |
| 118 | Benjamin Hunter | Oliver Christian | Buenos Aires | Argentina | 321245 | 7893451 |
| 119 | William Josiah | James Hudson | Mexico City | Mexico | 213908 | 8099900 |
| 120 | Noah Charles | Liam Isaiah | Johannesburg | South Africa | 264578 | 4569956 |
Example: SQL ORDER BY
The following SQL statement selects all Employee from the “Employees” table, and sorted by the “City” column:
SELECT * FROM Employees ORDER BY City;

Example: SQL ORDER BY DESC
There are the following SQL statement selects all Employee from the “Employees” table, sorted DESCENDING by the “City” column:
SELECT * FROM Employees ORDER BY City DESC;

SQL ORDER BY ASC
There are the following SQL statement selects all Employee from the “Employees” table, sorted Ascending by the “EmployeeName” column:
SELECT * FROM Employees ORDER BY EmployeeName ASC;

ASC (Ascending) is the default value for the SQL ORDER BY clause. therefore, So, if you don’t specify anything after column name in the ORDER BY clause, the output will be sorted in ascending order by default.
Example: ORDER BY Several Columns
There are the following SQL statement selects all Employee from the “Employees” table, therefore, these are sorted by the “EmployeeName” and “City” column. hence this means that it orders by City, but if some rows have the same city, it orders them by EmployeeName:
SELECT * FROM Employees ORDER BY City, EmployeeName;

Another Example: ORDER BY Several Columns
There are the following SQL statement selects all Employee from the “Employees” table, sorted ascending by the “City” and descending by the “EmployeeName” column:
SELECT * FROM Employees ORDER BY City ASC, EmployeeName DESC;

Example: Sort a result set by an expression
Therefore, The LEN() SQL function returns the number of characters of a string. furthermore, there is the following statement uses the LEN() function in the SQL ORDER BY keyword to retrieve an Employee list sorted by the length of the EmployeeName.
SELECT
EmployeeName,
FatherName
FROM
Employees
ORDER BY
LEN(EmployeeName) DESC;

Example: Sort by ordinal positions of columns
The SQL Server allows you to sort the result set based on the ordinal positions of columns that appear in the select list.
There is the following statement sorts the Employee by EmployeeName and FatherName. But instead of specifying the column names explicitly, furthermore, it will use the ordinal positions of the columns:
SELECT
EmployeeName,
FatherName
FROM
Employees
ORDER BY
1,
2;

In this above example, 1 (First) means the EmployeeName column and 2 (Second) means the FatherName column.
Therefore, Using the ordinal positions of columns in the SQL ORDER BY clause, which is considered a bad programming practice or exercise for a couple of reasons.
First, the columns in a table don’t have ordinal positions and need to be referenced by the name.
Second, when you modify the select list, you may forget to make the corresponding changes in the SQL ORDER BY clause.
Example
SELECT CompanyName, ContactName, City, Country FROM Supplier ORDER BY CompanyName
| Supplier |
|---|
| CompanyName |
| ContactName |
| City |
| Country |
Demo table
| Id | CompanyName | ContactName | City | Country |
|---|---|---|---|---|
| 121 | Amdocs Pvt. Ltd | Cheryl Saylor | Delhi | India |
| 131 | Ascon Group | Dolly Ben | New Delhi | India |
| 101 | Lalit Pvt.Ltd | Nitin Shukla | Lucknow | India |
| 231 | Singsymsis Pvt.Ltd | Aditya Shukla | Kanpur | India |
Question 2: SQL Case statement For Order By clause with Desc/Asc sort
SELECT
*
FROM
TableName
WHERE
ORDER BY
CASE @OrderByColumn WHEN 1 THEN Forename END DESC, Date,
CASE @OrderByColumn WHEN 2 THEN Surname END ASC
Resource
Conclusion
In this article, you have learned how to use the SQL ORDER BY clause to sort a result set by columns in descending or ascending order and with the different types of ORDER BY Clause example. I hope you will enjoy it!