Home > Ask the Oracle Database / Applications Experts > Oracle database design and architecture Questions & Answers > Ordering of rows or columns in a relational table
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Ordering of rows or columns in a relational table

Brian Peasland EXPERT RESPONSE FROM: Brian Peasland

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 03 September 2001
How can we retrieve the 10th record from a table through the SQL prompt? When I write "select rownum from table" the table gives me rownum but when I write "select * from table where rownum=10" it says "no rows selected". Can you please guide me in solving this problem?

>

According to Relational Database theory initially proposed by Dr. Codd, there is no ordering to rows or columns in a relational table. This means that what is the 10th record this time is not guaranteed to be the 10th record next time. But humans don't like to think that way. So this question comes up all the time.

In order to understand the solution, we need to understand the process a little. Let's suppose that I issue the following query:

ORA9I SQL> select rownum,owner,object_name from objects
  2  where rownum < 10;

    ROWNUM OWNER      OBJECT_NAME
---------- ---------- ------------------------------
         1 SYS        ACCESS$
         2 SYS        AGGXMLIMP
         3 SYS        AGGXMLIMP
         4 SYS        AGGXMLINPUTTYPE
         5 SYS        ALL_ALL_TABLES
         6 SYS        ALL_ARGUMENTS
         7 SYS        ALL_ASSOCIATIONS
         8 SYS        ALL_AUDIT_POLICIES
         9 SYS        ALL_BASE_TABLE_MVIEWS

9 rows selected.

This gives me the first 9 rows of the table. But what if I change the query slightly? For example:

ORA9I SQL> select rownum,owner,object_name from objects
  2  where owner='SYSTEM' and rownum < 10;

    ROWNUM OWNER      OBJECT_NAME
---------- ---------- ------------------------------
         1 SYSTEM     AQ$DEF$_AQCALL
         2 SYSTEM     AQ$DEF$_AQERROR
         3 SYSTEM     AQ$_DEF$_AQCALL_E
         4 SYSTEM     AQ$_DEF$_AQERROR_E
         5 SYSTEM     AQ$_INTERNET_AGENTS
         6 SYSTEM     AQ$_INTERNET_AGENT_PRIVS
         7 SYSTEM     AQ$_QUEUES
         8 SYSTEM     AQ$_QUEUES_CHECK
         9 SYSTEM     AQ$_QUEUES_PRIMARY

9 rows selected.

How can an object owned by SYSTEM have ROWNUM=1 when I've already shown that the first row in the table is an object owned by SYS?!?! That's because the ROWNUM "psuedo-field" is only assigned after the result set of the query is known. In the first example, I returned *all* rows of the table. The WHERE ROWNUM < 10 clause limited my output to the first 9 rows out of all rows of the table. But look at the second query closely. It is asking for only those rows where the owner is SYSTEM. Now out of that result set, return the first 9 rows. So ROWNUM is a psuedo column that is only defined after the result set is determined.

Now to answer your question... How do you return a specific row? Let's say I want to return the 2nd row out of this table:

ORA9I SQL> select * from users;

USERNAME      USER_ID CREATED
---------- ---------- ---------
OUTLN              11 27-JUN-01
DBSNMP             17 27-JUN-01
PEASLAND           18 24-AUG-01

I have to issue a query like the following:

ORA9I SQL> select username,user_id,created
  2  from (select rownum r,username,user_id,created from users)
  3* where r=2;

USERNAME      USER_ID CREATED
---------- ---------- ---------
DBSNMP             17 27-JUN-01

This gets the rownum from the table first, aliases it, and then uses that result in the where clause.

For More Information

  • The Best Oracle Web Links: tips, tutorials, scripts, and more.
  • Have an Oracle or SQL tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle database design and architecture
Why am I receiving Oracle memory allocation errors?
How to join two tables with unique keys in Oracle
How does the Oracle LGWR write to online redo log files?
Can I check an Oracle instance without logging into the Oracle server?
How to use V$SEGMENT_STATISTICS to find the most accessed Oracle table
Can I install an Oracle client on Windows 7?
How to use the Oracle Database Upgrade Assistant (DBUA)
Can I specify Oracle column order in my database table?
Can I have a single Oracle 11g RAC instance across multiple databases?
How to use the Oracle export utility to duplicate database structure

Oracle database design and architecture
Why am I receiving Oracle memory allocation errors?
How to join two tables with unique keys in Oracle
Can I check an Oracle instance without logging into the Oracle server?
How does the Oracle LGWR write to online redo log files?
How to determine your SQL database through needs analysis
Breaking down the contenders in the SQL database market
The MySQL open source database in the enterprise
Diving deeper into the SQL database features
What managers should consider when starting a database scaling project
How to use V$SEGMENT_STATISTICS to find the most accessed Oracle table

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
E. F. Codd  (SearchOracle.com)
extent  (SearchOracle.com)
flexfield  (SearchOracle.com)
foreign key  (SearchOracle.com)
multidimensional database  (SearchOracle.com)
object-oriented database management system  (SearchOracle.com)
quad tree  (SearchOracle.com)
relational online analytical processing  (SearchOracle.com)
row  (SearchOracle.com)
splay tree  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts