Quick Tip: Limit The Number Of Rows Returned From A SQL Query
Read Time:1 Min, 1 Sec
This is pretty basic, but I had to look up the syntax to use for SQL on IBM DB2 so I thought I’d write a quick tip.
Limiting the number of records returned from a query is really helpful when querying a table with millions of records and you expect a large amount of data back. I use it a lot when determining what my ‘where’ statement should look like. My workflow usually goes something like this:
- I know the data is in table F0911 (yeah, it’s a big one)
- I only need records associated with a particular Document Number but I don’t remember what that column is named.
- So, I start with this:
SELECT * FROM PRODDTA.F0911 FETCH FIRST 10 ROW ONLY - That way I get the column names and can continue working on the ‘WHERE’ clause.
Below are examples of ways to limit the number of results in your SQL query:
IBM DB2 Syntax
SELECT * FROM PRODDTA.F0911 FETCH FIRST 10 ROW ONLY;
SQL Server Syntax
SELECT TOP 10 FROM PRODDTA.F0911;
Oracle Syntax
SELECT * FROM PRODDTA.F0911 WHERE ROWNUM <= 10;
Source: IBM Knowledge Center | W3Schools.com
If you have any questions, comments or ideas, please leave them in the comments.
What’s up mates, nice article and nice urging commented here,
I am genuinely enjoying by these.