{"id":230,"date":"2018-02-07T19:35:01","date_gmt":"2018-02-07T19:35:01","guid":{"rendered":"http:\/\/eipsoftware.com\/musings\/?p=230"},"modified":"2021-09-27T00:53:00","modified_gmt":"2021-09-27T00:53:00","slug":"oracle-12c-limit-rows-returned","status":"publish","type":"post","link":"https:\/\/eipsoftware.com\/musings\/oracle-12c-limit-rows-returned\/","title":{"rendered":"Oracle 12c Limit Rows Returned"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Limit Rows Returned<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Previous to Oracle 12c limit the number of rows returned usually meant encapsulating the query as a subquery.&nbsp; It wasn&#8217;t particularly nice looking and was out of alignment with other relational database query languages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL uses the <strong>LIMIT&nbsp;<\/strong>operator and Microsoft T-SQL uses the <strong>TOP<\/strong>&nbsp;operator. In Oracle 12c, Oracle introduced the&nbsp;<strong>FETCH FIRST<\/strong>&nbsp;operator.&nbsp; Let&#8217;s see how it works.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">At the end of a <strong>SELECT<\/strong> statement you can use the <strong>FETCH FIRST<\/strong> operator to limit the total number of rows returned.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:plsql decode:true\">SELECT      eventrequest.eventno    AS event_number\n            ,eventrequest.dateheld\n            ,COUNT(eventplan.planno) AS count_event_plans\nFROM        eventrequest\n            INNER JOIN eventplan\n                ON eventrequest.eventno = eventplan.eventno\nWHERE       eventplan.workdate BETWEEN '01-DEC-2013' AND '31-DEC-2013'\nGROUP BY    eventrequest.eventno\n            ,eventrequest.dateheld     \nFETCH FIRST 5 ROWS ONLY;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively if we want to return the first 5 rows and include any additional rows that the same value in count_event_plans we can use the option&nbsp;<strong>WITH TIES<\/strong>. You need to use the <strong>ORDER BY&nbsp;<\/strong>operator to ensure correct results.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:plsql decode:true\">SELECT      eventrequest.eventno    AS event_number\n            ,eventrequest.dateheld\n            ,COUNT(eventplan.planno) AS count_event_plans\nFROM        eventrequest\n            INNER JOIN eventplan\n                ON eventrequest.eventno = eventplan.eventno\nWHERE       eventplan.workdate BETWEEN '01-DEC-2013' AND '31-DEC-2013'\nGROUP BY    eventrequest.eventno\n            ,eventrequest.dateheld\nORDER BY    1,2     \nFETCH FIRST 5 ROWS ONLY WITH TIES;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another option with the <strong>FETCH<\/strong> operator is return a percentage of rows with&nbsp;<strong>PERCENT<\/strong> option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:plsql decode:true\">SELECT      eventrequest.eventno    AS event_number\n            ,eventrequest.dateheld\n            ,COUNT(eventplan.planno) AS count_event_plans\nFROM        eventrequest\n            INNER JOIN eventplan\n                ON eventrequest.eventno = eventplan.eventno\nWHERE       eventplan.workdate BETWEEN '01-DEC-2013' AND '31-DEC-2013'\nGROUP BY    eventrequest.eventno\n            ,eventrequest.dateheld     \nFETCH FIRST 10 PERCENT ONLY;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly we may want to paginate the results or start 25 rows from the start of the query results. A key note here is that you need to change from <strong>FETCH FIRST<\/strong> to <strong>FETCH NEXT<\/strong>.&nbsp;&nbsp;You need to use the <strong>ORDER BY&nbsp;<\/strong>operator to ensure correct results.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:plsql decode:true\">SELECT      eventrequest.eventno    AS event_number\n            ,eventrequest.dateheld\n            ,COUNT(eventplan.planno) AS count_event_plans\nFROM        eventrequest\n            INNER JOIN eventplan\n                ON eventrequest.eventno = eventplan.eventno\nWHERE       eventplan.workdate BETWEEN '01-DEC-2013' AND '31-DEC-2013'\nGROUP BY    eventrequest.eventno\n            ,eventrequest.dateheld \nORDER BY    1,2    \nOFFSET 25 ROWS FETCH NEXT 25 ROWS ONLY;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you found the above informative, let me know in the comments below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"mailto:michael.data@eipsoftware.com\">\u2014 michael.data@eipsoftware.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Limit Rows Returned Previous to Oracle 12c limit the number of rows returned usually meant encapsulating the query as a subquery.&nbsp; It wasn&#8217;t particularly nice looking and was out of alignment with other relational database query languages. MySQL uses the LIMIT&nbsp;operator and Microsoft T-SQL uses the TOP&nbsp;operator. In Oracle 12c, Oracle introduced the&nbsp;FETCH FIRST&nbsp;operator.&nbsp; Let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,44,6],"tags":[49,14,30,48],"series":[],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-code","category-oracle-pl-sql","category-sql","tag-oracle","tag-sql","tag-code","tag-pl-sql"],"_links":{"self":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/comments?post=230"}],"version-history":[{"count":8,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/230\/revisions\/356"}],"wp:attachment":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/tags?post=230"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/series?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}