Oracle: Inner join syntax

Hello
I am confused with Inner Join syntax.
According to the defination of Inner Join, it is said that
Inner joins return all rows in multiple tables satisfied the join condition.

By the way by examples of inner join in web, join Inner is written in these syntaxes as indicated:

First syntax:

SELECT Person.LastName, Person.FirstName, person Sales.OrderNo INNER JOIN sales on Person.P_Id = Sales.P_Id

Second syntax:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM the suppliers orders
WHERE suppliers.supplier_id = orders.supplier_id;

Please tell me if the keyword Inner Join its use is mandatory or not?

Thank you for reading.

user672373773 wrote:
First of all, thanks Anurag to your prompt response.

I have a request here, please tell me why oracle has provided two syntax? This is the ANSI and non-ANSI syntax

I know, it's the Oracle had a format no ANSI to join since its initial versions. In later versions, oracle decided to except ANSI format too since Oracle want to support the ansi format in almost everything.

So since he was using the ansi non format, it can't support/stop using only for backward compatibility and also it as having the users to use the ansi format, it is both.

Concerning
Anurag

Tags: Database

Similar Questions

  • use the ansi join syntax

    From what I've been reading is preferable to use the new syntax (don't know how is it news)

    The ANSI join syntax was new to Oracle in Oracle 8i in 1998 - that is 15 years old.

    It can produce more readable code and is also much more readable and less human-source of errors for outer joins.

    The ANSI format allows an outer join between several tables in a way the old (+) syntax specific to oracle does not and introduced the JOIN FULL OUTER you should use very rarely.

    You should not use NATURAL JOIN in code - add columns not related to the tables involved can give very different results.

    There have not been important bugs for ANSI joins in Oracle from Oracle 10.2 set up about 8 years ago.

    As Paul says, the part on should be the criteria to join between the tables. Did clause should be the filtering of the joined tables.

    So assuming start_date and end_date in_property_id are variable

    SELECT resv_num, unit_date

    OF p_resv_unit ru

    INNER JOIN p_pm_unit_night pun

    ON pun.resv_unit_id = ru.resv_unit_id

    WHERE pun.property_id = in_property_id

    AND pun.unit_date BETWEEN start_date AND end_date

    AND pun.pm_unit_num = cvUnitNum;

    If start_date and end_date are the columns of p_resv_unit the query would be:

    SELECT resv_num, unit_date

    OF p_resv_unit ru

    INNER JOIN p_pm_unit_night pun

    ON pun.resv_unit_id = ru.resv_unit_id AND pun.unit_date BETWEEN ru.start_date AND ru.end_date

    WHERE pun.property_id = in_property_id

    AND pun.pm_unit_num = cvUnitNum;

    Inner join queries work with criteria in the wrong place, but they are harder to read. Outer joins do not work unless you put the criteria in the right place.

  • Bug in outer join syntax?

    I use Oracle 10.2, and I think I found a bug in what is allowed for the outer join syntax. Specifically, I am allowed to specify "outer join" without specifying if it's left, right, or full outer join. It behaves as an inner join.

    The documents show that the type_de_jointure is optional, but does not allow the 'outside' keyword be used alone: http://download.oracle.com/docs/cd/A97630_01/server.920/a96540/statements_103a.htm#2126207

    Small example:
    create table TABLE_A (ID number(10) primary key, VALUE_A varchar2(50));
    create table TABLE_B (ID number(10) primary key, VALUE_B varchar2(50));
    insert into TABLE_A (ID, VALUE_A) values (1, 'abc');
    insert into TABLE_A (ID, VALUE_A) values (2, 'def');
    insert into TABLE_A (ID, VALUE_A) values (3, 'ghi');
    insert into TABLE_B (ID, VALUE_B) values (2, 'jkl');
    insert into TABLE_B (ID, VALUE_B) values (3, 'mno');
    insert into TABLE_B (ID, VALUE_B) values (4, 'pqr');
    commit;
    select ID, VALUE_A from TABLE_A;
    select ID, VALUE_B from TABLE_B;
    select ID, VALUE_A, VALUE_B from TABLE_A join TABLE_B using (ID);
    select ID, VALUE_A, VALUE_B from TABLE_A full outer join TABLE_B using (ID);
    select ID, VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID);
    The release of the last three selects shows that the OUTER JOIN behaves as a simple JOIN, rather than return a syntax error or at least behave like a FULL OUTER JOIN (this is where the absence of a syntax error is misleading):
    SQL> select ID, VALUE_A, VALUE_B from TABLE_A join TABLE_B using (ID);
    
            ID VALUE_A                                            VALUE_B
    ---------- -------------------------------------------------- --------------------------------------------------
             2 def                                                jkl
             3 ghi                                                mno
    
    SQL> select ID, VALUE_A, VALUE_B from TABLE_A full outer join TABLE_B using (ID);
    
            ID VALUE_A                                            VALUE_B
    ---------- -------------------------------------------------- --------------------------------------------------
             1 abc
             2 def                                                jkl
             3 ghi                                                mno
             4                                                    pqr
    
    SQL> select ID, VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID);
    
            ID VALUE_A                                            VALUE_B
    ---------- -------------------------------------------------- --------------------------------------------------
             2 def                                                jkl
             3 ghi                                                mno
    
    SQL> 
    Y at - there somewhere that I can tell you that?

    If you have a supported Oracle agreement you can save a Service request with Oracle, but they can answer that this is not a bug. The problem is that the 'outside' keyword in your 3rd example is treated as an alias for TABLE_A because it is not considered as a reserved keyword.

    with table_a as (
    select 1 as id, 'abc' as value_a from dual union all
    select 2 as id, 'def' as value_a from dual union all
    select 3 as id, 'ghi' as value_a from dual
    
    )
    , table_b as (
    select 2 as id, 'jkl' as value_b from dual union all
    select 3 as id, 'mno' as value_b from dual union all
    select 4 as id, 'pqr' as value_b from dual
    )
    select ID, outer.VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID);
    
    ID                     VALUE_A VALUE_B
    ---------------------- ------- -------
    2                      def     jkl
    3                      ghi     mno
    

    If you query the view RESERVED_WORDS of V$ it will tell you what keywords are reserved.

    select * from V$RESERVED_WORDS where keyword in ('OUTER', 'SELECT','USING');
    
    KEYWORD                        LENGTH                 RESERVED RES_TYPE RES_ATTR RES_SEMI DUPLICATE
    ------------------------------ ---------------------- -------- -------- -------- -------- ---------
    USING                          5                      N        N        N        N        N
    OUTER                          5                      N        N        N        N        N
    SELECT                         6                      Y        N        N        N        N
    

    You'll get a similar result, if you tried

    select ID, VALUE_A, VALUE_B from TABLE_A using join TABLE_B using (ID);
    

    Kind regards
    Bob

  • difference between Inner Join join natural r

    Experts,

    I'm trying to understand the difference between Natural Join and inner join.

    Inner join: the following query provides 106 lines based on two tables

    SQL > SELECT EMPLOYEE_ID EID, last NAME, DEPT_NAME DEPARTMENT_NAME

    2 EMPLOYEES A, B MINISTRIES

    3. WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID

    4 order employee_id;

    Natural Join: This is the combination or combined result of all the columns in both tables.

    When I run the Sub statement, it combines employees and the table from department of a HR schema and provides only 32 ranks

    SQL > select employe_id, first_name, department_name

    2 departments of NATURAL JOIN employees;


    EMPLOYEE_ID NAME DEPARTMENT_NAME

    ----------- -------------------- -------------------

    Neena 101 Executive

    Lex 102 Executive

    104 Bruce IT

    David 105 HE

    question: why the NATURAL JOIN query omit the employee_id 103 and on what basis it omit a few other employee_id

    Thanks in advance

    Hello

    The main difference is that INNER JOIN is used in real life; NATURAL JOIN is used only in textbooks.

    More rigorously, FRANCKLIN JOIN is an inner join, involving all the columns that have the same name.  The hr.departments and hr.employees have 2 columns with the same name: department_id and manager_id, so

    OF hr.departments d

    NATURAL JOIN e hr.employees

    is equivalent to

    OF hr.departments d

    INNER JOIN hr.employees e ON e.department_id = d.deparment_id

    AND e.manager_id = d.manager_id

    or, using the old join syntax

    OF hr.departments d

    e hr.employees

    WHERE e.department_id = d.deparment_id

    AND e.manager_id = d.manager_id

  • Inner join and 'PIVOT' results

    Good day to all;

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    "CORE 10.2.0.4.0 Production."
    AMT for Solaris: release 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production


    I have 2 paintings I want to do a query on and then "pivot" results. I understand that, since it is 10 g using the PIVOT operator is not supported.
    (Corrected code :)) issues
    create table CT
    (
    DESIGN_ID NUMBER,
    CT_ID VARCHAR2(15)  
    )
    INSERT INTO CT VALUES ('654321','10QWER123456');
    INSERT INTO CT VALUES ('987654','7ASDF654987');
    INSERT INTO CT VALUES ('321654','82CHEV852963');
     
    create table CXRF
    (
    DESIGN_ID NUMBER,    
    ROW_SEQ_NBR NUMBER,    
    XREF_CT_ID VARCHAR2(15)  
    )
    INSERT INTO CXRF VALUES ('654321','1','25ABCD');
    INSERT INTO CXRF VALUES ('654321','2','262ABCD');
    INSERT INTO CXRF VALUES ('987654','1','14WXYZ');
    INSERT INTO CXRF VALUES ('987654','2','34FRED');
    INSERT INTO CXRF VALUES ('321654','1','1TOM');
    -Oops, measure twice cut once...
    select design_id,
            ct_id,
            xref_ct_id
      from cxrf
         INNER JOIN
           ct
       on(ct.design_id = cxrf.design_id) 
    Expected results would look like this:
    DESIGN_ID             CT_ID            XREF_CT_1     XREF_CT_2
       654321       10QWER123456        25ABCD         262ABCD
    Published by: GMoney on 9 may 2013 08:47

    Published by: GMoney on 9 may 2013 08:49

    Thanks for posting examples of data and version, however, as said, it would be better if you can test, DDLS is full of typos.

    Did you visit the FAQ before posting?

    {message: id = 9360005}

    There are also effective methods in previous versions of 11 g data pivot.
    For example:

    SQL> select ct.design_id
      2       , ct.ct_id
      3       , min(case when cxrf.row_seq_nbr = 1 then cxrf.xref_ct_id end) as XREF_CT_1
      4       , min(case when cxrf.row_seq_nbr = 2 then cxrf.xref_ct_id end) as XREF_CT_2
      5  from cxrf
      6       join ct on (ct.design_id = cxrf.design_id)
      7  group by ct.design_id, ct.ct_id ;
    
     DESIGN_ID CT_ID        XREF_CT_1    XREF_CT_2
    ---------- ------------ ------------ ------------
        654321 10QWER123456 25ABCD       262ABCD
        987654 7ASDF654987  14WXYZ       34FRED
        321654 82CHEV852963 1TOM
     
    
  • I would like to convert a subselect to use an inner join?

    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
    I have the SQL below (it gives the correct data) using several sub selects I want to use in a PS query via the PS request handler. However, Manager queries PS will only a sub select when you create a query, but it will allow an inner join. So, I was hoping to rewrite it as a join but can not understand. Perhaps using a view would work? Or if it happens to be expert PS9.1 out there who might know an effcient way more to create the query that would be great as well. I have several versions of different trails of my SQL inner join if someone wishes to see those.
    select business_unit,
           ledger,
           fund_code,
           account,
           foreign_amount,
           open_item_key
      from ps_open_item_gl a
     where (business_unit, ledger, fund_code, account) in
           (select business_unit, ledger, fund_code, account
              from (select business_unit,
                           ledger,
                           fund_code,
                           account,
                           sum(foreign_amount)
                      from ps_open_item_gl b
                     where b.open_item_status = 'O'
                     group by business_unit, fund_code, account, ledger
                    having sum(foreign_amount) = 0))
       and open_item_status = 'O'
       and business_unit not in ('4110', '5301', '6501', '6602')
     order by 1, 2, 3, 4, 6 

    Hello

    956171 wrote:
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
    I have the SQL below (it gives the correct data) using several sub selects I want to use in a PS query via the PS request handler. However, Manager queries PS will only a sub select when you create a query, but it will allow an inner join. So, I was hoping to rewrite it as a join but can not understand.

    I don't know what you mean by "select sub", or what exactly is the PS limitation.
    Are you talking about the query nested in the IN clause? It is not necessary. The following is equivalent to what you posted:

    select business_unit,
           ledger,
           fund_code,
           account,
           foreign_amount,
           open_item_key
      from ps_open_item_gl a
     where (business_unit, ledger, fund_code, account) in
           (
               select    business_unit, ledger, fund_code, account
               from      ps_open_item_gl b
               where     b.open_item_status = 'O'
               and           business_unit  not in ('4110', '5301', '6501', '6602')
               group by  business_unit, ledger, fund_code, account
               having    sum (foreign_amount) = 0
           )
       and open_item_status = 'O'
     order by 1, 2, 3, 4, 6
    

    I move the condition 'and business_unit not in ('4110', '5301', '6501', 6602') ' in the subquery just for efficiiency.

    I think you would need a subquery, even if you don't re - he writes as a join.

    Perhaps using a view would work?

    No doubt. Again, I know not what PS objects to, but you can probably hide in a vision where the PS can complain about this.

  • Update with INNER JOIN

    Hello

    My update with the inner join does not seem to work.

    UPDATE RECAP R SET R.FLAVOR = (SELECT FN. FLAVOR_NDC FN FLAVOR, REPLACE CAP R WHERE R.NDC11 = FN. NDC11)

    When I write the query above, the inner circle question (SELECT FN. FLAVOR_NDC FN FLAVOR, REPLACE CAP R WHERE R.NDC11 = FN. NDC11) returns multiple lines, and it's a new syntax for me (as I was Teradata and SQL server).

    Can you please how this request can be written to make it work?

    I get the error message below

    SQL error: ORA-01427: einreihig subquery returns multiple rows
    01427 00000 - "einreihig subquery returns several lines.

    1. fix your code:

    UPDATE RECAP R SET R.FLAVOR = (SELECT FN.FLAVOR FROM FLAVOR_NDC FN WHERE R.NDC11 = FN.NDC11)
    

    2. you can use the fusion

    merge into RECAP R
    using FLAVOR_NDC FN
    on(R.NDC11 = FN.NDC11)
    when matched then
         update
         set R.FLAVOR = FN.FLAVOR
    

    Kind regards
    Malakshinov Sayan

  • Need help with the ANSI Join syntax

    Version: 11.1.0.7

    I have my select statement that returns 3000 lines and looks like
    select ..
    from tab1 a,tab2 b,tab3 c
    where a.c1=b.c1 and
    b.c2 = c.c2(+)
    and ...
    group by
    ..
    I want to convert it to ANSI SQL, because it must be able to Hibernate, and the developer says that Hibernate include only ANSI SQL. I tried something like
    select ..
    from tab1 a INNER JOIN tab2 b
    ON a.c1=b.c1 
    LEFT OUTER JOIN tab3 c
    ON b.c2=c.c2
    where ...
    group by
    ..
    I felt bad and it turned out to be the same. It returns no rows. Please help with the correct syntax.

    I'm not sure, sorry, because it's certainly the ANSI equivalent to the non-ANSI.

    Would you have examples of queries and data that you can reproduce the problem with, and that we could analyze?

    Published by: Seanmacgc on July 15, 2009 12:48

    If there is no Clause WHERE conditions in the original that are included in the ANSI version of the query in the WHERE Clause for the EXTERNAL table itself joined, then that could transform the JOIN JOIN internally, that is, all the filters on the EXTERNAL joined table should be included in the JOIN predicate - IT (b.c2 = c.2 AND c.c3 = 'X').

  • INNER JOIN vs WHERE (+) rating

    I am trying to determine the best way to write joins in Oracle. I'm a guy from SQL Server, and I'm used to writing

    Select t1.c1 from table1 t1 INNER JOIN table2 t2 ON t1.c1 = t2.c1

    I have been informed that the Oracle compiler prefers the sign (+) rating to join columns in which the clause. I have also been informed that Oracle has deprecated notation plue for INNER JOIN scoring in 10g. What is the real answer?

    I personally never did a comparison of performance between the two.

    I've always preferred the ANSI rating because I think it is more descriptive than the Oracle outer join notation.

  • Must use the nested left join syntax?

    I am currently an editing view. The view has one with a followed left join to an inner join such as this:

    Select
    T1.a,
    T2.b,
    T3.c
    Of
    (select 1 as a Union double all the)
    Select 2 as double
    ) t1
    left join
    (select 1A, 11 b of all the double union)
    Select 2A, 12 b of the double
    ) t2
    on t1.a = t2.a
    Join
    T3 (select 11 b, 14-c double)
    on t2.b = t3.b;
    / * - Try out: -.
    A, B AND C
    ------- ------- -------
    1-11-14
    */

    The inner join converts left join in an inner join "de facto"? should I use nested join syntax in order to get the other ranks:

    Select
    T1.a,
    T2.b,
    T3.c
    Of
    (select 1 as a Union double all the)
    Select 2 as double
    ) t1
    left join
    (select 1A, 11 b of all the double union)
    Select 2A, 12 b of the double
    ) t2
    on t1.a = t2.a
    Join
    T3 (select 11 b, 14-c double)
    on t2.b = t3.b;
    / * - Try out: -.
    A, B AND C
    ------- ------- -------
    1-11-14
    */

    Sorry, just saw your last join condition now:

    on t2.b = t3.b;
    

    This results in a JOIN INTERNAL to the EXTERNAL table JOINED to, will certainly lead to a result of diminshed set - not quite the same that forced an INNER JOIN, but the result is the same, Yes. So there are indeed of equivalence.

    This, however, would not have resulted a reduced resultset:

    on t1.a = t3.a;
    

    But of course it is not possible, since the t3 has a column "a".

  • Join ANSI join VS. Oracle (old join)

    Hello
    I have a request on the old version of join for oracle
    its not take more then 30 dry running, I tried to change to an ANSI join query
    but then he tooks 400 seconds, I would like to know how Oracle is to solve the two querys?
    Im running the application on a different server where the tables.

    These are the querys


    Qry 1 - old join
    Select cp.account_no,
    CPC.id_value Subscr_no,
    cce.Component_id,
    CCE.active_dt,
    CP. Package_instance_id_serv
    Of Cmf_Packages@arborp01 cp,.
    Cmf_Package_Components@arborp01 code of criminal procedure
    cmf_component_elements@arborp01 EAC,
    customer_contracts@arborp01 cc
    Where cp. Package_instance_id = cpc. Package_instance_id
    And cp. Package_instance_id_serv = cpc. Package_instance_id_serv
    And cpc.component_instance_id = cce.component_instance_id
    And cpc.component_instance_id_serv = cce.component_instance_id_serv
    And cce.association_id = cc.tracking_id
    And cpc.Component_id in (60584,90474,90475)
    And the EAC. Package_status = 1
    And the EAC. Inactive_dt is null
    And cce.association_type = 2

    Qry2 - join ANSI
    Select
    CP.account_no,
    CPC.id_value Subscr_no,
    cce.Component_id,
    CCE.active_dt,
    CP. Package_instance_id_serv
    Of Cmf_Packages@arborp01 cp
    Join Cmf_Package_Components@arborp01 cpc
    on (cp. Package_instance_id = cpc. Package_instance_id
    And cp. Package_instance_id_serv = cpc. Package_instance_id_serv)
    Join cmf_component_elements@arborp01 EAC
    on (cpc.component_instance_id = cce.component_instance_id
    And cpc.component_instance_id_serv = cce.component_instance_id_serv)
    Join Customer_contracts@arborp01 cc on cce.association_id = cc.tracking_id
    Where cpc.Component_id in (60584,90474,90475)
    And the EAC. Package_status = 1
    And the EAC. Inactive_dt is null
    And cce.association_type = 2


    An aplogize for my eanglish.
    Thank you

    Do you mean that the remote servers are running 8i?

    8i supported SQL 99 join syntax. So I would be quite surprised if forcing the instance of Oracle 9i local to do joins, representing the query Exchange plan.

    Justin

  • generator 10 I have a query with an inner join... join error unsupported expression! In a query, Report Designer cannot do a join?

    Hey in the 10 Report Builder Query Builder, I got an error:

    OftblFacility INNER JOIN tblStateProvince ON tblFacility.OIDStateProvince = tblStateProvince

    What is java.swl.SQLException: [Macromedia] [SequeLinkJDBC Driver] [ODBC Socket] [Microsoft] [ODBC Microsoft Access driver] Join expression not taken in charge.

    Why?

    Report Designer will automatically join tables only if you have defined a relationship between them in your database.

    In Report Designer, you can easily create a join by dragging a field from one table to the related field in the other table. It couldn't be simpler.

    See you soon

    Eddie

  • How to do an INNER JOIN subquery?

    Can't seem to find anything that could help out me with this.
    My prototype query goes like this but its gives me an ORA-00905: lack of keyword


    SELECT Poker_Site.site_name, bonus.sign_up_bonus
    OF poker_site
    INNER JOIN bonus
    WHERE Poker_site.site_name = 'Poker Stars'
    WE Poker_Site.site_name = bonus.site_name;

    Join INNER JOIN must be immediately followed by ONE and only then, where:

    SELECT Poker_Site.site_name, bonus.sign_up_bonus
    FROM poker_site
    INNER JOIN bonus
    ON Poker_Site.site_name = bonus.site_name
    WHERE Poker_site.site_name = 'Poker Stars';
    

    SY.

  • inner join is not eliminating duplicates!

    Name of the table is customers

    VALUE CITY SIDE NUMS CNAME
    1 2001 Hoffman 100 1001 London
    2 Rome 2002 200 1003 Giovanni
    3 2003 Liu San Jose 200 1002
    4 2004 grass Berlin 300 1002
    5 2006 Clemens 100 1001 London
    6 2008 Cisneros San Jose 300 1007
    7 2007 perish Rome 100 1004

    Query is
    To find all pairs of customers having the same side

    I tried:

    SELECT c.cname, cb.cname FROM customers c INNER JOIN customers cb WE cb.rating AND c.cname = c.rating! = cb.cname

    CNAME CNAME
    1 perish Hoffman
    2 Clemens Hoffman
    3 Liu Giovanni
    4 Giovanni Liu
    5 Cisneros grass
    6 perira Clemens
    7 Hoffman Clemens
    8 grass Cisneros
    Clemens 9 perish
    10 Hoffman perish


    Ideally, it should up to 5 rows, but it does not return these lines for an e.g. Hoffman perish since I'm Perira Hoffman already. Your answers will be appreciated.

    Thank you
    Vaibhav

    Hi, Vaibhav,

    914683 wrote:
    Name of the table is customers

    VALUE CITY SIDE NUMS CNAME
    1 2001 Hoffman 100 1001 London
    2 Rome 2002 200 1003 Giovanni
    3 2003 Liu San Jose 200 1002
    4 2004 grass Berlin 300 1002
    5 2006 Clemens 100 1001 London
    6 2008 Cisneros San Jose 300 1007
    7 2007 perish Rome 100 1004

    Query is
    To find all pairs of customers having the same side

    I tried:

    SELECT c.cname, cb.cname FROM customers c INNER JOIN customers cb WE cb.rating AND c.cname = c.rating! = cb.cname

    Perhaps what you want is

    SELECT      c.cname
    ,         cb.cname
    FROM          customers  c
    INNER JOIN  customers  cb  ON   c.rating  = cb.rating
                                       AND  c.cname       < cb.cname     -- not !=
    ;
    

    CNAME CNAME
    1 perish Hoffman
    2 Clemens Hoffman
    3 Liu Giovanni
    4 Giovanni Liu
    5 Cisneros grass
    6 perira Clemens
    7 Hoffman Clemens
    8 grass Cisneros
    Clemens 9 perish
    10 Hoffman perish

    The title you chose for this thread is "inner join is not eliminating duplicates!
    Inner joins (or other types of joints) are not supposed to remove duplicates. Use SELECT DISTINCT to remove duplicates, which is exactly the same lines.
    You don't have to duplicate. For example, these lines of your result set:

    `        CNAME     CNAME
    1     Perira     Hoffman
    10     Hoffman     Perira
    

    are not identical; Indeed, the two columns are different.

    Ideally, it should up to 5 rows, but it does not return these lines for an e.g. Hoffman perish since I'm Perira Hoffman already.

    Please report the exact output of the sample data you want. (As said by Hoek, post CREATE TABLE and INSERT statements for the sample data too.)

  • Issue of inner joins

    Hi all
    I have a few tables with samples as:
    CREATE OR REPLACE TABLE hotel
    ( 
         hotel_id             int  NOT NULL ,
         hotel_name           varchar2(50)  NULL ,
         hotel_address        varchar2(100)  NULL
    );
    
    CREATE OR REPLACE TABLE people_type
    ( 
         people_type_id       int  NOT NULL ,
         type_des             varchar2(30)  NOT NULL
    );
    
    CREATE OR REPLACE TABLE hotel_people
    ( 
         hotel_id                int  NOT NULL ,
         people_type_id      int  NOT NULL ,
         age_min                int  NULL ,
         age_max               int  NULL
    );
    
    CREATE OR REPLACE TABLE hotel_service
    ( 
         hotel_id              int  NOT NULL ,
         qty_room             int  NOT NULL ,
         qty_people           int  NOT NULL,
         qty_people_extra      int  NOT NULL
    );
    
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(1,'hotel1','hotel1 address1');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(2,'hotel2','hotel2 address2');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(3,'hotel3','hotel3 address3');
    insert into hotel(hotel_id, hotel_name, hotel_address)
    values(4,'hotel4','hotel4 address4');
    
    insert into people_type (people_type_id, type_des)
    values (1,'Baby');
    insert into people_type (people_type_id, type_des)
    values (2,'Young');
    insert into people_type (people_type_id, type_des)
    values (3,'Adult');
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,1,0,2);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,2,3,10);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (1,3,11,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (2,2,3,17);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (2,3,18,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (3,3,18,200);
    
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,1,0,2);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,2,3,10);
    insert into hotel_people (hotel_id, people_type_id, age_min, age_max)
    values (4,3,11,200);
    
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (1,10,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (1,5,4,0);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (2,6,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (2,12,3,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (3,10,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (3,5,4,0);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (4,6,2,1);
    insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra)
    values (4,12,3,1);
    As you can see I have Hotels with basic information, persons of type hotel_people (1:baby, 2:young and 3:Adult), which contains the type of people that each hotel can accommodate with min max age and hotel_service that contains the info from the number of rooms and the amount of people that can be in each room.
    I'm trying to get the records that match my condition as:
    1. I want to get the hotels that can accommodate 4 pieces with adults of 3 for each room.
    2 get hotels that can accommodate 6 rooms 2 where 2 of the bedrooms can have 1 young (3 people per room) and young people 9 years.

    For 1:
    select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max
    from hotel h
    inner join hotel_service ss on h.hotel_id = ss.hotel_id
    inner join hotel_people p on h.hotel_id = p.hotel_id
    inner join people_type t on p.people_type_id = t.people_type_id
    where ss.qty_room >= 4
    and t.people_type_id = 3
    and ss.qty_people >= 3
    For 2:
    select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max
    from hotel h
    inner join hotel_service ss on h.hotel_id = ss.hotel_id
    inner join hotel_people p on h.hotel_id = p.hotel_id
    inner join people_type t on p.people_type_id = t.people_type_id
    where ss.qty_room >= 6
    and ss.qty_people >= 3
    and t.people_type_id in (2,3)
    and 9 between p.age_min and p.age_max
    This does not work, the start of the query becomes more complex if I try to search for multiple rooms with a different number of people and children of different ages.
    Can anyone help with this?
    Thank you

    Published by: user9542267 on April 3, 2012 22:26

    Hello

    user9542267 wrote:
    ... Yes, what I need is a hotel that can accommodate TWO adults and youth. What do you mean by two copies?

    Right now I read something on the Grand Duke by Gilbert. I also have a book with the complete works of Gilbert, so I can leave it open, and when I see a reference in the commentary, I see exactly what the author is talking about in the book. However, when the comment compares some parts of this game to the other plays of Gilbert, The Yeomen of the Guard , it gets hard, because I have to keep changing the pages in the complete works, and I can't see both at the same time. It would be much easier if I could get a full second copy of the work; then I could leave a copy of the Grand Duke and leave the other copy open to The Yeomen of the Guard . This is similar to what you should do with the table of hotel_people, and what I do with the table scott.emp below.

    Imagine (using tables in the scott schema) you have to find the departments where both an analyst and a clerk to work.
    This:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       scott.emp     e  ON     d.deptno     = e.deptno
    WHERE       job          IN ('ANALYST', 'CLERK')
    ORDER BY  d.deptno
    ;
    

    product

    DEPTNO DNAME          LOC
    ------ -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
    

    because all these departments have either an analyst or a clerk. However, only deptno = 20 a time .
    If, instead of a single table of big job, you have a separate table for each Olympic Games, then you might do something like this:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       analyst     a  ON     d.deptno     = a.deptno
    JOIN       clerk          c  ON     d.deptno     = c.deptno
    ORDER BY  d.deptno
    ;
    

    Of course, you don't have tables separated like that, but you could generate result sets who behaved as separte tables, for example:

    WITH     analyst          AS
    (
         SELECT  *
         FROM     scott.emp
         WHERE     job     = 'ANALYST'
    )
    ,     clerk          AS
    (
         SELECT  *
         FROM     scott.emp
         WHERE     job     = 'CLERK'
    )
    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       analyst     a  ON     d.deptno     = a.deptno
    JOIN       clerk          c  ON     d.deptno     = c.deptno
    ORDER BY  d.deptno
    ;
    

    Note that the main request is exactly what I posted earlier.
    What produces the right output:

    DEPTNO DNAME          LOC
    ------ -------------- -------------
        20 RESEARCH       DALLAS
    

    There is no need to actually generate separate result sets, however. You can use two copies of the table scott.emp in the same query, as follows:

    SELECT DISTINCT
              d.*
    FROM       scott.dept     d
    JOIN       scott.emp     a  ON     d.deptno     = a.deptno
    JOIN       scott.emp     c  ON     d.deptno     = c.deptno
    WHERE       a.job          = 'ANALYST'
    AND       c.job          = 'CLERK'
    ORDER BY  d.deptno
    ;
    

Maybe you are looking for