SQL ORDER BY Statement

The SQL ORDER BY statement is used to sort the results of an SQL query. The ORDER BY keyword is used to call the column(s) to be sorted. We can sort the results in Ascending Order by default or in Descending Order.

SQL ORDER BY statement

SELECT column_name(s)

FROM table_name

ORDER BY column_name(s)

SQL ORDER BY statement in ascending order

SELECT column_name(s)FROM table_name

ORDER BY column_name(s) ASC

SQL ORDER BY statement in descending order

SELECT column_name(s)

FROM table_name

ORDER BY column_name(s) DESC


Database Table “Members”

Id FirstName LastName Email City Phone
1 Gilbert Duncan [email protected] Newark 973-600-3300
2 Elvis Daniels [email protected] Piscataway 862-600-3050
3 Michael Green [email protected] Detroit 732-500-5050
4 Kelvin Graham [email protected] Detroit 732-555-5050

SQL ORDER BY Example

SELECT *

FROM Members

ORDER BY FirstName

RESULT of SQL ORDER BY statement above will be

Id FirstName LastName Email City Phone
2 Elvis Daniels [email protected] Piscataway 862-600-3050
1 Gilbert Duncan [email protected] Newark 973-600-3300
4 Kelvin Graham [email protected] Detroit 732-555-5050
3 Michael Green [email protected] Detroit 732-500-5050

From the result above we can see the result is in ascending order which is by default. Now let us take a look at returning result in descending order.

SELECT *

FROM Members

ORDER BY FirstName DESC

RESULT of SQL ORDER BY statement above will be

Id FirstName LastName Email City Phone
3 Michael Green [email protected] Detroit 732-500-5050
4 Kelvin Graham [email protected] Detroit 732-555-5050
1 Gilbert Duncan [email protected] Newark 973-600-3300
2 Elvis Daniels [email protected] Piscataway 862-600-3050

From the result above we can see the result is in descending order.