Friday, July 7, 2023

MySQL ORDER BY Keyword

    MySQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY Syntax

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

Demo Database

Below is a selection from the “Employees” table in the database:

Example

SELECT * FROM Employees
ORDER BY City;

ORDER BY DESC Example

The following SQL statement selects all employers from the “Employees” table, sorted DESCENDING by the “City” column:

Example

SELECT * FROM Employees
ORDER BY City DESC;

ORDER BY Several Columns Example

The following SQL statement selects all employers from the “Employees” table, sorted by the “City” and the “LastName” column. This means that it orders by City, but if some rows have the same City, it orders them by LastName:

Example

SELECT * FROM Employees
ORDER BY City, LastName;

ORDER BY Several Columns Example 2

The following SQL statement selects all employers from the “Employees” table, sorted ascending by the “City” and descending by the “LastName” column:

Example

SELECT * FROM Employees
ORDER BY City ASC, LastName DESC;

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...