Logo

K-Learn

SQL Order By Clause

< Previous Next >

The ORDER BY clause in SQL is used to sort the result set of a query in ascending or descending order based on one or more columns. This clause is used in conjunction with the SELECT statement.

Order By Syntax

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

SQL Asc Keyword

For example, let's say we have a table named "student" with columns "id", "name", "email", "phone", and "address". If we want to retrieve a list of students ordered by their name in ascending order,

We can use the following SQL statement:

SELECT * FROM student ORDER BY name ASC;

OUTPUT

In this example,

This statement retrieves all columns from the "student" table and sorts the result set in ascending order based on the "name" column.

SQL Desc Keyword

We can also sort the result set in descending order by using the DESC keyword. For example, to retrieve a list of students ordered by their name in descending order,

We can use the following SQL statement:

SELECT * FROM student ORDER BY name DESC;

OUTPUT

In this above example,

This statement retrieves all columns from the "student" table and sorts the result set in ascending order based on the "name" column and in descending order based on the "phone" column.


< Previous Next >