dbTalk Databases Forums  

SELECT TOP * ORDER BY question

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SELECT TOP * ORDER BY question in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
metaperl
 
Posts: n/a

Default SELECT TOP * ORDER BY question - 12-31-2007 , 12:50 PM






If I do

SELECT TOP 25 * FROM table ORDER BY id

does it select any 25 records and then order them

or does it SELECT all the records, order them by ID then return the
first 25?

I'm guessing the former (based on some comparative SELECTs) and think
I need to do this:

SELECT TOP 25
(SELECT * FROM table ORDER BY id)

to get what I want.

Reply With Quote
  #2  
Old   
metaperl
 
Posts: n/a

Default Re: SELECT TOP * ORDER BY question - 12-31-2007 , 01:17 PM






On Dec 31, 1:50*pm, metaperl <metap... (AT) gmail (DOT) com> wrote:

Quote:
SELECT TOP 25
* *(SELECT * FROM table ORDER BY id)
actually the above is invalid syntax. how can I get MS-SQL to give me
the 25 records which have the lowest id instead of 25 random records
ordered by id?


Reply With Quote
  #3  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: SELECT TOP * ORDER BY question - 12-31-2007 , 01:20 PM



metaperl,

Logically, this statement will select all rows from "table", sort them
on column id, select the first 25 of them, and return these rows.

If you would want the other behavior that you describe (which is
unlikely), you would have to write this:

SELECT *
FROM (
SELECT TOP 25 *
FROM table
ORDER BY NEWID()
) AS T
ORDER BY T.id

--
Gert-Jan


metaperl wrote:
Quote:
If I do

SELECT TOP 25 * FROM table ORDER BY id

does it select any 25 records and then order them

or does it SELECT all the records, order them by ID then return the
first 25?

I'm guessing the former (based on some comparative SELECTs) and think
I need to do this:

SELECT TOP 25
(SELECT * FROM table ORDER BY id)

to get what I want.

Reply With Quote
  #4  
Old   
David Portas
 
Posts: n/a

Default Re: SELECT TOP * ORDER BY question - 12-31-2007 , 01:42 PM



"metaperl" <metaperl (AT) gmail (DOT) com> wrote

Quote:
If I do

SELECT TOP 25 * FROM table ORDER BY id

does it select any 25 records and then order them

or does it SELECT all the records, order them by ID then return the
first 25?

I'm guessing the former (based on some comparative SELECTs) and think
I need to do this:

SELECT TOP 25
(SELECT * FROM table ORDER BY id)

to get what I want.

Books Online is your friend:

"If the query includes an ORDER BY clause, the first expression rows, or
expression percent of rows, ordered by the ORDER BY clause are returned. If
the query has no ORDER BY clause, the order of the rows is arbitrary."

http://msdn2.microsoft.com/en-us/library/ms189463.aspx

This is a very silly syntax (invented by Microsoft and not part of standard
SQL) because it means the ORDER BY serves a double purpose, which leads to
confusion all round.

--
David Portas




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.