Query based on "NATURAL JOIN" statement of Oracle9i

I have created following 4 tables
create table CUST_MASTER
(CUST_NO NUMBER(3),CUST_NAME VARCHAR2(20),CUST_CITY VARCHAR2(20));
create table ITEM_MASTER
(ITEM_NO NUMBER(2),ITEM_NAME VARCHAR2(20),
PRICE NUMBER(4));
create table ORDER_HEADER
( ORDER_NO NUMBER(4), CUST_NO NUMBER(3));
create table ORDER_ITEMS
( ORDER_NO NUMBER(4), ITEM_NO NUMBER(2), QTY NUMBER(3));
Based on the above 4 table I have executed the following query.
select c.cust_no, c.cust_name, c.cust_city, oh.order_no, i.item_no, i.item_name, i.price, oi.qty
from cust_master c, item_master i, order_header oh, order_items oi
where c.cust_no = oh.cust_no and oh.order_no = oi.order_no and oi.item_no = i.item_no;
How should I build similar query in Oracle9i making use of "NATURAL JOIN" statement?

Hallo,
yes you are correct.
From SQL Reference
NATURAL JOIN The NATURAL keyword indicates that a natural join is being performed. A natural join is based on all columns in the two tables that have the same name. It selects rows from the two tables that have equal values in the relevant columns
Nothing about foreign keys.
Test:
select * from scott.emp natural join scott.dept
    DEPTNO      EMPNO ENAME      JOB               MGR HIREDATE                  SAL       COMM DNAME          LOC         
        20           7369     SMITH          CLERK                7902     17.12.1980            800                    RESEARCH           DALLAS
        30           7499     ALLEN          SALESMAN             7698     20.02.1981           1600            300     SALES              CHICAGO
        30           7521     WARD           SALESMAN             7698     22.02.1981           1250            500     SALES              CHICAGO
        20           7566     JONES          MANAGER              7839     02.04.1981           2975                    RESEARCH           DALLAS
        30           7654     MARTIN         SALESMAN             7698     28.09.1981           1250           1400     SALES              CHICAGO
        30           7698     BLAKE          MANAGER              7839     01.05.1981           2850                    SALES              CHICAGO
        10           7782     CLARK          MANAGER              7839     09.06.1981           2450                    ACCOUNTING         NEW YORK
        20           7788     SCOTT          ANALYST              7566     19.04.1987           3000                    RESEARCH           DALLAS
        10           7839     KING           PRESIDENT                     17.11.1981           5000                    ACCOUNTING         NEW YORK
        30           7844     TURNER         SALESMAN             7698     08.09.1981           1500              0     SALES              CHICAGO
        20           7876     ADAMS          CLERK                7788     23.05.1987           1100                    RESEARCH           DALLAS
        30           7900     JAMES          CLERK                7698     03.12.1981            950                    SALES              CHICAGO
        20           7902     FORD           ANALYST              7566     03.12.1981           3000                    RESEARCH           DALLAS
        10           7934     MILLER         CLERK                7782     23.01.1982           1300                    ACCOUNTING         NEW YORK
alter table scott.emp drop constraint fk_deptno
select * from scott.emp natural join scott.dept
    DEPTNO      EMPNO ENAME      JOB               MGR HIREDATE                  SAL       COMM DNAME          LOC         
        20           7369     SMITH          CLERK                7902     17.12.1980            800                    RESEARCH           DALLAS
        30           7499     ALLEN          SALESMAN             7698     20.02.1981           1600            300     SALES              CHICAGO
        30           7521     WARD           SALESMAN             7698     22.02.1981           1250            500     SALES              CHICAGO
        20           7566     JONES          MANAGER              7839     02.04.1981           2975                    RESEARCH           DALLAS
        30           7654     MARTIN         SALESMAN             7698     28.09.1981           1250           1400     SALES              CHICAGO
        30           7698     BLAKE          MANAGER              7839     01.05.1981           2850                    SALES              CHICAGO
        10           7782     CLARK          MANAGER              7839     09.06.1981           2450                    ACCOUNTING         NEW YORK
        20           7788     SCOTT          ANALYST              7566     19.04.1987           3000                    RESEARCH           DALLAS
        10           7839     KING           PRESIDENT                     17.11.1981           5000                    ACCOUNTING         NEW YORK
        30           7844     TURNER         SALESMAN             7698     08.09.1981           1500              0     SALES              CHICAGO
        20           7876     ADAMS          CLERK                7788     23.05.1987           1100                    RESEARCH           DALLAS
        30           7900     JAMES          CLERK                7698     03.12.1981            950                    SALES              CHICAGO
        20           7902     FORD           ANALYST              7566     03.12.1981           3000                    RESEARCH           DALLAS
        10           7934     MILLER         CLERK                7782     23.01.1982           1300                    ACCOUNTING         NEW YORK
ALTER TABLE scott.emp
ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno)
REFERENCES SCOTT.dept (deptno)
ENABLE NOVALIDATE
/Regards
Dmytro Dekhtyaryuk

Similar Messages

  • Query tuning - update with join statement

    Hi,
    I have 2 tables (table1 and table 2) with following condition:
    TABLE 1 - Need to selection a column say staus ! = 'C'
    join emp_id and project_id of TABLE1 and TABLE2
    From join I am selecting minimun and maximum of TABLE2.salary and updating TABLE3 respective column.
    Now as per requirement I have to update required column on TABLE2.EMP_ID = TABLE3.EMP_ID
    Following is the query I have written,
    update TABLE3 T3
    set (T3.MIN_salary, T3.MAX_salary) = (
    select min(c.salary), max(c.salary)
    from TABLE2 t2, TABLE1 t1
    where t2.emp_id = t1.emp_id
    and t2.project_id = t1.project_id
    and t1.status != 'C'
    and t2.emp_id = t3.emp_id)
    The above query takes 7 min to update some 8 lakhs records. Can you please suggest some other optimized method to do so?
    Thanks in advance
    Sandeep

    Hi SBH
    Required info for "Please provide oracle version, indexes on the 3 tables and also stats info for the table from user_tables"
    1) Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit
    2) Index is not there on TABLE3 (It is temporary table)
    Composite index on TABLE 1 - column1, column2 - Index name say table1index1
    Composite index on TABLE 2 - column1, column2 - Index name say table2index1
    TABLE_NAME     NUM_ROWS     BLOCKS     DEGREE     INSTANCES     SAMPLE_SIZE     PARTITIONED
    FXO_CFW     11767341     144930     1     1     11767341     NO
    FXO_RSL_STBL               1     1          NO
    FXO_TRN     4917778     230196     1     1     4917778     NO
    execution plan is:
    Execution Plan
    Plan hash value: 3131833900
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | UPDATE STATEMENT | | 789K| 23M| 2397 (3)| 00:00:29 |
    | 1 | UPDATE | TABLE3 | | | |
    | 2 | TABLE ACCESS FULL | TABLE3 | 789K| 23M| 2397 (3)| 00:00:29 |
    | 3 | SORT AGGREGATE | | 1 | 28 | | |
    | 4 | TABLE ACCESS BY INDEX ROWID | TABLE2 | 1 | 17 | 3 (0)| 00:00:01 |
    | 5 | NESTED LOOPS | | 1 | 28 | 12 (0)| 00:00:01 |
    |* 6 | TABLE ACCESS BY INDEX ROWID| TABLE1 | 1 | 11 | 9 (0)| 00:00:01 |
    |* 7 | INDEX RANGE SCAN | IDX_TABLE1INDEX1 | 6 | | 3 (0)| 00:00:01 |
    |* 8 | INDEX RANGE SCAN | IDX_TABLE2INDEX1 | 1 | | 2 (0)| 00:00:01 |
    Thanks for quick response,
    Sandeep

  • IN A NATURAL JOIN THE ANSWER QUERY DEPENDS OF PROJECTION ??!!

    Hi,
    Related to this question
    Link: problems with natural join
    Does the answer query depend of projection in a complex NATURAL JOIN query?
    Thanks,
    Ion

    You are most likely encountering this bug:
    Bug 5031632 - Wrong results from NATURAL JOIN, Metalink Note: 5031632.8
    It currently affects all versions of Oracle.
    You can tell something weird is going on by the EXPLAIN PLAN results:
    Query:
    SELECT     *
    FROM     BOOK
    NATURAL JOIN
         AUTHORSHIP
         NATURAL JOIN
              AUTHOR
              NATURAL JOIN
              COUNTRY
    WHERE     COUNTRY.NAMECOUNTRY = 'Canada'
    Explain Plan:
    SQL> /
    PLAN_TABLE_OUTPUT
    Plan hash value: 3878360587
    | Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT |      |     1 |    39 |    28   (8)| 00:00:01 |
    |*  1 |  HASH JOIN       |      |     1 |    39 |    28   (8)| 00:00:01 |
    |*  2 |   HASH JOIN      |      |     1 |    29 |    17   (6)| 00:00:01 |
    |*  3 |    HASH JOIN     |      |     1 |    23 |     9  (12)| 00:00:01 |
    |   4 |     VIEW         |      |     2 |    16 |     2   (0)| 00:00:01 |
    |   5 |      UNION-ALL   |      |       |       |            |          |
    |   6 |       FAST DUAL  |      |     1 |       |     2   (0)| 00:00:01 |
    |*  7 |       FILTER     |      |       |       |            |          |
    |   8 |        FAST DUAL |      |     1 |       |     2   (0)| 00:00:01 |
    |   9 |     VIEW         |      |     3 |    45 |     6   (0)| 00:00:01 |
    |  10 |      UNION-ALL   |      |       |       |            |          |
    |  11 |       FAST DUAL  |      |     1 |       |     2   (0)| 00:00:01 |
    |  12 |       FAST DUAL  |      |     1 |       |     2   (0)| 00:00:01 |
    |  13 |       FAST DUAL  |      |     1 |       |     2   (0)| 00:00:01 |
    |  14 |    VIEW          |      |     4 |    24 |     8   (0)| 00:00:01 |
    |  15 |     UNION-ALL    |      |       |       |            |          |
    |  16 |      FAST DUAL   |      |     1 |       |     2   (0)| 00:00:01 |
    |  17 |      FAST DUAL   |      |     1 |       |     2   (0)| 00:00:01 |
    |  18 |      FAST DUAL   |      |     1 |       |     2   (0)| 00:00:01 |
    |  19 |      FAST DUAL   |      |     1 |       |     2   (0)| 00:00:01 |
    |  20 |   VIEW           |      |     5 |    50 |    10   (0)| 00:00:01 |
    |  21 |    UNION-ALL     |      |       |       |            |          |
    |  22 |     FAST DUAL    |      |     1 |       |     2   (0)| 00:00:01 |
    |  23 |     FAST DUAL    |      |     1 |       |     2   (0)| 00:00:01 |
    |  24 |     FAST DUAL    |      |     1 |       |     2   (0)| 00:00:01 |
    |  25 |     FAST DUAL    |      |     1 |       |     2   (0)| 00:00:01 |
    |  26 |     FAST DUAL    |      |     1 |       |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - access("BOOK"."IDBOOK"="AUTHORSHIP"."IDBOOK")
       2 - access("AUTHORSHIP"."IDAUTHOR"="AUTHOR"."IDAUTHOR")
       3 - access("AUTHOR"."IDCOUNTRY"="COUNTRY"."IDCOUNTRY")
       7 - filter(NULL IS NOT NULL)
    Query:
    SELECT     NAMEBOOK
    FROM     BOOK
    NATURAL JOIN
         AUTHORSHIP
         NATURAL JOIN
              AUTHOR
              NATURAL JOIN
              COUNTRY
    WHERE     COUNTRY.NAMECOUNTRY = 'Canada'
    Explain Plan:
    SQL> /
    PLAN_TABLE_OUTPUT
    Plan hash value: 3246924444
    | Id  | Operation             | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT      |      |     1 |    24 |    27   (4)| 00:00:01 |
    |*  1 |  HASH JOIN            |      |     1 |    24 |    27   (4)| 00:00:01 |
    |   2 |   MERGE JOIN CARTESIAN|      |     1 |    21 |    19   (6)| 00:00:01 |
    |*  3 |    HASH JOIN          |      |     1 |    11 |     9  (12)| 00:00:01 |
    |   4 |     VIEW              |      |     2 |    16 |     2   (0)| 00:00:01 |
    |   5 |      UNION-ALL        |      |       |       |            |          |
    |   6 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |*  7 |       FILTER          |      |       |       |            |          |
    |   8 |        FAST DUAL      |      |     1 |       |     2   (0)| 00:00:01 |
    |   9 |     VIEW              |      |     3 |     9 |     6   (0)| 00:00:01 |
    |  10 |      UNION-ALL        |      |       |       |            |          |
    |  11 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  12 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  13 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  14 |    BUFFER SORT        |      |     5 |    50 |    19   (6)| 00:00:01 |
    |  15 |     VIEW              |      |     5 |    50 |    10   (0)| 00:00:01 |
    |  16 |      UNION-ALL        |      |       |       |            |          |
    |  17 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  18 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  19 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  20 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  21 |       FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
    |  22 |   VIEW                |      |     4 |    12 |     8   (0)| 00:00:01 |
    |  23 |    UNION-ALL          |      |       |       |            |          |
    |  24 |     FAST DUAL         |      |     1 |       |     2   (0)| 00:00:01 |
    |  25 |     FAST DUAL         |      |     1 |       |     2   (0)| 00:00:01 |
    |  26 |     FAST DUAL         |      |     1 |       |     2   (0)| 00:00:01 |
    |  27 |     FAST DUAL         |      |     1 |       |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - access("BOOK"."IDBOOK"="AUTHORSHIP"."IDBOOK")
       3 - access("AUTHOR"."IDCOUNTRY"="COUNTRY"."IDCOUNTRY")
       7 - filter(NULL IS NOT NULL)
    41 rows selected.There are two major differences in these plans.
    1. The correct result has HASH JOINS. However the incorrect result has a MERGE JOIN CARTESIAN. A cartesian join should not be here as there is no need for one per the table structure and NATURAL JOIN syntax.
    2. There is a filter condition missing in the predicate information session.
    Both #1 and #2 above point to the bug I identified. The current work around us to use the JOIN ... ON syntax.
    HTH!

  • Dynamic From statement in select query and/or outer join not working

    Dear Experts, I have a select query where the select columns are dynamic, the where condition is also dynamic. It is of the below format:
    Select (dynamic columns) INTO <wa>
    FROM a inner join b on af1 = bf1
    inner join c on af2 = cf2......
    WHERE (dynamic conditios)
    ORDER BY ( dynamic sort condition).
    Now I have to include some tables (dynamically depending on the user input) in the inner join statement which will give description for the selected fields. And these database tables may or may no be empty. So in this case, my select query will not return any data if these tables are empty. And I dont want that.
    I tried using outer join for the extra tables but it gave me a runtime error. I also tried forming the inner join statement dynamically but it was not supporting.
    Kindly give me pointers.
    Thanks

    Hey thanks for the reply, but the problem is not solved.
    I am already using  ( fileds, value) like table in my where condition and the select statement was working properly.
    the problem is that now I have to include some tables in the join statement which can be empty and so i want to use Outer join.
    But I am getting a runtime error as below:
    Error analysis
        An exception occurred that is explained in detail below.
        The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SYNTAX', was not
         caught in
        procedure "ZATSCSNG_RFC_READ_TABLE" "(FUNCTION)", nor was it propagated by a
         RAISING clause.
        Since the caller of the procedure could not have anticipated that the
        exception would occur, the current program is terminated.
        The reason for the exception is:
        The running ABAP program wanted to execute a SELECT statement whose
        WHERE condition was (partly) specified dynamically. The part that is
        specified in an internal table at runtime is compared to a field of the
        right table of an LEFT OUTER JOIN. Such comparisons are not supported by
         all database systems and are therefore not allowed.

  • Natural join , join on & join using

    Hi
    I am preparing for the IZO-051 exams .I am totally confused with the concept of joins when answering questions .
    In the Oracle certification book ..HR schema example of employees and departments is given
    While answering the multiple choice questions it is difficult to to choose answer !!
    I wanted to know if *Join using & join on conditions yield the same result in some cases or
    where there are only 1 common column between two tables * ??
    for eg if the same HR schema is to be taken
    QUES: Which sql statement produce the name ,department name & city of all the employees who earn more than 10,000.
    The correct answer given is the query below ..
    I understand the query delivers the needed result ..
    select first_name ,department_name,city
    from employees e
    join departments d
    using(department_id)
    join locations l
    using(location_id)
    where salary > 10000;
    Also the query below gives the same results but this is not the answer???
    select first_name, department_name,city
    from employees e join departments d on (e.department_id= d.department_id)
    join locations l on (d.location_id=l.location_id)
    where salary>10000;
    ALSO is there any efficiency issues related to the queries??

    Hi,
    VANPERSIE wrote:
    Frank Kulash wrote:
    The only thing you really have to know about them is "Don't use them." >Nobody in the real world does.Dear Frank
    You confuse me
    why you said noboby in the real world use them?Obviously, that was hyperbolic. I haven't asked everyone in the world if they use USING or EXISTS.
    I have seen a lot of code, on this forum and elsewhere, written by a large number of people I honestly think that fewer than 1 in 1000 uses either USING or NATURAL JOIN, and it is always in the context of studying for an exam, like you, or trying something the user saw in a book. I have absolutely never seen any production code that used either.
    please I just want to know whyWhy are they not used? They make assumptions about the relationships between tables based entirely on column names. Those assumptions do often not reflect reality. People are liable to name their columns anything, sometimes with very good reasons, that don't match the assumptions made by USING and NATRUAL JOIN.
    NATURAL JOIN is more obviious. It's common to have auditing columns such as created_date or last_modify_user in several tables. No human being would assume that these columns were designed for joining, but that's exactly what NATURAL JOIN assumes.
    USING has the same problem, to a much lesser degree. If you're using 3 tables, and they all have a column named order_id, you can't use order_id to join only 2 of the tables with USING. Also, it reverses the convention of qualifying columns that are used in multiple tables. Many people regulary qualify columns (e.g. <b>d.</b>deptno); personally, I think it's a great programming practice. When you use ON, then you must qualify column names that occur in multiple tables, but when you use USING then you must not qualify the columns that occur in join conditions, but you must qualify other ambiguous columns. Most people find that very conf<b>using</b>.

  • Using CASE in the join statement in the AND clause. Where's the problem?

    All,
    I have my code, where based on the case statement, I would like to restrict the number of records in my JOIN condition. It's the CASE statement that I have used, that's giving me the problem. Not sure, what the problem is. The error says (in my case expression):
    Msg 102, Level 15, State 1, Line 59
    Incorrect syntax near '>'.
    Select a.Col1, a.Col2, b.Col1, b.Col2, b.Col3
    From Table1 a
    Join Table2 b
    On a.id = b.id
    Join Table3 c
    On c.FiltId = a.FiltId
    And
    Case
    When b.Col3 <> 5 --b.Col3 is an INT field
    Then (b.OutDate >= DATEADD(DAY, -30, c.InsertDate) And b.OutDate <= DATEADD(DAY, 30, c.InsertDate))
    When b.Col3 = 5
    Then (b.InDate >= DATEADD(DAY, 5, c.InsertDate))
    End
    Join Table 4 -- Other Join conditions and where clause conditions

    You do not know the CASE is an expression and fields are not columns. This is fundamental and probably means that a lot of your code needs work. 
    You can probably re-write the query with something like this: 
    SELECT A.col1, A.col2, B.col1, B.col2, B.col3
      FROM Table_1 AS A, Table_2 AS B, Table_3 AS C
     WHERE A.generic_id = B.generic_id
       AND C.foobar_id = A.foobar_id
       AND CASE
           WHEN B.col3 <> 5 
                AND (B.something_out_date 
                     <= DATEADD(DAY, -30, C.something_insertion_date) 
           THEN 'T'
           WHEN B.col3 = 5 
                AND (B.something_out_date 
                    >= DATEADD(DAY, -30, C.something_insertion_date) 
           THEN 'T'
           ELSE 'F' END = 'T' ;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Creating Infoset query based on ABAP program

    Hello
    I have 3 tables FEBEP, BKPF and BSEG and I need to join the 3 tables based on:
    FEBEP-MANDT = BKPF-MANDT = BSEG-MANDT
    FEBEP-NBBLN = BKPF-BELNR = BSEG-AUGBL
    FEBEP-GJAHR = BKPF-GJAHR = BSEG-GJAHR
    Then I have a few view fields from all the 3 tables. After this I can build an infoset query based on structure + ABAP program, and a generic datasource on top of it.
    Can someone give me the ABAP code to be written SE38? Also should I select integrated program/external program in the infoset query?
    Thanks,
    Srini.

    Hi,
    Even if you create an ABAP program for infoset, you will writing a SELECT statement from BSEG table which is quite huge.
    And you will putting JOIN with other tables.
    Performance wise this is not advisble.
    Why do not try the following other tables and check if the fields you need are available?
    You can't join BSEG as it is a Cluster Table.In the place of BSEG you can use:
    Account Recivables data use BSID and BSAD tables
    GL Account Related data use BSIS and BSAS tables
    Account payables data use BSIK and BSAK tables
    Thanks.

  • How to specify more than Two Tables in NATURAL JOIN

    Hi,
    I am using Oracle9i R-2. I want help in writing a Query using Two Tables in SQL*PLUS. I am using ANSI/ISO Standrard for table-joins:
    select col1, col2, descr
    from tab1 natural join tab2
    There are two columns col1 & col2 is common between thse two tables. So, it will join them and query execute well.
    How can i use 3rd table tab3 in the same way in Natural Join...? If column col1 & col2 is available in tab3 also.
    I tried this way, but it gives me error:
    select col1, col2
    from tab1 natural join tab2 natural join tab3
    Is it possible to specify more than two tables in Natural Join Clause.
    Please check that out & help please. Thanks.
    Regards,
    Kamesh Rastogi

    I do not get an error when I try the same thing on the same version, as demonstrated below. Can you post your table structure and a copy and paste of a run of your actual query with the error that you are receiving?
    scott@ORA92> select banner from v$version
      2  /
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
    PL/SQL Release 9.2.0.1.0 - Production
    CORE     9.2.0.1.0     Production
    TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
    NLSRTL Version 9.2.0.1.0 - Production
    scott@ORA92> select * from tab1
      2  /
          COL1       COL2 COL3
             1          1 A
            10         10 B
    scott@ORA92> select * from tab2
      2  /
          COL1       COL2 COL4
             1          1 C
            20         20 D
    scott@ORA92> select * from tab3
      2  /
          COL1       COL2 COL5
             1          1 E
            30         30 F
    scott@ORA92> select col1, col2, col3, col4, col5
      2  from tab1 natural join tab2 natural join tab3
      3  /
          COL1       COL2 COL3 COL4 COL5
             1          1 A    C    E
    scott@ORA92>

  • IN in NATURAL JOIN help

    SELECT name,telephone
    FROM Customer where cid IN
    (SELECT cid
    FROM Order where oid IN
    (SELECT oid
    FROM OrderDetal where icode IN
    (SELECT icode
    FROM item where itemName=’Dry Fish 200g’)));
    How can I convert this query to Natural join?
    Thanks in Advance

    Hi,
    You can't do that with NATURAL JOIN.
    Forget about NATURAL JOIN.  I've never seen it used outside of a texbook (or on a forum like this) for good reasons.  If you add a column to any table, that may change the results of any NATURAL JOIN involving that table.  You don't want to look at all those queries whenever you condiser adding a new column to a table; you don't even want to keep track of which queries you'd have to look at.
    Upon sober reflection, I think it might be possible to do this with NATURAL JOIN, depending on how your tables are designed. I can't tell if it's possible or not without seeing the CREATE TABLE statements for all the tables involved, including unique constraints. If it is possible, it would look like this:
    SELECT        c.name, c.telephone
    FROM          Customer   c
    NATURAL JOIN  Orders     o -- ORDER is not a good table name
    NATURAL JOIN  OrderDetal od
    NATURAL JOIN  Item  i
    WHERE         i.ItemName = 'Dry Fish 200g'
    Depending on your data, you may need SELECT DISTINCT.
    Message was edited by: FrankKulash
    NATURAL JOIN is still a bad idea.

  • Join Using vs. Natural Join

    Hi I'm preparing for "Oracle9i: DBA Fundamentals I" exam as well.
    I just don't understand why I get different results with these two queries
    Here I 'm using join
    1 select last_name, department_name
    2 from employees
    3 join departments using (department_id)
    4* where last_name like'H%'
    SQL> /
    LAST_NAME DEPARTMENT_NAME
    Hunold IT
    Himuro Purchasing
    Hall Sales
    Hutton Sales
    Hartstein Marketing
    Higgins Accounting
    but when I use a natural join I get fewer rows:
    SQL> select last_name, department_name
    2 from employees
    3 natural join departments
    4 where last_name like'H%'
    5 /
    LAST_NAME DEPARTMENT_NAME
    Himuro Purchasing
    Hall Sales
    what's the difference between join and natural join?

    A NATURAL JOIN joins the two tables based on columns with the same name. So, in your case, the natural join is equivalent to:
    SQL> SELECT last_name, department_name
      2  from employees JOIN departments using (department_id, manager_id)
      3  WHERE last_name like 'H%'
      4  /
    LAST_NAME                 DEPARTMENT_NAME
    Hall                      Sales
    Himuro                    PurchasingNatural joins are a Bad Thing (tm).
    HTH
    John

  • Confusion about natural join

    In the HR schema, for the natural join done on three tables employees,department and locations, different no rows are returned based on the columns in the select list
    For the query
    select salary,department_id,location_id,country_id from employees
    natural join departments
    natural join locations
    32 rows are retrived.
    While for the query
    select salary,department_id from employees
    natural join departments
    natural join locations
    736 rows are retrieved.
    Please explain how column name in select list affect the natural join

    Hi,
    suchibm wrote:
    In the HR schema, for the natural join done on three tables employees,department and locations, different no rows are returned based on the columns in the select list
    For the query
    select salary,department_id,location_id,country_id from employees
    natural join departments
    natural join locations
    32 rows are retrived.
    While for the query
    select salary,department_id from employees
    natural join departments
    natural join locations
    736 rows are retrieved.
    Please explain how column name in select list affect the natural join
    I can't reproduce your results.  I get 32 rows with either query.
    I tried this in Oracle 11.2.0.2.0.  What version are you using?
    If I use "CROSS join locations" instead of "NATURAL join locations"  in either query, then I get 32 * 23 = 736 rows.  23 is the number of rows in the hr.locations table.  It's as if, when there is nothing from the locations table in the SELECT clause, it's doing a CROSS join where you specified NATURAL join.
    Nobody uses NATURAL JOIN outside of textbooks, and with good reason.  Don't waste your time exploring something you'll never want to use.

  • Adding to WHERE based on an IF statement in a procedure

    Is this legitimate?
    select CRIME_CLASSIFICATION_ID, crime_type, nvl(count(CRIME_CLASSIFICATION_ID),0) as CRIMECNT
    From vaps.vw_offenses
        where location_id in (vaLoc)
        If fromdate is not null Then
            AND offense_date >= to_date(fromdate, 'mm/dd/yyyy')  AND offense_date <= to_date(todate,'mm/dd/yyyy')
        End If;
    group by crime_classification_id, crime_type
    If it's not legitimate, how do I get the added clause into my query based on the value of fromdate?
    Thanks
    Chris

    Hi, Chris,
    Perhaps you want soemthing like this:
    WHERE   location_id in (vaLoc)
    AND     (    fromdate     IS NULL
            OR   offense_date BETWEEN TO_DATE (fromdate, 'mm/dd/yyyy')
                              AND     TO_DATE (todate,   'mm/dd/yyyy')
    By the way, storing date information in string columns or variables is a really bad idea.  Use DATE columns and variables for date information; it will be simpler, more robust and more efficient; you won't need to use TO_DATE in queries like this.
    I hope this answers your question.
    If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Point out where the condition above is giving the wrong results, and explain, using specific examples, how you get the correct results from the given data in those places.  If you changed the condition at all, post your code.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Query based on a date range

    I want to query based on a date range. I wrote the following EJB QL:
    "select object(a) from ActionItems as a where a.dueDate
    between ?1 and ?2"
    But when I deployed the application to Oracle's EJB server, I got the following error:
    Auto-deploying biogen.jar (No previous deployment found)... Invalid type for expression (a.dueDate BETWEEN ?1 AND ?2)
    EJB QL statement : 'select object(a) from ActionItems as a where a.dueDate between ?1 and ?2'
    EJB QL method : public abstract java.util.Collection ActionItemsLocalHome.findByDueDateRange(java.sql.Date,java.sql.Date) throws javax.ejb.FinderException
    at com.sun.ejb.ejbql.parser.EjbQLParser.parse EjbQLParser.java:218)
    at com.sun.ejb.ejbql.EjbQLDriver.parse(EjbQLDriver.java:86)
    at com.sun.ejb.sqlgen.SQLGenerator.generateSQLForEjbQLQueries(SQLGenerator.java:704)
    It works fine for a number range, but does not work for a date range.
    Any one has an idea on how to write a correct EJB QL for a query based on date range?
    Thanks.
    Jingzhi

    Re-posted. Please help!

  • Purchase register query based on down payment invoice

    Hai All,
    I am creating an invoice based on purchase order.The vat  tax is being calculated in the downpayment. I want the vat to be displayed in the downpayment invoice as well as the a/p invoice but i m not getting it in the a/p invoice. Pls guide me with the linking of tables to get the query working. I have formatted the fields req but could not link the tables...Pls guide me with that...
    Thanks & Regards,
    Neela

    Hi Neela,
    Check the thread.
    Re: Purchase register query based on down payment invoice
    FROM PCH1 T0
    INNER JOIN OPCH T1 ON T0.DocEntry = T1.DocEntry
    INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode
    LEFT OUTER JOIN PCH12 T3 ON T1.DocEntry = T3.DocEntry
    LEFT JOIN ODPI T4 ON T1.CardCode = T4.CardCode
    INNER JOIN DPO1 T5 ON T0.ItemCode = T5.ItemCode
    Close the thread, if issue solved.
    Regards,
    Madhan.

  • A simple problem with natural join

    i have 2 tables what have columns with distinct's names are referenced.
    natural join not works 'cose only foreign key reference this 2 tables.
    Have something to make table join in using an specific foreign key??
    I'm using oracle9i
    Example:
    table a( a_cod number, a_name varchar2(20))
    table b( b_cod number, b_month_year date, b_salary number)
    ALTER TABLE b ADD ( CONSTRAINT b_a_FK FOREIGN KEY (b_cod)
    REFERENCES a (a_cod));
    select * from a natural join b
    where trunc(b_month_year) = trunc(sysdate)

    I'm not express me correctly.
    We have 2 tables like this:
    CREATE TABLE PPTBA001
      PPA001_GROUP             NUMBER(3)            NOT NULL,
      PPA001_PEOPLECODE        VARCHAR2(20 BYTE)    NOT NULL,
      PPA001_NAME              VARCHAR2(60 BYTE)    NOT NULL,
      PPA001_DATEOFBIRTHDAY    DATE                 NOT NULL);
    CREATE TABLE CLTBA001
      CLA001_GROUP            NUMBER(3)             NOT NULL,
      CLA001_CLIENTCODE       VARCHAR2(20 BYTE)     NOT NULL,
      CLA001_SITUATION        NUMBER(1)             DEFAULT 1);
    SELECT * FROM PPTBA001 NATURAL JOIN CLTBA001;
    ALTER TABLE CLTBA001 ADD (
      CONSTRAINT CLA001_PPAA001_FK1 FOREIGN KEY (CLA001_GROUP, CLA001_CLIENTCODE)
        REFERENCES PPTBA001 (OFA001_GROUP,OFA001_PEOPLECODE));
          the select returns without results. 'cose does not have columns like same name.
    Natural join use a columns with same name. I need something use an foreign key.
    Why?? Now i have a problem .. i need aggregate one more column in primary key
    and i need change all selects that have join with PPTBA001 and put manualy a new column.. if have a other possibility like natural join to using a foreign key to join the selects problems is so more easy to resolve.
    Message was edited by:
    jonas.lima

Maybe you are looking for

  • HP Pavilion dv6-6145dx Entertainm​ent Notebook PC not playing blu ray

    So I have a HP Pavilion dv6-6145dx Entertainment Notebook PC and i was told it does play blu ray but its not working so can I get some help? Thank You! 

  • Why is the distort filter greyed out so I cannot access the twirl tool in CS6 Extended.

    I have a layer which is white with a clouds render but when I want to go the next step I cannot access the twirl tool or the zig zag tool. I notice this is the only filter I cannot access. Any advise as to why or what I should do to be able to access

  • For loop don't updates

    Hi, First I'm new at labview , I'm trying to read 8 checkboxs vals and for each checkbox I'm queuing a single val witch will be a test. I have a problem with my for loop it simply won't update and staying at 0 val ( only one iteration )  vi is attach

  • Crystal Reports and PHP

    Post Author: amyfletcher CA Forum: General I am working on an application for one of our clients in PHP and MySQL.  The client would like to utilize Crystal Reports to create some of the reports in the database.  I am not very familiar with Crystal R

  • Trying to turn off two step verification but just stuck in an infinite loop

    So, I try to turn two step verification off, I go to manage my ID, to Security and choose Turn off Two Step...so far so good I am told that I have to choose three security questions, enter my DOB and provide a recovery email, then press Continue...st