SQL SELECT order by

The basic syntax of the SELECT statement with the ORDER BY clause is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY column_name [ASC | DESC];

Let’s break down the syntax:

Here’s an example that demonstrates the usage of the SELECT statement with the ORDER BY clause:

SELECT name, age, salary
FROM employees
ORDER BY age DESC;

In the above example, we select the columns ‘name’, ‘age’, and ‘salary’ from the ‘employees’ table and sort the data based on the ‘age’ column in descending order.

The ORDER BY clause can also use multiple columns for sorting. For instance:

SELECT name, age, salary
FROM employees
ORDER BY age DESC, salary ASC;

In this example, the data is first sorted by the ‘age’ column in descending order, and then by the ‘salary’ column in ascending order.

In conclusion, the SQL SELECT statement with the ORDER BY clause is a powerful tool for retrieving data from a table while specifying the desired sorting order. It is useful when you need to present data in a particular sequence based on one or more columns.

#SQL #OrderBy