You are here: Re: SELECT TOP * ORDER BY question « MsSQL Server « IT news, forums, messages
Re: SELECT TOP * ORDER BY question

Posted by Gert-Jan Strik on 12/31/07 19:20

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:
>
> 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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация