Home > Ask the Oracle Experts > 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?

>
EXPERT RESPONSE

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
Database Design and Architecture
Should we worry about Oracle memory leaks on Windows?
How to disable Oracle constraints and triggers during an import in 10g
How to delete a partition on the F: drive with Oracle on Windows
What could be causing an SQL count 0 with a successful Oracle import?
What is a staging table, and how is the staging concept used in Oracle?
Tips for how to size an Oracle server
Oracle error 942 when exporting a schema
Do DDL statements roll back, and why?
Increasing Oracle storage capacity by moving to a new storage with minimum downtime
Using Undo Tablespaces vs. rollback segments in Oracle 9i, 10g

Oracle database design
Oracle database in the cloud floated at Harvard lab
Oracle OpenWorld 2008 Special Report
Weighing remote database administration pros and cons takes care
Oracle Database 11g makes waves at Burlington Coat Factory
How to create a database link in Oracle
Data modeling tools no substitute for hard work
How do I do that in Oracle?
The Oracle Database user's guide to Oracle OpenWorld 2007
Oracle OpenWorld 2007 Special Report
How many redo log files?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
extent  (SearchOracle.com)
field  (SearchOracle.com)
flexfield  (SearchOracle.com)
foreign key  (SearchOracle.com)
quad tree  (SearchOracle.com)
record  (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

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

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




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