Order By Clause (SQL)

Introduction

The SQL Order By clause sorts the rows of a query result.

Syntax

order-by-clause ::=
  ORDER BY sort-specification-list

sort-specification-list ::=
  sort-specification [ { ',' sort-specification } ]

sort-specification ::=
  value-expression [ ordering-specification ] [ null-ordering ]

ordering-specification ::=
  ASC | DESC

null-ordering ::=
  NULLS FIRST | NULLS LAST

Remarks

The sort-specification-list defines how the query result is sorted. Multiple sort-specification can be specified. The query result is sorted by the first sort-specification, followed by the second sort-specification, and so on.

The ASC keyword requests an ascending sort. The DESC keyword requests a descending sort.

NULL values are treated as lower than non-NULL values. The ordering of NULL values can be overridden by NULLS FIRST or NULLS LAST. Using NULLS FIRST places NULL values at the start of query result. Using NULLS LAST places NULL values at the end of query result.

Examples

To list all digital tags ordered by quality:

SELECT FullName,StateDesc,Quality
FROM TagDigital
ORDER BY Quality

Further Information

Fernhill SCADA Structured Query Language (SQL)

To learn more about the SQL features in Fernhill SCADA.

Glossary

For definitions of the terms used in Fernhill SCADA.