Solved

oracle query question

Posted on 2011-03-09
3
398 Views
Last Modified: 2012-05-11
experts.. I have a query in one of the packages here at work and want to know what it means

The query is
                         (SELECT 'Y'
                            FROM table_a a, table_b b
                           WHERE a.id= b.id
                                 AND a.del_ind = 'N'
                                 AND 0 =
                                        (SELECT COUNT (1)
                                           FROM table_c c
                                          WHERE a.SRC_ID =
                                                   c.SRC_ID
                                                AND c.PRD_ID =
                                                       ANY (SELECT prd_id
                                                              FROM product p
                                                             WHERE p.PRD_ID IN
                                                                      ('FSCC1',
                                                                       'FSCC2')))
I want to know what 0= means in SQL and where can such queries be used
0
Comment
Question by:chickanna
3 Comments
 
LVL 3

Accepted Solution

by:
CarlsbergFTW earned 175 total points
ID: 35083561
0 stands for the count that is done within the sub-query , this means that if COUNT (1) returns 0 then the condition is met. else result is ignored.
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 175 total points
ID: 35083565
This is just checking the returned count from the sub select.  The outer query will only return rows when the inner select returns 0 rows.

These are typically called inline views.  You can use them just about anywhere.

for example:
select 'Hello', (select 'World' from dual) as col1 from dual;
0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35084010
When you say count(*) = 0, it means you are checking for non existance of records with specifying conditions.

So, this is equivallant to NOT EXISTS clause of Oracle...

Please look at http://www.techonthenet.com/sql/exists.php for more information on exists/not exists

So for 0 and >0 you may use NON EXISTS and EXISTs respectively.

But suppose you need to figure out that there should be exactly 5 matching records returned by such subquery, you have to use Count(*) = 5

One very generic example of this is

suppose you want to find out the fifth highest paid saialry... you use this

SELECT DISTINCT (a.sal) FROM EMP A WHERE 5 = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);

Thanks,
Jaimin Soni
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now