Solved

oracle query question

Posted on 2011-03-09
3
401 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 77

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

820 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