Today I Learned

hashrocket A Hashrocket project

Limit Rows Returned in Oracle SQL

Postgres has a lovely LIMIT clause that allows you to limit the number of returned rows.

SELECT *
FROM blogs
LIMIT 10;

Oracle SQL does not have a LIMIT clause (one of the many reasons why FOSS is better 😉) Instead, it has FETCH which will do the same thing, albeit more verbosely.

SELECT *
FROM blogs
FETCH FIRST 10 ROWS ONLY;

FETCH is available in Oracle 12c and above. On older Oracle versions, you can use the ROWNUM pseudo column. Keep in mind it's 1-based indexing.

SELECT *
FROM blogs
where ROWNUM <= 10
See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.