ORDER BY clause
Use the ORDER BY
clause to sort the result set of a query based on one or more columns in ascending or descending order. It is commonly used to organize data in a specific sequence for better analysis and presentation.
Here is the basic syntax of the ORDER BY
clause:
Examples
Let’s assume we have a table named “employees” with columns “employee_id”, “employee_name”, and “salary”.
Code of creating the table and inserting data
To retrieve the list of employees sorted by their salaries in descending order, the SQL query would be:
In this example, the result set displayed the employees’ details sorted by their salaries in descending order, showing the highest-paid employees first. The ORDER BY
clause helps in arranging data in a structured and meaningful way for easier interpretation and decision-making.
Was this page helpful?