pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Support for SQL TOP clause?
4+ messages / 4 participants
[nested] [flat]

* Support for SQL TOP clause?
@ 2008-01-10 03:13  Chinyi Woo <chinyi.woo@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Chinyi Woo @ 2008-01-10 03:13 UTC (permalink / raw)
  To: pgsql-sql

Hello, everyone
Does Postgresql support query like SELECT *TOP 3* * FROM Individual  ? If I
use ORDER BY, I have to write non-sql code to get the first row in the
result set, which I try to avoid.

Thanks
Chinyi

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Support for SQL TOP clause?
@ 2008-01-10 03:19  Phillip Smith <phillip.smith@weatherbeeta.com.au>
  parent: Chinyi Woo <chinyi.woo@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Phillip Smith @ 2008-01-10 03:19 UTC (permalink / raw)
  To: 'Chinyi Woo' <chinyi.woo@gmail.com>; pgsql-sql

SELECT *

FROM   Individual

LIMIT 3

 

I asked the reverse question moving from PG to MSSQL :-(

 

-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Chinyi Woo
Sent: Thursday, 10 January 2008 14:14
To: pgsql-sql@postgresql.org
Subject: [SQL] Support for SQL TOP clause?

 

Hello, everyone

Does Postgresql support query like SELECT TOP 3 * FROM Individual  ? If I
use ORDER BY, I have to write non-sql code to get the first row in the
result set, which I try to avoid.

 

Thanks

Chinyi



THINK BEFORE YOU PRINT - Save paper if you don't really need to print this

*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to
the addressee.  If you are not the addressee indicated in this message or
responsible for delivery of the message to such person, you may not copy
or deliver this message to anyone, and you should destroy it and kindly
notify the sender by reply email.

Information in this message that does not relate to the official business
of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.
Weatherbeeta, its employees, contractors or associates shall not be liable
for direct, indirect or consequential loss arising from transmission of this
message or any attachments
e-mail.


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Support for SQL TOP clause?
@ 2008-01-10 11:46  Reinoud van Leeuwen <reinoud.v@n.leeuwen.net>
  parent: Phillip Smith <phillip.smith@weatherbeeta.com.au>
  0 siblings, 0 replies; 4+ messages in thread

From: Reinoud van Leeuwen @ 2008-01-10 11:46 UTC (permalink / raw)
  To: pgsql-sql

On Thu, Jan 10, 2008 at 02:19:43PM +1100, Phillip Smith wrote:
> SELECT *
> 
> FROM   Individual
> 
> LIMIT 3

Note that you will have to add an 'order by' clause to guarantee 
predictable results...

-- 
__________________________________________________
"Nothing is as subjective as reality"
Reinoud van Leeuwen    reinoud.v@n.leeuwen.net
http://www.xs4all.nl/~reinoud
__________________________________________________



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Support for SQL TOP clause?
@ 2008-01-10 13:56  Richard Broersma Jr <rabroersma@yahoo.com>
  parent: Chinyi Woo <chinyi.woo@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Richard Broersma Jr @ 2008-01-10 13:56 UTC (permalink / raw)
  To: pgsql-sql@postgresql.org, Chinyi Woo <chinyi.woo@gmail.com>

--- On Wed, 1/9/08, Chinyi Woo <chinyi.woo@gmail.com> wrote:

> Does Postgresql support query like SELECT *TOP 3* * FROM
> Individual  ? If I use ORDER BY, I have to write non-sql
> code to get the first row in the result set, which I try
> to avoid.

As you have seen, PostgreSQL's implementation of LIMIT predicate can produce the same results as TOP.

This is another way to get these results using neither TOP or Limit, but the query will probably not perform as quickly.

    SELECT I1.name, ...
      FROM Individual AS I1
INNER JOIN Individual AS I2
        ON I1.name < I2.name
     WHERE --any where criteria you might have
  GROUP BY I1.name, ...
    HAVING COUNT(*) < 3
  ORDER BY I1.name;

Regards,
Richard Broersma Jr.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2008-01-10 13:56 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-01-10 03:13 Support for SQL TOP clause? Chinyi Woo <chinyi.woo@gmail.com>
2008-01-10 03:19 ` Phillip Smith <phillip.smith@weatherbeeta.com.au>
2008-01-10 11:46   ` Reinoud van Leeuwen <reinoud.v@n.leeuwen.net>
2008-01-10 13:56 ` Richard Broersma Jr <rabroersma@yahoo.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox