Tuesday, July 19, 2011

SQL: Limiting the Number of Rows Returned

SQL Cookbook - O'Reilly Media

Chapter 1 Retrieving Records: Limiting the Number of Rows Returned.


Problem

You want to limit the number of rows returned in your query. You are not concerned with the order; any n rows will do.


Solution

Use the built-in function provided by your database to control the number of rows returned.


DB2



MySQL and PostgreSQL

Do the same thing in MySQL and PostgreSQL using LIMIT:
select * from emp limit 5
Oracle



SQL Server


Discussion

No comments: