ARTICLE

ORDER BY clause in T-SQL

Posted by Manish Tewatia Articles | Database & DBA April 11, 2011
ORDER BY clause is an optional element of a SELECT statement. When you execute SQL queries to retrieve data from a database server.
 
Reader Level:

The ORDER BY clause is an optional element of a SELECT statement. When you execute SQL queries to retrieve data from a database server, you get a data set in whatever orders it is. Therefore you need SQL ORDER BY clause to sort the data in order you want it to be. An ORDER BY clause allows you to specify the order in which rows appear in the ResultSet.

SQL ORDER BY clause allows you to:

  • Sort data set in single column of values of mutiple columns.
  • Sort any column in either ascending order (ASC) by default or descending order(DESC)
  • Refer to sort columns by name, by their position within the output column list of data set, or by using an alias.

It is permissible to combine ORDER BY on the outer query with ORDER BY in subqueries. It is not valid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

syntex:

ORDER BY { column-Name | ColumnPosition } [ ASC | DESC ] [ , column-Name | 
ColumnPosition
[ ASC | DESC ] ] *

Example:

Suppose we have two tables named Category and books look like:

Category_Id      Category
---------------     -----------
       1                  Arts

      
2                  science

      
3                  commerce

      
4                  phsical

books_ID  books_Title   publisher_ID    Category_Id     SalesPrice    AverageCost
-----------  -------------   --------------    --------------     ------------    --------------

   
001          ABCD             34561                2                    200               180

   
002          HJFG              15465                2                    225               200

   
003          YDJDF            14425                1                    352               300

   
004          GJHNDF          65866               1                    122               100

   
005          FGHFFF          86655                1                    100                 80

   
006          FNHDFFN        79256               3                      90                 70

   
007          HGNDGCF      49856                3                    421               350

   
008          GFNSDG         45965                4                    350               300

   
009          GDFTG            58546               1                    100                 90

   
010          GFBGB            56632               2                     200               175


Here is the query:

SELECT books.Category_Id,Category.Category,
 
      Count(books.books_ID) "Total sum"

FROM books,Category

WHERE  books.Category_Id = Category.Category_Id

GROUP BY books.Category_Id,Category.Category

ORDER BY Category.Category

GO

Output:

Category_Id    Category       Total sum
--------------   -------------     -----------

      
3            commerce            2   
             
      2             science               3                     

      4             phsical                1        
               
     
      
1             Arts                    4              
       
  

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor