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
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
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL - February 18, 2008
- Cheat Sheet Roundup - Over 30 Cheatsheets for developers - September 1, 2005
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.
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
Pete Freitag is a software engineer, and web developer located in










