In this post I want to show some example of using a new feature in 12c for selecting the first X number of records from the results set of a query.
See the bottom of this post for the background and some of the reasons for this post.
Before we had the 12c Database if we only wanted to see a subset or the initial set of records from the results of a query we could add something like the following to our query
...
AND ROWNUM <= 5;
The could use the pseudo column ROWNUM to restrict the number of records that would be displayed. This was particularly useful when the results many 10s, 100s, or millions of records. It allowed us to quickly see a subset and to see if the results where what we expected.
In my book (Predictive Analytics Using Oracle Data Miner) I had lots of examples of using ROWNUM.
What I wasn't aware of when I was writing my book was that there was a new way of doing this in 12c. We now have something like the following:
...
FETCH FIRST x ROWS ONLY;
There is an example:
SELECT * FROM mining_data_build_v
FETCH FIRST 10 ROWS ONLY;
There are a number of different ways you can use the row limiting feature. Here is the syntax for it:
[ OFFSET offset { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ { rowcount | percent PERCENT } ]
{ ROW | ROWS } { ONLY | WITH TIES } ]
In most cases you will probably use the number of rows. But there many be cases where you might what to use the PERCENT. In previous versions of the database you would have used SAMPLE to bring back a certain percentage of records.
select CUST_GENDER from mining_data_build_v
FETCH FIRST 2 PERCENT ROWS ONLY;
This will set the first 2 percent of the records.
You can also decide from what point in the result set you want the records to be displayed from. In the previous examples above the results displayed will befing with the first records. In the following example the results set will be processed to record 60 and then the first 5 records will be selected and displayed. This will be records 61, 62, 63, 64 and 65. So the first record processed will be the OFFSET record + 1.
select CUST_GENDER from mining_data_build_v
OFFSET 60 ROWS FETCH FIRST 5 ROWS ONLY;
Similar to the PERCENT example above you can use the OFFSET value, for example.
select CUST_GENDER from mining_data_build_v
OFFSET 60 ROWS FETCH FIRST 2 PERCENT ROWS ONLY;
This query will go to records 61 and return the next 2 percent of the records.
The background to this post
There are a number of reasons that I really love attending Oracle User Group conferences. One of the challenges I set myself is to go to presentations on topics that I think I know or know very well. I can list many, many reasons for this but there are 2 main points. The first is that you are getting someone elses perspective on the topic and hence you might learn something new or understand it better. The second is that you might actually learn something new, like some new command, parameter setting or something else like that.
At Oracle Open World recently I attended the EMEA 12 things about 12c set of presentations that Debra Lilly arranged during the User Group Forum on the Sunday. During these session Alex Nuijten gave an overview of some 12c new SQL features. One of these was the command FETCH FIRST x ROWS. This blog post illustrates some of the different ways of using this command.