To explicitly sort data retrieved using a SELECT statement, the ORDER BY clause is used.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC; |
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | Accessories | 65.00 | 20 | 1,300 |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | Ram | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | Pen | Stationery | 7.45 | 10 | 74.50 |
7007 | Pendrive | Accessories | 65.00 | 20 | 1,300 |
SELECT ProductID, ProductName, Category, Price FROM Product ORDER BY Price; |
This statement sorts the data alphabetically in the ascending order by the Price column.
ProductID | ProductName | Category | Price |
---|---|---|---|
7006 | Pen | Stationery | 7.45 |
7004 | Ram | Components | 23.50 |
7003 | Keyboard | Accessories | 36.00 |
7002 | Harddrive | Accessories | 65.00 |
7007 | Pendrive | Accessories | 65.00 |
7001 | Mouse | Accessories | 75.00 |
7005 | Honda | Bikes | 1,200 |
To sort by multiple columns, simply specify the column names separated by commas in the ORDER BY clause.
SELECT ProductID, ProductName, Price FROM Product ORDER BY Price, ProductName; |
This code retrieves three columns and sorts the results by two of them, first by Price and then by ProductName (i.e. If more than one record present with same price. those records will be sorted again based on the name).
ProductID | ProductName | Price |
---|---|---|
7006 | Pen | 7.45 |
7004 | Ram | 23.50 |
7003 | Keyboard | 36.00 |
7002 | Harddrive | 65.00 |
7007 | Pendrive | 65.00 |
7001 | Mouse | 75.00 |
7005 | Honda | 1,200 |
ORDER BY also supports ordering specified by relative column position.
SELECT ProductID, Price, ProductName FROM Product ORDER BY 2, 3; |
ORDER BY 2 means sort by the second column in the SELECT list, the Price column. ORDER BY 2, 3 means sort by Price and then by ProductName.
ProductID | Price | ProductName |
---|---|---|
7006 | 7.45 | Pen |
7004 | 23.50 | Ram |
7003 | 36.00 | Keyboard |
7002 | 65.00 | Harddrive |
7007 | 65.00 | Pendrive |
7001 | 75.00 | Mouse |
7005 | 1,200 | Honda |
Data sorting is not limited to ascending sort orders (from A to Z). Although this is the default sort order, the ORDER BY clause can also be used to sort in descending order (from Z to A).
To sort by descending order, the keyword DESC must be specified.
SELECT ProductID, ProductName, Price FROM Product ORDER BY Price DESC; |
This SQL sorts the products by Price in descending order (most expensive product present first).
ProductID | ProductName | Price |
---|---|---|
7005 | Honda | 1,200 |
7001 | Mouse | 75.00 |
7002 | Harddrive | 65.00 |
7007 | Pendrive | 65.00 |
7003 | Keyboard | 36.00 |
7004 | Ram | 23.50 |
7006 | Pen | 7.45 |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!