This is a query to select every nth record in a table. If there are 100 records in a table and you want to select...
every third record, then this is the query to use, although it is possible to achieve the same using PL/SQL. It has been tested on Oracle 8i Enterprise Edition.
select obj_name from (select object_name obj_name, rownum rnum from user_objects) where mod(rnum,3)=0;
Geoff H. writes: That is a very clever use of MOD. Simple is best, as always.
Ashok C. writes: This query may bring up the nth record, but everytime you run the script it will not be consistent. The rownum is assigned in the order the data is fetched from the database. The order of data fetching from the database is not guaranteed.
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.