How does the Query Engine know how to order via newid()

While we all know how to display a result in random order via newid() function, I wonder how the query engine interprets a GUID(result of newid), since ORDER BY statement expects a column list (or position)?

NOTE: And if the GUID somehow magically is converted to column position, this assumption is invalid for a table with single column, since it will return results in random order.


more ▼

asked Nov 19, 2009 at 09:41 AM in Default

avatar image

204 5 5 10

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

As you can see in the text from books online (bold text), order by expression can include an expression. NEWID() that is nondetermanistic is executed for every row returned by your select and then order by is applied on your set of data,

Specifies a column on which to sort. A sort column can be specified as a name or column alias, or a nonnegative integer representing the position of the name or alias in the select list. An integer cannot be specified when the order_by_expression appears in a ranking function. A sort column can include an expression, but when the database is in SQL Server (90) compatibility mode, the expression cannot resolve to a constant. Column names and aliases can be qualified by the table or view name. In SQL Server, qualified column names and aliases are resolved to columns listed in the FROM clause. If order_by_expression is not qualified, the value must be unique among all columns listed in the SELECT statement.

more ▼

answered Nov 19, 2009 at 09:56 AM

avatar image

Håkan Winther
16.6k 38 46 58

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Nov 19, 2009 at 09:41 AM

Seen: 1947 times

Last Updated: Nov 19, 2009 at 09:41 AM

Copyright 2018 Redgate Software. Privacy Policy