The ORDER BY
keyword is used to sort the result-set by one or more specified fields (columns). The ORDER BY
keyword sorts the records in ascending order by default. If you want to sort the records in descending order, you can use the DESC
keyword.
SQL ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC | DESC
ORDER BY Example
In this example, we will use the SELECT
statement to build a result-set that includes all fields in the Customers table ordering the results by the LastName
field.
SELECT *
FROM Customers
ORDER BY LastName
SELECT *
FROM tbl_employees
ORDER BY employeeID
Results
employeeID | employeeName |
---|---|
1000 | John Smith |
1001 | Fred White |
1002 | Jane Scott |
1003 | Samuel Williams |