Selection with sorted Results

suggest change
SELECT * FROM Employees ORDER BY LName

This statement will return all the columns from the table Employees. ASC is the default. Where NULL sorts is defined by the dialect.

Id FName LName PhoneNumber
2 John Johnson 2468101214
1 James Smith 1234567890
4 Johnathon Smith 1212121212
3 Michael Williams 1357911131
SELECT * FROM Employees ORDER BY LName DESC

DESC reverses that order.

SELECT * FROM Employees ORDER BY LName ASC

One may also specify multiple sorting columns. For example:

SELECT * FROM Employees ORDER BY LName ASC, FName ASC

This example will sort the results first by LName and then, for records that have the same LName, sort by FName. This will give you a result similar to what you would find in a telephone book.

In order to save retyping the column name in the ORDER BY clause, it is possible to use instead the column’s number. Note that column numbers start from 1.

SELECT Id, FName, LName, PhoneNumber FROM Employees ORDER BY 3

You may also embed a CASE statement in the ORDER BY clause.

SELECT Id, FName, LName, PhoneNumber FROM Employees ORDER BY CASE WHEN LName = 'Jones' THEN 0 ELSE 1 END ASC

This will sort your results to have all records with the LName of “Jones” at the top.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents