The ORDER BY Keyword


The ORDER BY keyword is used to sort the result. An Orders is given on the right:

 Company   OrderNumber 
Sega 3412
ABC Shop 5678
W3Schools 2312
W3Schools 6798

To display the companies in alphabetical order:

 SELECT  Company, OrderNumber
   FROM  Orders  ORDER BY  Company
 Company   OrderNumber 
ABC Shop 5678
Sega 3412
W3Schools 6798
W3Schools 2312

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

 SELECT  Company, OrderNumber
   FROM Orders ORDER BY Company, OrderNumber
 Company   OrderNumber 
ABC Shop 5678
Sega 3412
W3Schools 2312
W3Schools 6798

To display the companies in reverse alphabetical order:

 SELECT Company, OrderNumber
   FROM Orders ORDER BY Company DESC
 Company   OrderNumber 
W3Schools 6798
W3Schools 2312
Sega 3412
ABC Shop 5678

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

 SELECT Company, OrderNumber
   FROM Orders ORDER BY
     Company DESC, OrderNumber ASC
 Company   OrderNumber 
W3Schools 2312
W3Schools 6798
Sega 3412
ABC Shop 5678



The following is an SQL test area from w3schools.com where the Customers table may be created by the following command:
   SQL> create table  Customers (
     2    CustomerID   varchar(16),
     3    CompanyName  varchar(32),
     4    ContactName  varchar(32),
     5    Address      varchar(64),
     6    City         varchar(32),
     7    PostalCode   varchar(16),
     8    Country      varchar(32) );
Note that the Customers table is for read only.