VisualBuilder
  Home > Database > Tutorials > Arraging Data with Order by - SQL tutorial
Tell a friend
Link to us
Total Members
      Members: 84648
     
Sitemap Forum Chat
Home
SQL Tutorial Tutorial Home
1 . History of SQL
2 . SQL Components: An Overview
3 . Data Types in SQL
4 . Planning a Database: An Example: -
5 . Creating Tables
6 . Inserting Data using Insert
7 . Getting Data with Select Statement
8 . Working with Operators
9 . Selecting Unique rows with Distinct
10 . Arraging Data with Order by
11 . Working with Strings
12 . SQL AND
13 . Basic SQL Funtion
14 . Sub-Queries, Nested Queries
15 . Constraints in the Table
16 . SQL UPDATE
17 . View
18 . Relationships in a RDBMS
19 . Structure Modification,Updation and Deletion
 
Database Group Home
Database Discussion
Database Members (506)
Database Resources
Database Source Code (0)
Database Articles (0)
Database Blogs
Database Jobs
Database Components (0)
Database Books
Database Websites (0)
Database News (0)
Database Q & A (14)
- Database Ask Question
- Database Questions
- Database Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
LINQ
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Grails Tutorial
Hibernate Tutorial
Java 1.6 Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Service-Oriented Architecture (SOA) Tutorial For Managers
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (436 )
Source Code (3275 )
Articles (11 )
Components (1589 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Database sql Tutorial
 Arraging Data with Order by
  << Prev: Selecting Unique rows with Distinct Next: Working with Strings >>

ORDER BY clause is used to order the data sets retrieved from a SQL database. The ordering of the selected data can be done by one or more columns in a table. If we want to sort our Users table by the FirstName column, we'll have to use the following ORDER BY SQL statement:


SELECT * FROM Users ORDER BY FirstName;


The result of the ORDER BY statement above will be the following:















































FirstName LastName DateOfBirth Email City
David Stonewall 01/03/1954 david@sql-tutorial.com San Francisco
John Smith 12/12/1969 john.smith@john-here.com New York
Paul O'Neil 09/17/1982 paul.oneil@pauls-email.com New York
Stephen Grant 03/03/1974 sgrant@sgrantemail.com Los Angeles
Susan Grant 03/03/1970 susan.grant@sql-tutorial.com Los Angeles

As you can see the rows are ordered alphabetically by the FirstName column. You can use ORDER BY to order the retrieved data by more than one column. For example, if you want to order by both LastName and City columns, you would do it with the following ORDER BY statement:


SELECT * FROM Users ORDER BY LastName, DateOfBirth;


Here is the result of this ORDER BY statement:















































FirstName LastName DateOfBirth Email City
Susan Grant 03/03/1970 susan.grant@sql-tutorial.com Los Angeles
Stephen Grant 03/03/1974 sgrant@sgrantemail.com Los Angeles
Paul O'Neil 09/17/1982 paul.oneil@pauls-email.com New York
John Smith 12/12/1969 john.smith@john-here.com New York
David Stonewall 01/03/1954 david@sql-tutorial.com San Francisco

When using ORDER BY with more than one column, you need to separate the list of columns following ORDER BY with commas. What will happen if we reverse the order of the columns specified after the ORDER BY statement like in the statement below?


SELECT * FROM Users ORDER BY DateOfBirth, LastName;


This ORDER BY statement will return the same results as the one with the reversed columns order, but they will be ordered differently. Here is the result:















































FirstName LastName DateOfBirth Email City
David Stonewall 01/03/1954 david@sql-tutorial.com San Francisco
John Smith 12/12/1969 john.smith@john-here.com New York
Susan Grant 03/03/1970 susan.grant@sql-tutorial.com Los Angeles
Stephen Grant 03/03/1974 sgrant@sgrantemail.com Los Angeles
Paul O'Neil 09/17/1982 paul.oneil@pauls-email.com New York

The ORDER BY clause first sorts the retrieved data by the first column, then the next one, and so forth. In all the ORDER BY examples so far, we were sorting alphabetically for character columns (FirstName, LastName) and from earlier to later date for the DateOfBirth column. What do we do if we want to order our data alphabetically but this time backwards? In order to accomplish that we need to use the DESC SQL keyword:


SELECT * FROM Users ORDER BY FirstName DESC;


Here is the result:















































FirstName LastName DateOfBirth Email City
Susan Grant 03/03/1970 susan.grant@sql-tutorial.com Los Angeles
Stephen Grant 03/03/1974 sgrant@sgrantemail.com Los Angeles
Paul O'Neil 09/17/1982 paul.oneil@pauls-email.com New York
John Smith 12/12/1969 john.smith@john-here.com New York
David Stonewall 01/03/1954 david@sql-tutorial.com San Francisco

  << Prev: Selecting Unique rows with Distinct Next: Working with Strings >>
Database Sql Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
online fax
DSL, SDSL, ADSL
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising