SQL ORDER BY clause
SQL Tutorial » SQL ORDER BY clause
How does ORDER BY work in SQL ?
ORDER BY clause is used to sort the result in ascending or descending order as you need for your web page.
By default ORDER BY sorts the data in ascending order. To sort the records in descending order, use the DESC keyword.
Syntax:
FROM table_name
ORDER BY column_1, column_2, ... ASC|DESC;
Let's look at the "Users" table: We will have five columns as: "id", "first_name", "last_name", "age", "email":
ID | First Name | Last Name | Age | |
---|---|---|---|---|
1 | Clark | Kent | 24 | clarkkent@mail.com |
2 | Pire | Joveau | 31 | pierej@mail.com |
3 | Nicky | Anderson | 19 | nickya@mail.com |
4 | Pepe | Martínez | 38 | martinezpeper@mail.com |
5 | Peter | Parker | 21 | peterparker@mail.com |
SQL ORDER BY Example
The following SQL statement selects all users from the "Users" table, sorted by the "Age" column - Example:
ORDER BY age;
Output displayed:
ID | First Name | Last Name | Age | |
---|---|---|---|---|
3 | Nicky | Anderson | 19 | nickya@mail.com |
5 | Peter | Parker | 21 | peterparker@mail.com |
1 | Clark | Kent | 24 | clarkkent@mail.com |
2 | Pire | Joveau | 31 | pierej@mail.com |
4 | Pepe | Martínez | 38 | martinezpeper@mail.com |
Related subjects:
SQL add columns to a table
SQL in operator
SQL where clause
Conect to MySQLi
PHP MySQLi create table
Tags: sql order by: date, multiple columns, of operations, two 2 columns, by group by, by then by, alphabetically, by 1 How does ORDER BY work in SQL?, What is ORDER BY clause of SQL statement?, What is ORDER BY 2 desc in SQL?, Where do I put DESC in SQL?
SQL ORDER BY Several Columns Example
The next SQL statement selects all users from the "Users" table, sorted by the "last_name" and "age" column. This means that it orders by last_name, but if some rows have the same age, it orders them by last_name:
ORDER BY last_name, age;
SQL ORDER BY Several Columns The following SQL statement selects all Users from the "Users" table, sorted ascending by the "last_name" and descending by the "age" column:
ORDER BY last_name ASC, age DESC;
SQL ORDER BY Several Columns Example
The next SQL statement selects all users from the "Users" table, sorted by the "last_name" and "age" column. This means that it orders by last_name, but if some rows have the same age, it orders them by last_name:
ORDER BY last_name, age;
SQL ORDER BY Several Columns The following SQL statement selects all Users from the "Users" table, sorted ascending by the "last_name" and descending by the "age" column:
ORDER BY last_name ASC, age DESC;
sql order by: date, multiple columns, of operations, two 2 columns, by group by, by then by, alphabetically, by 1 How does ORDER BY work in SQL?, What is ORDER BY clause of SQL statement?, What is ORDER BY 2 desc in SQL?, Where do I put DESC in SQL?
SQL ORDER BY clause - sql tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.