Solved

oracle query question

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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 connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 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

12 Experts available now in Live!

Get 1:1 Help Now