Using ROWNUM Pseudocolumn in Oracle

Using ROWNUM Pseudocolumn in Oracle

When querying in Oracle, use ROWNUM pseudocolumn to limit the number of returned rows. Pseudocolumns behave like table columns but are not actually stored in tables. (Other pseudocolumns are RowId, Level, etc.).

In Sybase, the equivalent way to restrict returned rows in T-SQL is the following:

 Set Rowcount nWhere 'n' stands for number of rows returned by query

Consider following this example, which would restrict the resulting number of rows to9:

 SELECT firstnameFROM   employeeWHERE  ROWNUM < 10 ;

Here are the results:

 FIRSTNAME------------JohnTimJulieStacyRahulLeenaAmyBillTeri

However, one should be careful when using the Order By clause along withRownum. When Order By is used with Rownum to restrict query results, it works onlyif Ordered By is the primary key of the table. For example, empid is the primary key in this employee table:

 SELECT firstnameFROM   employeeWHERE  ROWNUM < 5 ORDER BY empid;

Here are the results:

 FIRSTNAME------------JohnJulieStacyTim

Now consider the following query, ordered by a non-PK column. The order of its results would be unpredictable. It would depend on how the rows were inserted in table.

 SELECT firstnameFROM   employeeWHERE  ROWNUM< 5 ORDER BY firstname;

Here are the results:

 FIRSTNAME------------TeriJulieStacyBill
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several