Thread: Support for SQL TOP clause?

Support for SQL TOP clause?

From
"Chinyi Woo"
Date:
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

Re: Support for SQL TOP clause?

From
"Phillip Smith"
Date:

SELECT *

FROM   Individual

LIMIT 3

 

I asked the reverse question moving from PG to MSSQL L

 

-----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 e-mail.

*******************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

Re: Support for SQL TOP clause?

From
Reinoud van Leeuwen
Date:
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
__________________________________________________


Re: Support for SQL TOP clause?

From
Richard Broersma Jr
Date:
--- 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.