pf » Returning TOP N Records
Returning TOP N Records
Returning only the first N records in a SQL query differs quite a bit between database platforms. Here's some samples:
Microsoft SQL Server
SELECT TOP 10 column FROM table
PostgreSQL and MySQL
SELECT column FROM table LIMIT 10
Oracle
SELECT column FROM table WHERE ROWNUM <= 10
Sybase
SET rowcount 10 SELECT column FROM table
Firebird
SELECT FIRST 10 column FROM table
Due to these differences if you want to keep your code database independent you should use the maxrows attribute in the cfquery tag in ColdFusion. The tradeoffs to database independance is performance, I would expect maxrows to be slower than specifying the rows in the SQL.
<cfquery datasource="#ds#" maxrows="10"> SELECT column FROM table </cfquery>
PostgreSQL and MySQL have a cool feature that will let you return an arbitrary range of rows (eg return rows 10-20). This is very handy for displaying pages of records:
SELECT column FROM table LIMIT 10 OFFSET 20
The above query will return rows 20-30
Need Help With SQL?
add to del.icio.us
| Tags: sql, mysql, oracle, postgresql, top, limit, rownum
Related Entries
- SQL to Select a random row from a database table - September 14, 2005
- LIMIT and OFFSET SQL Pagination - August 29, 2005
- SQL Reserved Key Words Checker Tool - March 28, 2005
- Top 10 Reserved SQL Keywords - October 28, 2008
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL - February 18, 2008
e.g. SELECT column FROM table LIMIT 10,20
The above query doesn't work properly in Oracle 9i database
FETCH FIRST 10 ROWS ONLY
set rowcount 3 select * from <tablename>
"select * from (select * from table order by dbms_random.value) where rownum<=5" pls suggest how to reduce the time taken by this query or tell me alternate way to fetch records randomly from a large table
Thanks & Regards Shashi Kumar Jha 09873061870
Thanks in advance
I was exactly looking the same stuff
Select top 10 * from employee
the above statement return top 10 row.
but i want the rows from the table other than the top 10. Can any one help me to get it.. iam using SQL server 2005
Thanks Bhaskar
thanks and regards
get the records from 11 to 20 rows
select * from (select top 20 ROW_NUMBER() over(order by col1) as rowsno ,* from tab1 where col3 not like '%abcd%') as tab2 where rowsno between '11' and '20'
use top keyword for faster result top n records is always equal or grater then the last limit of between comand its only works in 2005 server
How can we find only last updated row from multiple rows with last updated column.
Please tell me what to do....
SELECT TOP 10 * FROM tblname where title_id NOT IN (SELECT TOP 10 title_id FROM tblname ORDER BY title_id)ORDER BY title_id
here title_id is auto increment coloum, so u have to create table with auto increment coloum..
In DAO. Caused by: com.jnetdirect.jsql.m: Incorrect syntax near the keyword 'top'.
The records are retrieved from a stored procedure..
My requirement in to display 3 records per page. Can I use the above methods to retrieve the records from a stored procedure? If not please suggest me another way of getting the records..
Thanks Mahi
- ColdFusion 8 FCKeditor Vulnerability
- Ajax Same Origin Policy No More with Firefox 3.5
- Firefox 3.5 Introduces Origin Header, Security Features
- Tips for Secure File Uploads with ColdFusion
- 7 Years And Blog Entry Number 700
- CFCatch Java Exceptions
- Cheat Sheet for SQL Server
- CFML on Google App Engine for Java
RSS

Pete Freitag is a software engineer, and web developer located in











