MySQL ORDER BY Keyword

The ORDER BY keyword in MySQL is used to sort the result set of a SELECT statement in ascending or descending order based on one or more columns.

The syntax of the ORDER BY clause is as follows:

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

Here, column1, column2, etc. are the columns to sort by, and ASC or DESC is used to specify the sorting order. ASC (ascending) is the default sorting order, so it can be omitted.

For example, let’s say we have a table named employees with columns id, name, age, and salary. To sort the table by the age column in ascending order, we can use the following SQL query:

SELECT * FROM employees ORDER BY age;

To sort the table by the salary column in descending order, we can use the following SQL query:

SELECT * FROM employees ORDER BY salary DESC;

To sort the table by multiple columns, we can specify them in the ORDER BY clause separated by commas. For example, to sort the table by the age column in ascending order and the salary column in descending order, we can use the following SQL query:

SELECT * FROM employees ORDER BY age, salary DESC;

This will sort the result set by the age column in ascending order and then by the salary column in descending order.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial