Exists clause in plsql query

HI I see that by the use of exists clause while joining
1) oralce provides a better execution plan ... and 2) it eliminates duplicates automatically
but my question is how to access the columns of the inner tables in the select clause ?
EG:
select a.sno, b.course_name
from a,b
where a.cid=b.cid
----------------------in the below query How do i access b.course_name in the select clause? similarly even if it contains more tables nested, i should be able to access the column of the inner tables from the select clause . How to do this . hope i am clear
select a.sno from a where exists ( select 1 from b where b.cid=a.cid)

You cannot access columns from tables in an EXISTS (or NOT EXISTS) clause outside of that clause. You can only select data from tables (and other objects) that you are actually selecting from.
Justin

Similar Messages

  • Where Exist clause to improve query performance

    select * from emp
    where emp_code in (select emp_code from emp_acct)
    it is said tht its always better to use where exist clause instead of IN.
    I hav written the same query using where Exist,
    select * from emp e
    where exists (select null from emp_acct ea where e.emp_code = ea.emp_acct)
    but both these queries are sharing the same cost.
    is there is any other way to use exist to decrease cost of the query.
    ---Piyush

    You can't compare the cost of two different queries.
    You can't relate cost to the running time of the query.
    Having said that, why is this not just a join?
    select e.*
    from emp e
    , emp_acct ea
    where e.emp_code = ea.emp_codecheers,
    Anthony

  • Exists clause in query causes too much time to get back results

    We Are having the long query as follows which is taking so much time to respond.Is there any way to optimize the query.We see that there is exists clause in query which is taking long time .if it is so please suggest appropriate solution to remove exists clause.
    SELECT
    DISTINCT t0.JDOID,
    t0.JDOCLASS,
    t0.JDOVERSION,
    t0.ACTIVITY,
    t0.ADMINSTATE,
    t0.CREATEDDATE,
    t0.CREATEDUSER,
    t0.DESCRIPTION,
    t0.ENDDATE,
    t0.ID,
    t0.LASTMODIFIEDDATE,
    t0.LASTMODIFIEDUSER,
    t0.NAME,
    t0.NOSPEC,
    t0.OBJECTSTATE,
    t0.OWNER,
    t0.PARTITION,
    t0.PERMISSIONS,
    t0.SPECIFICATION,
    t0.STARTDATE
    FROM
    SERVINV.RESOURCESPECIFICATION t2,
    SERVINV.SPECIFICATION t3,
    SERVINV.TELEPHONENUMBER t0,
    SERVINV.TELEPHONENUMBERSPECIFICATION t1,
    SERVINV.TN_CHAR t4
    WHERE
    t3.NAME = 'usTelephoneNumber'
    AND
    t0.ID LIKE '0010210%'
    OR t0.ID LIKE '0010370%'
    OR t0.ID LIKE '0010690%'
    OR t0.ID LIKE '0010090%'
    OR t0.ID LIKE '0010610%'
    OR t0.ID LIKE '0010570%'
    OR t0.ID LIKE '0010330%'
    OR t0.ID LIKE '0010130%'
    OR t0.ID LIKE '0010410%'
    OR t0.ID LIKE '0010650%'
    OR t0.ID LIKE '0010730%'
    OR t0.ID LIKE '0010050%'
    OR t0.ID LIKE '0010450%'
    OR t0.ID LIKE '0010490%'
    OR t0.ID LIKE '0010530%'
    OR t0.ID LIKE '0010170%'
    OR t0.ID LIKE '0010290%'
    OR t0.ID LIKE '0010030%'
    OR t0.ID LIKE '0010250%'
    OR t0.ID LIKE '0010770%'
    AND t4.NAME = 'tnType'
    AND t4.VALUE = 'OWNED'
    AND NOT EXISTS (
    SELECT
    t5.JDOID
    FROM
    SERVINV.TNCONSUMER t5
    WHERE
    t5.TELEPHONENUMBER = t0.JDOID
    AND
    t5.ADMINSTATE IS
    NULL
    OR t5.ADMINSTATE <> 'UNASSIGNED'
    AND
    t0.OBJECTSTATE = 'ACTIVE'
    OR t0.OBJECTSTATE = 'INACTIVE'
    OR t0.OBJECTSTATE IS
    NULL
    AND t0.JDOCLASS = 'com.metasolv.impl.entity.TelephoneNumberDAO'
    AND t0.SPECIFICATION = t1.JDOID
    AND t0.JDOID = t4.TELEPHONENUMBER
    AND t1.JDOID = t2.JDOID
    AND t2.JDOID = t3.JDOID
    ORDER BY
    t0.ID ASC;
    Unable to post xplain plan as it is huge and exceeding 30000 characters.
    Pandu

    Hi Pandu,
    try something like this and check if it works:
    replace the OR conditions with substr function:
    AND substr(t0.ID,1,7) in
    ('0010210',
    '0010370',
    '0010690',
    '0010090',
    '0010610',
    '0010570',
    '0010330',
    '0010130',
    '0010410',
    '0010650',
    '0010730',
    '0010050',
    '0010450',
    '0010490',
    '0010530',
    '0010170',
    '0010290',
    '0010030',
    '0010250',
    '0010770')confirm if this reduces the time...will check further after your confirmation...
    Regards
    Imran

  • Not able to migrate a query with not exist clause

    Hi all,
    I'm using Toplink 10.1.3 and I am trying to rewrite the following query with Expression Framework:
    select distinct r.e_ogg_oper_k_oggetto
    FROM regola_accettazione_oper_banc r, oggetto_operazione_bancaria o
    where not exists (
    select 1
    FROM limitaz_ogg_tipo_oper_banc l
    where nvl(l.d_fine_validita,trunc(sysdate)+1)>trunc(sysdate)
    and l.d_inizio_validita <= trunc(sysdate)
    and l.e_tpodv_k_tipo_operazione=:appoggio.tipo_operaz
    and l.e_ogg_oper_k_oggetto=r.e_ogg_oper_k_oggetto
    and l.e_uni_oper_k_unita_oper_util= :appoggio.e_uni_oper_k_unita_oper_esegui )
    and r.e_oper_ban_k_operaz_bancaria=:appoggio.form
    and r.e_ogg_oper_k_oggetto=o.k_oggetto
    and o.e_ogg_oper_k_oggetto is null
    and o.k_oggetto != nvl(:appoggio.oggetto_autom,,'0')
    and o.k_oggetto like substr(:appoggio.oggetto,1,2)||'%'
    and r.d_inizio_validita <= :appoggio.d_contab
    and to_date(nvl(to_char(r.d_fine_validita,DD/MM/YYYY'),31/12/3999'),'DD/MM/YYYY')> :appoggio.d_contab
    and o.f_natura_oggetto in ('G','P') and r.f_oggetto_automatizzato!='S'
    I'm not able to "attach" the not exist clause to the rest of query.
    How can I do it?
    Thank you very much.

    Not exists can be used in an expression through using a ReportQuery sub-query.
    i.e.
    ExpressionBuilder outerBuilder = new ExpressionBuilder();
    ReadAllQuery outerQuery = new ReadAllQuery(Employee.class, outerBuilder);
    ExpressionBuilder subBuilder = new ExpressionBuilder();
    ReportQuery subQuery = new ReportQuery(Address.class, subBuilder);
    subQuery.addAttribute("id");
    subQuery.setSelectionCriteria(
    subBuilder.get("city").equal(outerBuilder.get("address").get("city")
    .and(subBuilder.notEqual(outerBuilder.get("address")))));
    outerQuery.setSelectionCriteria(
    outerBuilder.notExists(subQuery));
    List results = (List) session.executeQuery(outerQuery);
    Refer to the documentation section on sub-queries for more information.
    I would suggest simlpifying the where clause until you get the sub-query working to start.
    You can also always use a custom SQL query in TopLink.

  • Spatial query inside the exists clause return ora-13226

    The following does not work:
    select b.state, b.county from counties b where exists
    (select 's' from states a where a.state = 'New Jersey'
    and mdsys.sdo_relate (b.geom, a.geom, 'mask=INSIDE querytype=WINDOW' ) = 'TRUE');
    ERROR at line 1:
    ORA-13226: interface not supported without a spatial index
    ORA-06512: at "MDSYS.MD", line 1723
    ORA-06512: at "MDSYS.MDERR", line 8
    ORA-06512: at "MDSYS.SDO_3GL", line 302
    ORA-06512: at line 1
    The following does work:
    select b.* from states a,
    counties b where a.state = 'New Jersey'
    and mdsys.sdo_relate (b.geom, a.geom, 'mask=INSIDE querytype=WINDOW') = 'TRUE';
    I found bug 1243095 telling that this is not a bug but a limitation of the spatial operator. It cannot be invoked on a table that is not spatially indexed. In fact, the table is indexed but oracle cannot find the spatial index because table b(counties) is declared outside the EXISTS clause.
    In my case, I use object table. I cannot use the workaround specified above because I should use the DISTINCT clause but I cannot define the MAP and ORDER function (this is a general query).
    I've found another workaround :
    select b.state, b.county from counties b where exists
    (select 's' from states a where a.state = 'New Jersey'
    and mdsys.sdo_relate (a.geom, b.geom, 'mask=CONTAINS querytype=WINDOW') = 'TRUE');
    but sdo_relate still doesn't use the spatial index of table b (even if I specify it explicitely in the operator) and the query is very slow (more than 15 minutes).
    Is there a better workaround ?

    OK but I work in object model.
    And if I don't use the EXISTS clause, I must use the distinct clause.(I used the exists because of that)
    If I will to retrieve all the country that have at least a state beginning with the C letter, I will wrote :
    select c.* from country c, table(c.states) s where s.column_value.name like 'C%';
    (It is a simplified request to express the problem)
    In this case, I must use the distinct clause to select one occurence of each country objet (one country may contains more than one state beginning with C).
    select distinct c.* from country c, table(c.states) s where s.column_value.name like 'C%';
    For that, I must define a MAP or ORDER function for EACH type used in the country object.
    My first question is : I must retrieve all different country objects. Why the request doesn't use the MAP or ORDER function of the country type to distinct them ? Is there another syntax (or a hint) to express that ?
    In this case, it will make an ORA-00932 : incoherent datatype because the type of the nested table column cannot contain map or order method.
    Any suggestion ?
    Thanks in advance.

  • EXISTS Vs IN clause usage in query

    Hi,
    Can anyone please tell me how to rewrite the same query using IN clause instead of EXIST( NOT EXIST) clause.
    I guess use of IN operator instead of EXIST in this case would certainly improve query performance as the selective predicates are in subquery and not in Parent query.
    This is the query :
    SELECT pva.price_dt,
    pva.sec_id,
    pva.vendor_product_id,
    pva.price_typ,
    pva.price,
    pvp.curcy_cd,
    pvp.precedence,
    pvp.exch_cd,
    pvp.price_srce_cd,
    psq1.scrub_queue_id,
    pva.prc_derivation_cd,
    'Alternate Price' as location
    FROM prc_vendor_price pvp,
    prc_vendor_alternate_price pva,
    prc_scrub_queue psq1
    WHERE psq1.sec_id=pva.sec_id
    AND psq1.sec_id=pvp.sec_id
    AND pva.sec_id=pvp.sec_id
    AND psq1.eff_dt=pva.price_dt
    AND psq1.eff_dt=pvp.price_dt
    AND pva.price_dt=pvp.price_dt
    AND pvp.prc_derivation_cd=pva.prc_derivation_cd
    AND pva.vendor_product_id=pvp.vendor_product_id
    AND NOT EXISTS (SELECT 1
    FROM prc_vendor_price pvp2
    WHERE pva.sec_id=pvp2.sec_id
    AND pva.price_dt=pvp2.price_dt
    AND pva.price=pvp.price
    AND pva.price_typ=pvp2.price_typ
    AND pva.prc_derivation_cd=pvp2.prc_derivation_cd
    AND pva.vendor_product_id=pvp2.vendor_product_id)
    I know how to do it for query with single table but dont know how to do it for multiple tables and joins so please gimme some hint.
    Thanks,
    Aashish

    How about not using NOT IN or NOT EXISTS...
    SELECT pva.price_dt,
           pva.sec_id,
           pva.vendor_product_id,
           pva.price_typ,
           pva.price,
           pvp.curcy_cd,
           pvp.precedence,
           pvp.exch_cd,
           pvp.price_srce_cd,
           psq1.scrub_queue_id,
           pva.prc_derivation_cd,
           'Alternate Price' as location
    FROM prc_vendor_price pvp JOIN prc_vendor_alternate_price pva ON (pva.sec_id=pvp.sec_id
                                                                  AND pva.price_dt=pvp.price_dt
                                                                  AND pva.vendor_product_id=pvp.vendor_product_id
                                                                  AND pvp.prc_derivation_cd=pva.prc_derivation_cd)
                              JOIN prc_scrub_queue psq1 ON (psq1.sec_id=pva.sec_id
                                                        AND psq1.sec_id=pvp.sec_id
                                                        AND psq1.eff_dt=pva.price_dt
                                                        AND psq1.eff_dt=pvp.price_dt)
                   LEFT OUTER JOIN prc_vendor_price pvp2 ON (pva.sec_id=pvp2.sec_id
                                                         AND pva.price_dt=pvp2.price_dt
                                                         AND pva.price=pvp.price
                                                         AND pva.price_typ=pvp2.price_typ
                                                         AND pva.prc_derivation_cd=pvp2.prc_derivation_cd
                                                         AND pva.vendor_product_id=pvp2.vendor_product_id)
    WHERE pvp2.sec_id is null

  • Use of EXIST clause

    Need to seek advice on the use of EXISTS clause.
    What is this statement trying to do? What is the '1' in the inner SELECT statement means?
    DELETE FROM a
    WHERE NOT EXISTS (SELECT 1
    FROM b
    WHERE a.a_id = b.a_id);
    What about these 2 statements doing??
    SELECT * FROM a WHERE EXISTS (SELECT 1 FROM b WHERE a.a_id = b.a_id);
    DELETE FROM a
    WHERE EXISTS (SELECT 1 FROM b WHERE a.a_id = b.a_id);

    have a look at http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:953229842074 for a complete explanation of how an "exists" and an "in" is executed. For "not in" and "not exists", look here: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:442029737684
    greetings
    Freek D
    Freek and Mona are correct in that the 1 is just a place holder, because you need to SELECT something. However, the EXISTS clause actually returns TRUE or FALSE depending on whether the SELECT statement finds a row.
    EXISTS is similar to IN using a SELECT statement. The advantage of EXISTS, particularly when you can use a correlated sub-query, is that it stops looking as soon as it finds a single row that matches the criteria in the SELECT statement, where an IN clause will return all the rows that match, including duplicates

  • Use of EXISTS clause in Interface

    I want to build an interface from one table but using an exist clause to check the data change in the same table.
    In SQL statement it can be expressed as follows
      SELECT PB.PROJECT_KEY, COUNT (DISTINCT DW_PROJECT_BUILDING_KEY)
        FROM DW_PROJECT_BUILDING PB
       WHERE     EXISTS
                    (SELECT 1
                       FROM DW_PROJECT_BUILDING PB1
                      WHERE     PB1.PROJECT_KEY = PB.PROJECT_KEY
                            AND PB1.RECORD_STATUS_ID = 1)
             AND PB.RECORD_STATUS_ID = 1
             AND TRUNC (PB.LOAD_DT) >= '03-OCT-2013'
    GROUP BY PB.PROJECT_KEY;
    How can I build this SQL with exists clause in the interface ?
    Thanks

    Create the interface with DW_PROJECT_BUILDING table and give this table a name PB.
    Now put one filter on the table with the conditions that you have just mentioned in the query above, with the exist statement.
    I assume data from this table is targeted into one more aggregated table: here is the definition for the table:
    CREATE TABLE PROJECT_BUILDING_AGGR
    (PROJ_BUILD_KEY VARCHAR2(20),
    PROJECT_KEY varchar2(20));
    perform mapping like :
    PROJECT_KEY=PB.PROJECT_KEY
    PROJ_BUILD_KEY=count(distinct PB.DW_PROJECT_BUILDING_KEY)
    Select IKM as IKM SQL control append.
    FLOW control as false.
    run the interface, it is giving something like this for me, I hope this is according to your requirement:
    insert into
    ORACLE_SOURCE.PROJECT_BUILDING_AGGR
    PROJ_BUILD_KEY,
    PROJECT_KEY
    select
        PROJ_BUILD_KEY,
    PROJECT_KEY  
    FROM (
    select
    count(distinct PB.DW_PROJECT_BUILDING_KEY) PROJ_BUILD_KEY,
    PB.PROJECT_KEY PROJECT_KEY
    from
    ORACLE_SOURCE.DW_PROJECT_BUILDING   PB
    where
    (1=1)
    And (EXISTS
                    (SELECT 1
                       FROM DW_PROJECT_BUILDING PB1
                      WHERE     PB1.PROJECT_KEY = PB.PROJECT_KEY
                            AND PB1.RECORD_STATUS_ID = 1)
             AND PB.RECORD_STATUS_ID = 1
             AND TRUNC (PB.LOAD_DT) >= '03-OCT-2013')
    Group By PB.PROJECT_KEY
    )    ODI_GET_FROM

  • In Oracle, Can i use if exits clause in a query?

    In Oracle, Can i use if exits clause in a query?
    For example, "Drop table if exists tablename"
    Is the above command valid in oracle?
    If not then is there any equivalent for if exists clause?

    Here is the SP code code that might help you to Drop a table if it exisit, whith out throwing an error if the table is not present.
    create or replace PROCEDURE DROP_TABLE(TabName in Varchar2)
    IS
    temp number:=0;
    tes VARCHAR2 (200) := TabName;
    drp_stmt VARCHAR2 (200):=null;
    BEGIN
    select count(*) into temp from user_tables where TABLE_NAME = tes ;
    if temp =1 then
    drp_stmt := 'Drop Table '||tes;
    EXECUTE IMMEDIATE drp_stmt;
    end if;
    EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    END DROP_TABLE;
    Call this SP in your Scripts by : CALL DROP_TABLE ('<Table Name>')
    Hope this Helps!
    Regards,
    Kaarthiik

  • Execution of subquery of IN and EXISTS clause.

    Hi Friends,
    Suppose we have following two tables:
    emp
    empno number
    ename varchar2(100)
    deptno number
    salary number
    dept
    deptno number
    location varchar2(100)
    deptname varchar2(100)
    status varchar2(100)
    Where dept is the master table for emp.
    Following query is fine to me:
    SELECT empno, ename
    FROM emp,dept
    WHERE emp.deptno = dept.deptno
    AND emp.salary &gt;=5000
    AND dept.status = 'ACTIVE';
    But I want to understand the behaviour of inline query (Used with IN and EXISTS clause) for which I have used this tables as an example (Just as Demo).
    1)
    Suppose we rewrite the above query as following:
    SELECT empno, ename
    FROM emp
    WHERE emp.salary &gt;=5000
    AND deptno in (SELECT deptno FROM dept where status = 'ACTIVE')
    Question: as shown in above query, suppose in our where clause, we have a condition with IN construct whose subquery is independent (it is not using any column of master query's resultset.). Then, will that query be executed only once or will it be executed for N number of times (N= number of records in emp table)
    In other words, how may times the subquery of IN clause as in above query be executed by complier to prepared the subquery's resultset?
    2)
    Suppose the we use the EXISTS clause (or NOT EXISTS clause) with subquery where, the subquery uses the field of master query in its where clause.
    SELECT E.empno, E.ename
    FROM emp E
    WHERE E.salary &gt;=5000
    AND EXISTS (SELECT 'X' FROM dept D where status = 'ACTIVE' AND D.deptno = E.deptno)
    Here also, I got same confusion. For how many times the subquery for EXISTS will be executed by oracle. For one time or for N number of times (I think, it will be N number of times).
    3)
    I know we can't define any fix thumbrule and its highly depends on requirement and other factors, but in general, Suppose our main query is on heavily loaded large transaction table and need to check existance of record in some less loaded and somewhat smaller transaction table, than which way will be better from performance point of view from above three. (1. Use of JOIN, 2. Use of IN, 3. Use of EXISTS)
    Please help me get solutions to these confusions..
    Thanks and Regards,
    Dipali..

    Dipali,
    First, I posted the links with my name only, I don;t know how did you pick another handle for addressing it?Never mind that.
    >
    Now another confusion I got.. I read that even if we used EXISTS and , CBO feels (from statistics and all his analysis) that using IN would be more efficient, than it will rewrite the query. My confusion is that, If CBO is smart enough to rewrite the query in its most efficient form, Is there any scope/need for a Developer/DBA to do SQL/Query tuning? Does this means that now , developer need not to work hard to write query in best menner, instade just what he needs to do is to write the query which resluts the data required by him..? Does this now mean that now no eperts are required for SQL tuning?
    >
    Where did you read that?Its good to see the reference which says this.I haven't come across any such thing where CBO will rewrite the query like this. Have a look at the following query.What we want to do is to get the list of all teh departments which have atleast one employee working in it.So how would be we write this query? Theremay be many ways.One,out of them is to use distinct.Let's see how it works,
    SQL> select * from V$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
    PL/SQL Release 11.1.0.6.0 - Production
    CORE    11.1.0.6.0      Production
    TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
    NLSRTL Version 11.1.0.6.0 - Production
    SQL> set timing on
    SQL> set autot trace exp
    SQL> SELECT distinct  D.deptno, D.dname
      2        FROM     scott.dept D,scott.emp E
      3  where e.deptno=d.deptno
      4  order by d.deptno;
    Elapsed: 00:00:00.12
    Execution Plan
    Plan hash value: 925733878
    | Id  | Operation                     | Name    | Rows  | Bytes | Cost (%CPU)| T
    ime     |
    |   0 | SELECT STATEMENT              |         |     9 |   144 |     7  (29)| 0
    0:00:01 |
    |   1 |  SORT UNIQUE                  |         |     9 |   144 |     7  (29)| 0
    0:00:01 |
    |   2 |   MERGE JOIN                  |         |    14 |   224 |     6  (17)| 0
    0:00:01 |
    |   3 |    TABLE ACCESS BY INDEX ROWID| DEPT    |     4 |    52 |     2   (0)| 0
    0:00:01 |
    |   4 |     INDEX FULL SCAN           | PK_DEPT |     4 |       |     1   (0)| 0
    0:00:01 |
    |*  5 |    SORT JOIN                  |         |    14 |    42 |     4  (25)| 0
    0:00:01 |
    |   6 |     TABLE ACCESS FULL         | EMP     |    14 |    42 |     3   (0)| 0
    0:00:01 |
    Predicate Information (identified by operation id):
       5 - access("E"."DEPTNO"="D"."DEPTNO")
           filter("E"."DEPTNO"="D"."DEPTNO")
    SQL>
    SQL> SELECT distinct  D.deptno, D.dname
      2        FROM     scott.dept D,scott.emp E
      3  where e.deptno=d.deptno
      4  order by d.deptno;
        DEPTNO DNAME
            10 ACCOUNTING
            20 RESEARCH
            30 SALES
    Elapsed: 00:00:00.04
    SQL>So CBO did what we asked it do so.It made a full sort merge join.Now there is nothing wrong in it.There is no intelligence added by CBO to it.So now what, the query looks okay isn't it.If the answer is yes than let's finish the talk here.If no than we proceed further.
    We deliberately used the term "atleast" here.This would govern that we are not looking for entirely matching both the sources, emp and dept.Any matching result should solve our query's result.So , with "our knowledge" , we know that Exist can do that.Let's write teh query by it and see,
    SQL> SELECT   D.deptno, D.dname
      2        FROM     scott.dept D
      3          WHERE    EXISTS
      4                 (SELECT 1
      5                  FROM   scott.emp E
      6                  WHERE  E.deptno = D.deptno)
      7        ORDER BY D.deptno;
        DEPTNO DNAME
            10 ACCOUNTING
            20 RESEARCH
            30 SALES
    Elapsed: 00:00:00.00
    SQL>Wow, that's same but there is a small difference in the timing.Note that I did run the query several times to elliminate the physical reads and recursive calls to effect the demo. So its the same result, let's see the plan.
    SQL> SELECT   D.deptno, D.dname
      2        FROM     scott.dept D
      3          WHERE    EXISTS
      4                 (SELECT 1
      5                  FROM   scott.emp E
      6                  WHERE  E.deptno = D.deptno)
      7        ORDER BY D.deptno;
    Elapsed: 00:00:00.00
    Execution Plan
    Plan hash value: 1090737117
    | Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Ti
    me     |
    |   0 | SELECT STATEMENT             |         |     3 |    48 |     6  (17)| 00
    :00:01 |
    |   1 |  MERGE JOIN SEMI             |         |     3 |    48 |     6  (17)| 00
    :00:01 |
    |   2 |   TABLE ACCESS BY INDEX ROWID| DEPT    |     4 |    52 |     2   (0)| 00
    :00:01 |
    |   3 |    INDEX FULL SCAN           | PK_DEPT |     4 |       |     1   (0)| 00
    :00:01 |
    |*  4 |   SORT UNIQUE                |         |    14 |    42 |     4  (25)| 00
    :00:01 |
    |   5 |    TABLE ACCESS FULL         | EMP     |    14 |    42 |     3   (0)| 00
    :00:01 |
    Predicate Information (identified by operation id):
       4 - access("E"."DEPTNO"="D"."DEPTNO")
           filter("E"."DEPTNO"="D"."DEPTNO")Can you see a keyword called Semi here? This means that Oralce did make an equi join but not complete.Compare the bytes/rows returned from this as well as cost with the first query.Can you notice the difference?
    So what do we get from all this?You asked that if CBO becomes so smart, won't we need developers/dbas at that time?The answer is , what one wants to be, a monkey or an astranaut? Confused,read this,
    http://www.method-r.com/downloads/doc_download/6-the-oracle-advisors-from-a-different-perspective-karen-morton
    So it won't matter how much CBO would become intelligent, there will be still limitations to where it can go, what it can do.There will always be a need for a human to look all the automations.Rememember even the most sofisticated system needs some button to be pressed to get it on which is done by a human hand's finger ;-).
    Happy new year!
    HTH
    Aman....

  • IN clause Vs Exist clause

    Which is better to use in select query IN or EXIST clause ??

    Which better in terms of what performance,readability ? Performance is the same  but EXISTS is more readable and easy understand in my opinion.
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Exist Clause

    Helllo All,
    One Order (table oe_order_headers_all ) can have mulitple lines(table oe_order_lines_all) associated with it.
    Both tables are joined with header_id.
    Now I need to run a query for a specific date range. suppose i gave both date parameters as 26-mar-10.
    Then the orders that are shipped on 26th only should come.
    Here let say few lines shipped on 26th and the final lines got shipped on 27th mar
    So if i ran the query for 26th march then we should not get the data.
    If i ran for 27th mar then i should get the data.
    (Only when all lines got shipped then on that date i should get data)
    Below is th sample query. which i wrote(only i need to modify the exist clause here)
    Actually i have problem with Exist Clause.Exist clause should be written to meet the above said condition.
    Select ooh.* from oe_order_headers_all ooh
    where 1=1
    and
    EXISTS (
    ( SELECT 1
    from apps.oe_order_lines l
    where l.header_id = ooh.header_id and
    trunc (l.shipment_date ) >= nvl (:DATE_FROM ,
    trunc (l.shipment_date ) )
    and trunc (l.shipment_date ) <= nvl ( :DATE_TO,
    trunc ( l.shipment_date ) )
    )

    You do not need exists for that:
    select  ooh.*
      from  oe_order_headers_all ooh,
             select  header_id
               from  apps.oe_order_lines
               group by header_id
               having max(shipment_date) < nvl(:DATE_TO,shipment_date) + 1
                  and min(shipment_date) >= nvl(:DATE_FROM,shipment_date)
            ) l
      where l.header_id = ooh.header_id
    /SY.

  • How to link a non-linkable query and a plsql query in the datamodel

    Hi,
    I am creating a matrix group....
    Where in I get my
    1) Column values from a non-linkable query - Period (Jan, Feb....)
    2) Row values from a plsql query (ref cursor). Group is also from this query
    3) Cross product is on the above two queries.
    4) Cell info comes from a plsql query (refcursor).
    I created a group link from the cross product group (3) to the cell info query (4)
    I prepared the layout and executed the report... I am getting redundant data....
    The reason being the (4) and (1) are not linked properly.
    From google, About non-linkable queries column.... I understood that to link two non-linkable queries, we need to create a group link and then add a where clause in the child query referring the parent query column.
    But my case is a non-linkable query (which is a parent ) and a plsql query which is the cell.
    Any help...?
    Thanks in advance.
    KK

    Hello Sam,
    >
    I was wondering if you could have any link or examples to show how to make a form with report and an insert form in the same page, these two forms are related to the same table. Our customer wants a user can add new row to the table in a form and see all of rows created by this user in a report, this report should provide edit link as well. the problem is: whenever I inserted a new row or edit a row or delete a row, and submitted, and return to this page, all of hidden items lost their values, so report is blank, and some display only items also lost their values. Could anyone give me suggestions?
    >
    This will help:
    http://www.grassroots-oracle.com/2011/09/apex-tutorial-form-report-sharing-same.html
    Hope it helps!
    Regards,
    Kiran

  • Suppress "Order By" clause in Answers Query

    Hello,
    Is it possible to Suppress "Order By" clause in Answers Query.
    I'm using a database view as data source. In the view definition, "order by" clause is already specified. Is it possible to get the same order in the OBIEE report??
    I do not want to use Sort Order column in the repository.
    Thanks,
    Girish

    You add a rownumber to your DB view and use that to 'sort' your report.
    regards
    John
    http://obiee101.blogspot.com/

  • How do you use 3 Where Clauses in a query

    Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
    Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
    Order the result by OrderID in ascending order.
    First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
    Second WHERE clause use equals and sub query.
    Third WHERE clause uses equal and company name.
    This is what i have so far but i am pretty confused on how to do this.
    My Code for NorthWind:
    Select OrderID
    From Orders o
    Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
    (Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
    The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
    Select VenderName, InvoiceNumber, InvoiceTotal
    FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
    WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
    ORDER BY VendorName;

    >Where Orders.OrderDate
    > ALL  (Select
    CompanyName
    The comparison operator (>) requires compatible data types.
    DATETIME is not compatible with VARCHAR string for comparison.
    Here is your homework:
    SELECT orderid
    FROM orders o
    WHERE o.orderdate > ALL (SELECT orderdate
    FROM orders
    WHERE shipvia = (SELECT Max(shipvia)
    FROM orders o
    INNER JOIN customers c
    ON c.customerid =
    o.customerid
    WHERE
    c.companyname = 'Bottom-Dollar Markets'));
    11064
    11065
    11066
    11067
    11068
    11069
    11070
    11071
    11072
    11073
    11074
    11075
    11076
    11077
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

Maybe you are looking for