Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Check for existence of a record

Anil KuppaOct 17 2013 — edited Oct 18 2013

I have a table XX_TEMP. Let us say the columns are inventory_item, organization_id and description

The values are:

Inventory_itemOrganizationDescription
200m1Sample
200m2Not Sample
400

m4

check
700m5Test

I just want to check the existence of an item in the table, I have written two queries and would like to know which one is better in terms of performance:

Q1:

select count(1) from xx_temp where inventory_item=200 and rownum=1;

Q2:

select count(1) from dual where exists (select 1 from xx_temp where inventory_item=200);

Both Q1 and Q2 return the same result. In fact, I was surprised with the result from Q1 as I expected that the rownum would be evaluated after the where condition. I expected Q1 to return 2

I thought that the below query:

select count(1) from xx_temp where inventory_item=200;

and Q1 would return the same result as rownum would be evaluated at end. In effect, I've 2 questions:

1. Isn't rownum calculated at the end?

2. What is the best way in terms of performance to check for an existence of record?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 15 2013
Added on Oct 17 2013
1 comment
314 views