SQL Order By Clause
Tips>>SQL

MAKE FULL WEB APPLICATION WITH JUST SQL KNOWLEDGE? CLICK HERE


Our SQL tutorial
will help to teach you how to use commonly used SQL commands and you will be able to apply most of the knowledge gathered from this SQL tutorial to any of the databases above :-

The SQL ORDER BY clause comes in easy to use when you want to sort your SQL result sets by some column(s). You can use a column, which can compare to other similar column number instead of the column name in the ORDER BY clause. ASC is the default sort order.

The Syntax of ORDER BY clause is :-

The syntax for the ORDER BY clause is to obtain and display data in a specific order :-

SELECT select list
FROM tablename
[ORDER BY column-name I select_ list_number 1 expression
[ASCIDEESC] [,column_ name I select_list_number I expression
[ASXIDESC]….]

For Instance,

The ORDER BY clause is used to sort the rows.

 

Orders :-

Company Order Number
India Links 3412
A. M Computers 5678
Virtual Splat 2312
Virtual Splat 6798

 

 

EXAMPLE 1:-

To display the companies in alphabetical order:

SELECT Company, Order Number FROM Orders

ORDER BY Company

 

RESULT:-

Company Order Number
A. M Computers 5678
India Links 3412
Virtual Splat 6798
Virtual Splat 2312

 

 

EXAMPLE 2:-

To display the companies in alphabetical order AND the order numbers in numerical order:

SELECT Company, Order Number FROM Orders

ORDER BY Company,Order Number


RESULTS:-

Company Order Number
A. M Computers 5678
India Links 3412
Virtual Splat 2312
Virtual Splat 6798

 

 

EXAMPLE 3:-

To display the companies in reverse alphabetical order:

SELECT Company, Order Number FROM Orders

ORDER BY Company DESC

 

RESULT:-

Company Order Number
Virtual Splat 6798
Virtual Splat 2312
India Links 3412
A. M Computers 5678

 

 

EXAMPLE 4:-

To display the companies in reverse alphabetical order AND the order numbers in numerical order:

SELECT Company, OrderNumber FROM Orders

ORDER BY Company DESC, OrderNumber ASC

RESULT:-

Company Order Number
Virtual Splat 2312
Virtual Splat 6798
India Links 3412
A. M Computers 5678

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641