Joins subqueries set operators

Hi All,
I am new to oracle SQL, Could any body tell me what is the differences between Joins, subqueries, set operators ? What is the benifits or advantages and dis-advantages comparing the above three.
Thanks in advance
Mahesh Ragineni

Not using that syntax you won't in PL/SQL as it's just wrong.
As you're querying multiple rows you'd have to either loop or collect it in to a collection e.g.
SQL> declare
  2    type aNums is table of number;
  3    vNums aNums;
  4  Begin
  5    select n
  6      bulk collect into vNums
  7    from
  8      (
  9      select 1 as n from dual
10      union
11      select 2 from dual
12      );
13  end;
14  /
PL/SQL procedure successfully completed.
But yes, you can use any SQL statements in SQL that you use in PL/SQL.

Similar Messages

  • SELECTing records from two tables. Set Operators, CASE, DECODE, ...

    Hi all,
    I have two tables:
    CRETE TABLE T1 (T1_COL1 NUMBER, T1_COL2 NUMBER)
    CRETE TABLE T2 (T2_COL1 NUMBER, T2_COL2 NUMBER)
    T1 may or may not have records. T2 always has records. There are two scenarios.
    Scenario 1:
    =======
    SELECT * FROM T1 returns five rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the five rows from T1.
    Scenario 2:
    =======
    SELECT * FROM T1 returns zero rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the 10 rows from T2.
    In other words, if records present in T1, I need them all. If not, I need records from T2.
    There are no common columns (for joins).
    Now need a single query to achive this. I tried set operators, CASE and DECODE. But I'm unable to solve it.
    Please help. Thanks in advance.

    Iniyavan wrote:
    Yes, Justin. I'm sure that this is the way it's modelled. I also find it's tough and odd.Are you sure it's the right way for that data to be modeled? I understand that's the way it is being modeled, but a data model change may be the best option.
    Is there any other way, which is simpler, without using RANK?That's the simplest option I can think of. You could also do something like
    SELECT t1_col1, t1_col2
      FROM (
        SELECT t1_col1, t1_col2, rownum rn
          FROM (
            SELECT t1_col1, t1_col2
              FROM (
                SELECT t1_col1, t1_col2, 1 tbl
                  FROM t1
                UNION ALL
                SELECT t2_col2, t2_col2, 2 tbl
                  FROM t2       
             ORDER BY tbl
    WHERE rn = 1I'm not sure that's any simpler...
    Justin

  • What is use of set operators in OBIEE

    Hi Guys,
    Colud you please tell me what is main use of Set Operators (Intersection,Union,Union all)in OBIEE.
    Is it mandatory to use them in reports?
    Pls tell me.
    Regards,
    siva

    Hi,
    What scenario we are using set operators.
    For Eg.
    Col 1 is having values:
    A
    B
    C
    Col 2 is having values:
    1
    2
    3
    I have to create a new column in answers having values
    A
    B
    C
    1
    2
    3
    solution:
    You can use SET operators in OBIEE and it's really easy to set it in answers section. So create one request and then add other similar request to it by using UNION operator.
    or
    we can use case statement also.
    2. Is it mandatory to use them in reports?
    Normal reports not mandatory.
    Hope it helps.
    Thanks,
    satya

  • Creating measure using set operators in BO Xi 3.0

    Hi All,
    I am trying to create a measure like this in BO XI 3.0 Designer.
    Select count(*) from
    select id from table1 where Name=<Prompt Value>
    INTERSECT
    select id from table1 where Name=<Prompt Value>
    But I don't know how to use set operators in Designer.
    Can someone please help or any pointer would help.
    Thanks,
    Kuldeep
    Edited by: Kuldeep Chitrakar on Apr 2, 2009 4:06 PM

    Create a derived table with same SQL and drag the table into universe pane to create object.

  • Set operators in PLSQL

    Hi All,
    Can we use set operators in PLSQL. I hope we can not use it in PLSQL. But just wanted to check with you all.
    Begin
    select 1 into n from dual
    union
    select 2 into n from dual;
    end;
    Thanks

    Not using that syntax you won't in PL/SQL as it's just wrong.
    As you're querying multiple rows you'd have to either loop or collect it in to a collection e.g.
    SQL> declare
      2    type aNums is table of number;
      3    vNums aNums;
      4  Begin
      5    select n
      6      bulk collect into vNums
      7    from
      8      (
      9      select 1 as n from dual
    10      union
    11      select 2 from dual
    12      );
    13  end;
    14  /
    PL/SQL procedure successfully completed.
    But yes, you can use any SQL statements in SQL that you use in PL/SQL.

  • Set Operators - Clarification needed

    This is my query. I am executing this in HR Schema Oracle 11g.
    SELECT HIRE_DATE, EMPLOYEE_ID, to_char(null) FROM EMPLOYEES WHERE LAST_NAME='Taylor'
    UNION ALL
    SELECT SYSDATE, TO_CHAR(NULL), 1+1 FROM DUALIt gives ORA-01790: expression must have same datatype as corresponding expression error. What data type is mismatching here? I don't seem to understand. HIRE_DATE and SYSDATE - DATE data type. Is something wring with my other two columns? I read that TO_CHAR(NULL) can be used for filling missing values. That's why I tried this way.
    Any guidance, please?

    Hi,
    whenever you use set operators there has to be ont to one datatype correspondance of the columns you define in all the select statements.
    Here the first select statement's employee_id number is conflicting with to_char(null) and to_char(null) is conflicting with 1+1.
    Hence try out this
    SELECT HIREDATE, Empno, to_char(null) FROM EMP WHERE ename='Taylor'
    UNION ALL
    SELECT SYSDATE, 1+1,TO_CHAR(NULL) FROM DUAL
    or use
    SELECT HIRE_DATE, EMPLOYEE_ID, to_NUMBER(null) FROM EMPLOYEE WHERE LAST_NAME='Taylor'
    UNION ALL
    SELECT SYSDATE, TO_NUMBER(NULL), 1+1 FROM DUAL

  • Set operators over ore.frame

    Is there a way to make set operators work with ore.objects without ore.pull?
    EXAMPLE:
    > df1 <- data.frame(ID=c(1:3),A=rep(1,3))
    > df2 <- data.frame(ID=c(2:4),B=rep(1,3))
    > class(df1)
    [1] "data.frame"
    > # Union
    > union(df1$ID,df2$ID)
    [1] 1 2 3 4
    > # Minus
    > setdiff(df1$ID,df2$ID)
    [1] 1
    > #Intersect
    > intersect(df1$ID,df2$ID)
    [1] 2 3
    > #-------------------------------------------------
    > of1 <- ore.push(df1)
    > of2 <- ore.push(df2)
    > class(of1)
    [1] "ore.frame"
    attr(,"package")
    [1] "OREbase"
    > # Union
    > union(of1$ID,of2$ID)
    integer(0)
    > # Minus
    > setdiff(of1$ID,of2$ID)
    integer(0)
    > #Intersect
    > intersect(of1$ID,of2$ID)
    integer(0)
    > #-------------------------------------------------
    > vec1 <- ore.push(df1$ID)
    > vec2 <- ore.push(df2$ID)
    > class(vec1)
    [1] "ore.integer"
    attr(,"package")
    [1] "OREbase"
    > union(vec1,vec2)
    integer(0)

    Many of the commonly used base R functions are overloaded in the Oracle R Enterprise transparency layer to work with ore.frame objects.  These functions are located in the OREbase and OREstats packages.
    For example, to get the union, use merge, as it has been overloaded to operate on ore.frame objects:
    R> find("merge")
    [1] "package:OREbase" "package:base"  
    merge(x = of1, y = df2, all = TRUE)
    R> merge(x = of1, y = of2, all = TRUE)
    ID  A  B
    1|NA  1  1 NA
    2|1   2  1  1
    3|2   3  1  1
    NA|3  4 NA  1
    to subtract:
    R> find("Arith")
    [1] "package:OREbase" "package:methods"
    of1$A - of2$B
    R> of1$A - of2$B
    [1] 0 0 0
    There is no need to pull the data if the function is overloaded to work with ore.frame objects.  More examples can be found in the ORE 1.4 Training Materials: Oracle R Enterprise 1.4 Transparency Layer: http://www.oracle.com/technetwork/database/database-technologies/r/r-enterprise/learnmore/ore-1-4-transparency-layer-215…
    You can view the functions in the OREbase and OREstats packages by typing:
    ls("package:OREbase")
    ls("package:OREstats")
    Sherry

  • Nested Left Outer Join : Data Set

    Hi All
    I am bit confused about data set used by Nested Left outer join.
    Can anyone help me.
    Here is sample data:
    Tables (Name, 3 Column each, total rows and matched rows if any):
         Table 1          
         A     B     C
         Total 20 Rows          
         Table 2          
         A     D     E
         Total 50 Rows and 10 Matching on      2.A = 1.A     
         Table 3          
         D     M     N
         Total 15 Rows and 15 Matching on 3.D = 2.D     
         Table 4          
         M     X     Y
         Total 20 Rows and 10 Matching on 4.M = 3.M     
    Sql
    select *
    From Table 1
    Left Outer Join on Table 2 on
                   2.A = 1.A
    -- Data set 1 will contain 20 Rows (10 matching and 10 non matching)
    Left Outer Join on Table 3 on
                   3.D = 2.D
    -- What will be data set? 20 Rows of Data set 1 or 15 Matching Rows?
    Left Outer Join on Table 4 on
                   4.M = 3.M
    -- What will be data set? X Rows of Data set 2 or 10 Matching Rows?
    Please have a look and clear my understanding.

    SeshuGiri wrote:
    I have two tables defined (below). Emp table has data and there is no data in Emp_Type table yet! Right now it is empty.
    I want to write a query that returns data from both the tables even though there is no data in Emp_type table. I am using left outer join but it returning nothing. Anyone can help?
    select *
    from emp e
    left outer join emp_Type t
    on e.empid = t.empid
    WHERE t.type_id = 1
    and t.end_date is null;
    The join is including all rows from emp, just like you want.
    The WHERE clause is discarding all of those rows. Since all the columns from emp_type (alias t) are NULL, the condition "t.type_id = 1" in the WHERE clause is never true.
    Perhaps you meant to include all those conditions in the join conditions, like this:
    select *
      from emp e
      left outer join emp_Type t
        on e.empid = t.empid
       and t.type_id = 1
       and t.end_date is null;Edited by: Frank Kulash on Jan 30, 2012 3:56 PM

  • SET OPERATORS IN ABAP

    Hi Friends,
    I  want to compare a variable has any of the 25 stored values in internal table or any variable?
    Is there any SET operation possible using ABAP like IN , UNION, NOT IN etc.,
    Thanks in advance,
    Durai.

    Hi,
    Set Operations Using Bit Sequences
    If you have a set of m elements, you can represent any subset as a sequence of bits. If the nth element of the set is present in a subset, the n th bit is set to 1, otherwise, it is set to 0. The universal set therefore contains no zeros.
    In ABAP, you can represent a set of m elements using a field with type X and length of at least m/8. To include a member of the universal set in a set, use the SET BIT statement. To check which members of the universal set are included in a particular set, use the GET BIT statement. You can use bit operations for the following set operations:
    Set operation
    Bit operation
    Intersection
    BIT-AND
    Union
    BIT-OR
    Symmetrical difference
    BIT-XOR
    You can also use the O operator when comparing bit sequences to determine whether a particular set is a subset of another.
    DATA: FRANKFURT(4) TYPE X,
          FRISCO(4)    TYPE X,
          INTERSECT(4) TYPE X,
          UNION(4)     TYPE X,
          BIT          TYPE I.
    DATA: CARRID TYPE SPFLI-CARRID,
          CARRIER LIKE SORTED TABLE OF CARRID
                              WITH UNIQUE KEY TABLE LINE.
    DATA WA TYPE SPFLI.
    SELECT CARRID FROM SCARR INTO TABLE CARRIER.
    SELECT CARRID CITYFROM FROM SPFLI
                           INTO CORRESPONDING FIELDS OF WA.
      WRITE: / WA-CARRID, WA-CITYFROM.
      READ TABLE CARRIER FROM WA-CARRID TRANSPORTING NO FIELDS.
      CASE WA-CITYFROM.
        WHEN 'FRANKFURT'.
          SET BIT SY-TABIX OF FRANKFURT.
        WHEN 'SAN FRANCISCO'.
          SET BIT SY-TABIX OF FRISCO.
      ENDCASE.
    ENDSELECT.
    INTERSECT = FRANKFURT BIT-AND FRISCO.
    UNION     = FRANKFURT BIT-OR  FRISCO.
    SKIP.
    WRITE 'Airlines flying from Frankfurt and San Francisco:'.
    DO 32 TIMES.
      GET BIT SY-INDEX OF INTERSECT INTO BIT.
        IF BIT = 1.
          READ TABLE CARRIER INDEX SY-INDEX INTO CARRID.
          WRITE CARRID.
        ENDIF.
    ENDDO.
    SKIP.
    WRITE 'Airlines flying from Frankfurt or San Francisco:'.
    DO 32 TIMES.
      GET BIT SY-INDEX OF UNION INTO BIT.
        IF BIT = 1.
          READ TABLE CARRIER INDEX SY-INDEX INTO CARRID.
          WRITE CARRID.
        ENDIF.
    ENDDO.
    This produces the following output list:
    AA   NEWYORK
    AA   SANFRANSISCO
    AZ    ROME
    AZ    ROME
    AZ    TOKYO
    AZ    ROME
    DL    NEWYORK
    DL    SANFRANSISCO
    The program uses four hexadecimal fields with length 4 - FRANKFURT, FRISCO, INTERSECT, and UNION. Each of these fields can represent a set of up to 32 elements. The basic set is the set of all airlines from database table SCARR. Each bit of the corresponding bit sequences representes one airline. To provide an index, the external index table CARRIER is created and filled with the airline codes from table SCARR. It is then possible to identify an airline using the internal index of table CARRIER.
    In the SELECT loop for database table SPFLI, the corresponding bit for the airline is set either in the FRANKFURT field or the FRISCO field, depending on the departure city. The line number SY-TABIX is determined using a READ statement in which no fields are transported.
    The intersection and union of FRANKFURT and FRISCO are constructed using the bit operations BIT-AND and BIT-OR.
    The bits in INTERSECT and UNION are read one by one and evaluated in two DO loops. For each position in the fields with the value 1, a READ statement retrieves the airline code from the table CARRIER.
    <REMOVED BY MODERATOR>
    Cheers,
    Chandra Sekhar.
    Edited by: Alvaro Tejada Galindo on Mar 4, 2008 2:50 PM

  • Display Job ID and Department ID using set operators

    I am trying to produce a list with the following output from table employees. This query will produce a list of jobs for departments 20, 60, and 25 in that order.
    JOB_ID DEPARTMENT_ID
    ASST 20
    CLERK 60
    MAN 60
    MAN_B 25
    So far I have the query to give the output but I can't figure out the order by part...please help.
    SELECT job_id, department_id FROM employees
    INTERSECT
    SELECT job_id, department_id FROM employees WHERE department_id IN (20,60,25)
    ORDER BY ???
    ;

    The SQL Developer forum is meant for discussions about the tool with the same name.
    What you want is help on the SQL And PL/SQL forum...
    K.
    PS: Hint: ORDER BY Decode(...)
    Message was edited by: K.

  • Joiner and Set Operations

    I have a situation where source table A contains 3 code columns. The descriptions for the codes are contained in 3 different reference (lookup) tables. I want to denormalize target table B to include the descriptions for the 3 codes (as well as all columns from source table A).
    How do I design the mapping to include reference (lookup) tables code descriptions?

    You can use the key lookup mapping operator to get the values from the lookup tables on the fly. I.e. draw a connection from the source 'code' column to the key lookup operator, configure the key lookup operator to point to the appropriate lookup table with the correct lookup condition and connect the output of the key lookup to the name/description target column. The same 'code' column that you connected to the key lookup, you can now also directly connect to the code column in the target table.
    Regards:
    Igor

  • I'm using my airport express so I can control stereo via iPhone/iPad. It's linked to my router wirelessly (via "join network" setting). It works fine for a while, but then airplay ability / icon always disappears.

    My airport express is brand new and has latest software.
    My router is an Engenius 300N, and is 6 months old.
    iPhone is a 5S and has latest software.
    I've tried restarting router, resetting Airport, and spoken with apple support.
    The only thing that works is turning power to Airport off and on, but this only works for 5 minutes then disappears again.
    Help please
    Matt

    Oh - and also disappears from other devices eg macbook and ipad.
    thanks

  • Set operator Vs Self join

    hi all,
    i have a main table and a corresponding history table.
    Like employee and h_employee both has primary key as emp_id.
    now i want a query which should only retrieve the common values among these two table.
    I have an idea of doing this in two way 1.self join 2. set operator
    1.select emp_id
    from emp e,h_emp h
    using (emp_id)
    2. select emp_id from emp
    intersect
    select emp_id from h_emp
    now guide me which gives better performance? how to evaluate the performance in PL/SQL Developer client?
    is there any keyword to trace the timing of the each query?
    Please advice.
    Regards,
    Slu

    Set operators combines the output of 2 or more queries into single output....
    Internally its doing sorting the two tables
    Join is just matching the records based on the where condition ....
    use Explain plan to the cost of the query
    SQL> Explain plan for select emp_id
    from emp e,h_emp h
    using (emp_id)
    and use
    SQL > select * from plan_table
    to see the cost...
    Edited by: LPS on Jul 12, 2011 12:08 AM

  • Alternative to Outer Join Required !!!

    d/b version 11g R2
    i have 3 tables a,b and c.Below is the data in the tables.
    table A
    SQL> select id from a;
            ID
             1
             2
             3
             4
             5
    table B
    SQL> select id from b;
            ID
             1
             2
             3
             4
             5
             6
             7
             8
             9
            10
    table C
    SQL> select id from c;
            ID
             1
             2
             3
             4
             5
             6
             7
             8
             9
            10
            11
            12
            13
            14
            15
    I would like to rewrite the below query's in the form of union set operators, Could some body help me out
    select a.id,b.id,c.id from a full outer join b on (a.id=b.id) left outer join on (b.id=c.id);
    select a.id,b.id,c.id from a full outer join b on (a.id=b.id) right outer join on (b.id=c.id);

    You need to post expected results. Your both queries have syntax issues. Theay are missing table c name and ON clause. For eample, corrected first query:
    with a as (select level id from dual connect by level <= 5),
         b as (select level id from dual connect by level <= 10),
         c as (select level id from dual connect by level <= 15)
    select  a.id,
            b.id,
            c.id
      from      a
            full outer join
                b
              on a.id=b.id
            left outer join
                c
              on b.id=c.id
            ID         ID         ID
             1          1          1
             2          2          2
             3          3          3
             4          4          4
             5          5          5
                        6          6
                        7          7
                        8          8
                        9          9
                       10         10
    10 rows selected.
    SQL> But is it expected result? Are you looking for:
    with a as (select level id from dual connect by level <= 5),
         b as (select level id from dual connect by level <= 10),
         c as (select level id from dual connect by level <= 15)
    select  a.id,
            b.id,
            c.id
      from      a
            full outer join
                b
              on a.id=b.id
            full outer join
                c
              on b.id=c.id
            ID         ID         ID
             1          1          1
             2          2          2
             3          3          3
             4          4          4
             5          5          5
                        6          6
                        7          7
                        8          8
                        9          9
                       10         10
                                  15
            ID         ID         ID
                                  12
                                  14
                                  13
                                  11
    15 rows selected.
    SQL> SY.

  • Rewrite the query with out joins and group by

    Hi,
    This was an interview question.
    Table Names: bookshelf_checkout
    bookshelf
    And the join condition between these two tables is title
    We need to rewrite below query without using join condition and group by clause ?
    SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
               FROM bookshelf_checkout bc,bookshelf b
               WHERE bc.title(+)=b.title
               GROUP BY b.title;When I was in college, I read that most of the SELECT statements can be replaced by basic SQL operations (SET OPERATORS). Now I am trying to rewrite the query with SET operators but not able to get the exact result.
    Kindly help me on this.
    Thanks,
    Suri

    Something like this?
      1  WITH books AS (
      2  SELECT 'title 1' title FROM dual UNION ALL
      3  SELECT 'title 2' FROM dual UNION ALL
      4  SELECT 'title 3' FROM dual ),
      5  bookshelf AS (
      6  SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
      7  SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
      8  SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
      9  SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
    10  UNION
    11  (SELECT b.title, NULL FROM books b
    12  MINUS
    13* SELECT bs.title, NULL FROM bookshelf bs)
    SQL> /
    TITLE   MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
    title 1                                                           14
    title 2                                                           44
    title 3Lukasz

Maybe you are looking for

  • Want to edit text, but do not have the font

    Hello: I have a certificate in a pdf file, and I have Adobe Acrobat XI. The file contains the fonts Shalom Old Style (only a couple of Hebrew words) and the font Sanvito. Since I have neither font in Windows 7, I assume (also after having tried) that

  • Invoiced quantity is smaller than ordered quantity

    HI Everyone, When i'm doing archiving for the object MM_EKKO.The following message i got "Invoiced quantity is smaller than ordered quantity". Please can any one tell me how to close this type of purchase orders and continue archiving Regards subhash

  • Adobe Flash Player Quit Working

    I am running Windows Pro 7 (64-bit) and MSIE 9. For some unknown reason, my flash player quit working. No big deal, as I went to Adobe's download page and first obtained their uninstall executable. I used it to completely rid my machine of it's origi

  • Cant open mp4 video, bluetoothed from mobile.

    Hi, I am trying to get a video i took on my mobile to work on my mac but it wont open it. when i click to open it says: "Movie could not be opened: Invalid sample description was found in the movie" Its from a samsung mobile and mp4 format - i wonder

  • Capture a physical signature from a touch screen?

    Is there a way to capture a physical signature from a touch screen PC, tablet, or phone and imbed that directly into an Acrobat fillable form without utilizing other devices or programs? Thank you!