Ordering query results

In this section, you will add an ORDER BY clause to the SELECT statement to display results in alphabetical or numerical order.

Unless otherwise requested, Sybase IQ displays the rows of a table in no particular order. Often it is useful to look at the rows in a table in a more meaningful sequence. For example, you might like to see employees in alphabetical order.

Listing employees in alphabetical order

The following example shows how adding an ORDER BY clause to the SELECT statement causes the results to be retrieved in alphabetical order.

SELECT * FROM Employees ORDER BY Surname

EmployeeID

ManagerID

Surname

GivenName

DepartmentID

...

1,751

1,576

Ahmed

Alex

400

...

1,013

703

Barker

Joseph

500

...

591

1,576

Barletta

Irene

400

...

191

703

Bertrand

Jeannette

500

...

1,336

1,293

Bigelow

Janet

300

...

1,062

1,576

Blaikie

Barbara

400

...

750

703

Braun

Jane

500

...

160

501

Breault

Robert

100

...

1,191

1,576

Bucceri

Matthew

400

...

Notes

The order of the clauses is important. The ORDER BY clause must follow the FROM clause and the SELECT clause.

NoteIf you omit the FROM clause, or if all tables in the query are in the SYSTEM dbspace, the query is processed by SQL Anywhere instead of Sybase IQ and may behave differently, especially with respect to syntactic and semantic restrictions and the effects of option settings. See the SQL Anywhere documentation for rules that may apply to processing.

If you have a query that does not require a FROM clause, you can force the query to be processed by Sybase IQ by adding the clause FROM iq_dummy, where iq_dummy is a one-row, one-column table that you create in your database.