Re: top n problem

From: Delbert Glass (delbert_at_noincoming.com)
Date: 02/13/04


Date: Fri, 13 Feb 2004 17:23:38 -0600

Any abuse that is occurring
would be from using TOP at all
since top is not standard SQL -- not from
using TOP without ORDER BY
which is clearly allowed
by the following passage from BOL:

>TOP n [PERCENT]
>
>Specifies that only the first n rows are
>to be output from the query result set.
>n is an integer between 0 and 4294967295.
>If PERCENT is also specified, only the
>first n percent of the rows
>are output from the result set.
>When specified with PERCENT,
>n must be an integer between 0 and 100.
>
>If the query includes an ORDER BY clause,
>the first n rows (or n percent of rows)
>ordered by the ORDER BY clause are output.
>If the query has no ORDER BY clause,
>the order of the rows is arbitrary.
>
>WITH TIES
>
>Specifies that additional rows be returned
>from the base result set with the same value
>in the ORDER BY columns appearing as
>the last of the TOP n (PERCENT) rows.
>TOP ...WITH TIES can only be specified
>if an ORDER BY clause is specified.

---
Perhaps you'll find the following helpful.
Just because ALL often should be included
in queries that contain UNION -- but often
is inadvertitely omitted,
does not mean UNION without ALL is undefined.
Likewise:
Just because ORDER BY often should be included
in queries that contain TOP -- but often
is inadvertitely omitted,
does not mean TOP without ORDER BY is undefined.
Bye,
Delbert Glass
"Vlad Vissoultchev" <wqweto@nospam.myrealbox.com> wrote in message
news:uMxyjnn8DHA.3880@tk2msftngp13.phx.gbl...
> > > the point here is that TOP n without ORDER BY is undefined
> >
> > Pray tell, where does it say that in the documention?
> > Are you just repeating mis-information, or did you make that up
yourself?
>
> ok,
>
> [ab]use it as you wish. next you can start rewriting those set-oriented
> selects using cursors -- it's legal T-SQL syntax :-))
>
> my point here is that everytime a TOP n w/o ORDER BY is used should be
> considered a gross hack. there are numerous examples of dubious
> selects/updates that [seem to] work but might just stop to under different
> conditions.
>
> btw, i get the same gut feeling when a see an IDENTITY property used but
> this is OT by far and large...
>
> cheers,
> </wqw>
>
>


Relevant Pages

  • RE: Any good T-SQL quick reference recommended?
    ... The full syntax of the SELECT ... SELECT Clause ... Specifies the columns to be returned by the query. ... Specifies that duplicate rows can appear in the result set. ...
    (microsoft.public.sqlserver.programming)
  • Re: SQL SELECT TOP question
    ... Specifies that only the first n rows are to be output from the query result ... rows) ordered by the ORDER BY clause are output. ... "Brad" wrote in message ...
    (microsoft.public.dotnet.languages.vb)
  • Re: SELECT TOP
    ... percentage of rows in the query result. ... You must include an ORDER BY clause ... The ORDER BY clause specifies the columns ... Therefore, if you specify 10 for nExpr, the query result can contain more ...
    (microsoft.public.fox.programmer.exchange)
  • Re: top n problem
    ... Delbert Glass wrote: ... If the query has no ORDER BY clause, the order of the rows is arbitrary. ... "A car burns fuel." ... >n specifies how many rows are returned. ...
    (microsoft.public.sqlserver.programming)
  • [Info-ingres] Wrong results returned from outer join
    ... " Outer joins specified in the from clause are not the same as joins ... while the where clause specifies restrictions to be applied to the sources ... Still the script returns all 2 rows. ... ON COMMIT PRESERVE ROWS WITH NORECOVERY ...
    (comp.databases.ingres)