Returning TOP N Records
By Pete Freitag
Returning only the first N records in a SQL query differs quite a bit between database platforms. For example, you can't say on oracle select top 100. Here's some of the different ways you can return the top 10 records on various database platforms (SQL Server, PostgreSQL, MySQL, etc):
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 independence 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
Using LIMIT and OFFSET is a great way to do paging with SQL on MySQL or PostgreSQL.
Returning TOP N Records was first published on April 18, 2003.
If you like reading about sql, mysql, oracle, postgresql, top, limit, or rownum then you might also like:
- SELECT a random row with SQL
- SQL Pagination with LIMIT and OFFSET
- SQL Reserved Key Words Checker Tool
- Order by NULL Values in MySQL, Postgresql and SQL Server
Discuss / Follow me on Twitter ↯
Tweet Follow @pfreitagComments
fetch.rows.between(tablename,min,max)
Every thing is well .but when we are using query for randomly fetching record from large table
This Query take more time
"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
I have a serious problem in sql 2000, I want top records from 11 to 20, in sql express i can use 'except' but it is not working in sql 2000 so please help me
Thanks in advance
I have a question,
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
I want top records from 11 to 20, in sql 2005
this is nandu,So nice of giving information regarding queries in different platforms.I have a doubt that i want to repeat the same record evenly as 2nd,4th,6th etc. How to write query in oracle .
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.
how can i get the highest three records in employee table on the behalf of salary field
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'column'
Please tell me what to do....
get the records from 11 to 20 rows
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..
example: sum(amount) return highest 200 values. anyhelp?
select top <n> *
from <tablename>
where <id> not in(
select top <k> <id>
from <tablename>
)
for all n = no.of records u need to fetch at a time.
and k = multiples of n(eg. n=5; k=0,5,10,15,....)
Some people said I should use "order by" in that query, I tried, but the result as usual. Why?Any help would be appreciate it.
SELECT TOP 10 column FROM table
And it is best than
SET rowcount 10
because rowcount is a param of the connection, and have to reconfigure the connection to set rowcount = 0 for the following queries, , if you do not want to have unexpected results.
Country and Value in the same table. I need to run a query that can return top 10 from Country A and top 20 from Country B, and top 100 from country C.
Can someone help? Thanks in advance
SELECT column FROM table
LIMIT 10 OFFSET 20
In Firebird 2 (released a long time ago) and newer, it's
SELECT column FROM table
ROWS 20 TO 30
answer: in sql
select * from (select rownum r,emp.*from emp) where r not between 1 and 10;
SELECT * FROM (
SELECT * FROM table_name
ORDER BY primary_key_column
)
WHERE ROWNUM <= 10;
e.g.
SELECT column FROM table
LIMIT 10,20