Re: Selecting only the first row from a query

From: Brian M. Biggs <bbiggs_at_cincom.com>
Date: 1995/11/30
Message-ID: <49kfvu$4pk_at_wormhole.cincom.com>#1/1


joel_nylund_at_mail.amsinc.com (Joel Nylund) wrote:
>
>
>I need to write a sql statement to only return the first row that meets >the criteria. For example I have a table called people wit=
 h names and >phone numbers. There may be several "Joel Nylund's" in the table, I only >want the first one I find.
>
>in DB2, I can do a select first
>
>I am looking for something that will work in oracle and sybase.
>
>thanks in advance
>Joel

Try the following:

SELECT phone_number
FROM people
WHERE name = 'Joel Nylund' AND

      rownum = 1;

This will return the first row in the result set. Note that if you stick an ORDER BY clause on the query, the row number (rownum) is determined BEFORE the sort takes place. This would only have an effect if you did something like 'WHERE rownum <= 50' to return the first 50 rows of a query result.

-- 
Brian M. Biggs                             mailto:bbiggs_at_cincom.com
Cincom Systems, Inc.                       voice: (513) 677-7661
http://www.cincom.com/
Received on Thu Nov 30 1995 - 00:00:00 CET

Original text of this message