Previous Topic: Selecting Using Search ConditionsNext Topic: Eliminating Duplicate Rows


Ordering by Column Values

When the ordering of retrieved data is important, use the ORDER BY clause.


 Problem
Specify columns CUST_NO, CITY, and STATE for retrieval from the
CUSTOMERS table.  Place rows of the result table in ascending order by
CUST_NO.
 Solution

               .
               .
       (COBOL statements)
               .
               .
  1   EXEC SQL
  2       DECLARE CUSTLIST CURSOR FOR
  3           SELECT CUST_NO, CITY, STATE
  4           FROM CUSTOMERS
  5           ORDER BY CUST_NO
  6   END-EXEC.
               .
               .
       (COBOL statements)
               .
               .

Line 3

Three columns are selected for inclusion in the result table.

Line 5

The ORDER BY clause specifies the order the rows are to be placed in the result table. The default order is ascending, so the rows are in ascending order, that is to say, the lowest customer identification number to the highest.