Search content within the blog

Friday, April 17, 2009

Order by in SqlServer

The order by statement allows for table column range. It allows for ascending or descending lists of column values, permitting SQL to reorder your table rows for the purpose of viewing.

Syntax:
SELECT * FROM employees ORDER BY Lastname;

Output:-
Lastname Firstname Title
Davis Julie manager
Harris Joel crew
Hicks Freddy crew
Hively Jessica crew
Johnson David crew

The above example arrange our rows of data alphabetically by lastname.

Ordering by two different columns

First we alphabatize our job titles and again we order by lastname.

Syntax:
SELECT * FROM employees ORDER BY Title,Lastname;

Output:-
Lastname Firstname Title
Harris Joel crew
Hicks Freddy crew
Hively Jessica crew
Johnson David crew
Davis Julie manager

No comments:

Post a Comment