• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

oracle query question

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
chickanna
Asked:
chickanna
2 Solutions
 
CarlsbergFTWCommented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
jaiminpsoniCommented:
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now