Results 1 to 6 of 6

Thread: How to sort the query result in SQL?

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    How to sort the query result in SQL?

    Hi friends,

    I just started the SQL study, so I have little bit knowledge about the SQL queries. I have to write the query to to sort the result of query applied to table. I am getting confused, about which command I need to use to achieve this requirement. I tried many commands but no success.
    If you know how to sort the query result in SQL, then please let me know that. I would greatly appreciate your help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to sort the query result in SQL?

    Hi,

    If you want to sort the query result in SQL, then you need to use the 'Order By' command of the SQL. Using this command you are able to sort the query result in ascending or descending order. By default the 'Order By' command returns the result in the ascending order. Please refer following it's example:
    Select empName, empID, empSal
    from empDetails
    Order By empName;

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to sort the query result in SQL?

    Hello friend,

    We can sort result of query either in ascending order or descending order. To sort result in ascending order we have to use the "ORDER BY" clause with name of the column according to which we want to apply the sorting. Following is it's syntax with example:
    Syntax:
    select columnNAME
    from tableNAME
    order by columnName;
    Example:
    select StudName, studID
    from StudDemo
    order by studID;

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to sort the query result in SQL?

    It is also possible to sort the result of sql query basis on more than one column of the table. you just need to put the number of column names after the 'order by' clause. Suppose we want to sort the query result of the 'BankDemo' table which is on the ascending order of columns 'BankEmpName' and 'BankEmpID' then you we should have to use the following SQL query:
    SELECT BankEmpName, BankEmpDesignation, BankEmpID
    FROM BankDemo ORDER BY BankEmpName, BankEmpID ;

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to sort the query result in SQL?

    There are two types of sorting we can do in SQL i.e Descending and Ascending. If you want to apply ascending sorting then should use the 'Order By' sql clause. And if you want to apply descending sorting then should use the 'Order By' sql clause with 'Desc' clause. For reference see below:
    Ascending sorting:
    SELECT column_Name1, column_Name2, column_Name3
    FROM Table_Name ORDER BY column_Name2, column_Name1;
    Descending sorting:
    SELECT column_Name1, column_Name2, column_Name3
    FROM Table_Name ORDER BY column_Name2 Desc, column_Name1 Desc;

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to sort the query result in SQL?

    Hi friend,

    I suggest you to use the order by clause along with the select command of the sql for sorting query result. But there are many limitation over this clause of sql, for example you can not use this clause for derived tables, views, sub-queries and inline functions. In short you can not apply order by clause for more complex queries. If you want to know more about the sorting result of sql queries then please go through below link:
    http://msdn.microsoft.com/en-us/library/ms188385.aspx

Similar Threads

  1. Replies: 4
    Last Post: 24-04-2012, 07:53 AM
  2. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  3. Insert sql query result in a file
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 11:52 AM
  4. Difference in SQLite and SQLite3 query result
    By KAILEY in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 04:57 PM
  5. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,853,528.84302 seconds with 16 queries