Order by

Order by

An ORDER BY clause in SQL specifies that a SQL SELECT statement returns a result set with the rows being sorted by the values of one or more columns. The sort criteria do not have to be included in the result set. The sort criteria can be expressions, including – but not limited to – column names, user-defined functions, arithmetic operations, or CASE expressions. The expressions are evaluated and the results are used for the sorting, i.e. the values stored in the column or the results of the function call.

ORDER BY is the only way to sort the rows in the result set. Without this clause, the relational database system may return the rows in any order. If an ordering is required, the ORDER BY must be provided in the SELECT statement sent by the application. Although some database systems allow the specification of an ORDER BY clause in subselects or view definitions, the presence there has no effect. A view is a logical relational table, and the relational model mandates that a table is a set of rows, implying no sort order whatsoever. The only exception are constructs like ORDER BY ORDER OF ... (not standardized in SQL:2003) which allow the propagation of sort criteria through nested subselects.

The SQL standard's core functionality does not explicitly define a default sort order for Nulls. With the SQL:2003 extension T611, "Elementary OLAP operations", nulls can be sorted before or after all data values by using the NULLS FIRST or NULLS LAST clauses of the ORDER BY list, respectively. Not all DBMS vendors implement this functionality, however. Vendors who do not implement this functionality may specify different treatments for Null sorting in the DBMS.[1]

Structure ORDER BY ... DESC will order in descending order, otherwise ascending order is used. (The latter may be specified explicitly using ASC.)

Examples

SELECT * FROM Employees 
ORDER BY LastName, FirstName

This sorts by the LastName field, then by the FirstName field if LastName matches.

References



Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • order — or·der 1 n 1: a state of peace, freedom from unruly behavior, and respect for law and proper authority maintain law and order 2: an established mode or state of procedure a call to order 3 a: a mandate from a superior authority see also …   Law dictionary

  • Order — Or der, n. [OE. ordre, F. ordre, fr. L. ordo, ordinis. Cf. {Ordain}, {Ordinal}.] [1913 Webster] 1. Regular arrangement; any methodical or established succession or harmonious relation; method; system; as: (a) Of material things, like the books in …   The Collaborative International Dictionary of English

  • Order Up! — Developer(s) SuperVillain Studios Publisher(s) NA …   Wikipedia

  • Order No. 1 — Order Number 1 was issued March 1, 1917 (O.S.) and was the first official decree of The Petrograd Soviet of Workers and Soldiers Deputies. The order was issued following the February Revolution in response to actions taken the day before by the… …   Wikipedia

  • order — [ôr′dər] n. [OFr ordre < L ordo (gen. ordinis), straight row, regular series, akin to ordiri, to lay the warp, hence begin, set in order, prob. < IE base * ar , to join, fit > ARM1, ART1] 1. social position; rank in the community 2. a… …   English World dictionary

  • order — ► NOUN 1) the arrangement of people or things according to a particular sequence or method. 2) a state in which everything is in its correct place. 3) a state in which the laws and rules regulating public behaviour are observed. 4) an… …   English terms dictionary

  • order — [n1] arrangement, organization adjustment, aligning, array, assortment, cast, categorization, classification, codification, composition, computation, disposal, disposition, distribution, establishment, form, grouping, harmony, layout, line,… …   New thesaurus

  • Order — Or der, v. t. [imp. & p. p. {Ordered}; p. pr. & vb. n. {Ordering}.] [From {Order}, n.] 1. To put in order; to reduce to a methodical arrangement; to arrange in a series, or with reference to an end. Hence, to regulate; to dispose; to direct; to… …   The Collaborative International Dictionary of English

  • order# — order n 1 *association, society, club 2 *command, injunction, bidding, behest, mandate, dictate Analogous words: instruction, direction, charging or charge (see corresponding verbs at COMMAND) order vb …   New Dictionary of Synonyms

  • Order 81 — is one of the most controversial of Paul Bremer s 100 Orders, issued during the reconstruction of Iraq following the invasion of Iraq by the United States in 2003. The order establishes intellectual property restrictions on the use of genetically …   Wikipedia

  • Order — Sf Anweisung, Befehl per. Wortschatz fremd. Erkennbar fremd (17. Jh.) Entlehnung. Entlehnt aus frz. ordre, das von l. ordo Ordnung, Regel stammt. Verb: ordern, beordern.    Ebenso nndl. order, ne. order, nschw. order, nnorw. ordre; Orden.… …   Etymologisches Wörterbuch der deutschen sprache

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”