SQL question on natural joins

I am a newbie to SQL and I studied the book of Fundamentals SQL Server Oracle OCA 12 c, but a section on natural joins to confuse me.

Basically, the author says:

SELECT r.region_name, d.department_name, l.city, c.country_name

DEPARTMENTS d

NATURAL JOIN places l, c countries, regions r;

The natural join between DEPARTMENTS and LOCATIONS creates a provisional result, consisting of 27 lines since they are implicitly attached on the column of location_id. This set is then Cartesian-joined to the table of the COUNTRY as a join condition is not implicitly or explicitly specified. 27 interim lines are attached to 25 lines in the COUNTRY table, which gives a new temp results set with 675 (27 × 25) rows and three columns: DEPARTMENT_NAME, CITY and COUNTRY_NAME. This set is then attached to the REGION table. Once more, a Cartesian join occurs because column REGION_ID is absent from any join condition. The final result set contains rows and four columns (675 × 4) 2700.

I can understand because you evaluate joins from the left. But then he wrote:

The JOIN... USE and JOIN... ON the syntaxes are better suited to join multiple tables. The following query joins four tables using the natural join syntax:

SELECT country_id region_id, c.country_name, l.city, d.department_name

DEPARTMENTS d

NATURAL JOIN places l

NATURAL JOIN country c

NATURAL JOIN region r;

This query generates correctly 27 lines in the final results set since the required join columns are listed in the SELECT clause. The following query illustrates how the JOIN... CLAUSE is used to extract the same 27 lines. A join condition can reference columns only in its scope. In the following example, the join of DEPARTMENTS slots can not reference columns in the COUNTRIES or REGIONS of tables, but the join between the COUNTRIES and REGIONS may refer to any column of four tables involved in the query.

This second method of the part of the natural writing joined confuses me. I don't understand the logic behind the 2nd series of States of Natural Join. For me, it seems that the first series and the 2nd set look alike, but they are apparently not.

Can someone tell me the differences?

Thank you!!

Hello

The more important thing to learn more about NATURAL JOIN is: never use it.  If you add new columns, joins can get different results.  I've never heard of someone uisng NATURAL JOIN apart from a manual or a question like yours.

There are a lot of things in Oracle that take the time to learn and are useful.  All the time you spend learning things is better spent on them.

Tags: Database

Similar Questions

  • Natural join of Table to herself out of Cartesian product

    Hello

    With

    CREATE TABLE A)

    an INTEGER,

    b VARCHAR (15).

    c DATE

    );

    INSERT IN A (a, b, c) VALUES (1, 'AAA', SYSDATE);

    INSERT IN A (a, b, c) VALUES (2, 'BBBB', SYSDATE);

    INSERT IN A (a, b, c) VALUES (3, 'CCCCC', SYSDATE);

    CREATE VIEW A1

    AS SELECT * FROM A

    ;

    the query

    SELECT

    *

    A.

    NATURAL JOIN HAS

    ;

    returning nine records - but

    SELECT

    *

    A.

    NATURAL JOIN A1

    ;

    only three - which is what I expected.

    Why the difference?

    Tested in 11g. Where is the documentation pertaining to this particular aspect?

    Thank you!

    Report it as a bug, the correct result, it's the three ranks of result you can go back to giving has an alias, for example:

    SQL > select * a natural join a and b;

    A B               C

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

    1 AAA 15 JULY 15

    2 BBBB 15 JULY 15

    3 CARTER 15 JULY 15

    3 selected lines.

    SQL > select * from a natural join.

    A B               C

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

    1 AAA 15 JULY 15

    1 AAA 15 JULY 15

    1 AAA 15 JULY 15

    2 BBBB 15 JULY 15

    2 BBBB 15 JULY 15

    2 BBBB 15 JULY 15

    3 CARTER 15 JULY 15

    3 CARTER 15 JULY 15

    3 CARTER 15 JULY 15

    9 selected lines.

    Run on 11.2.0.4 - but reproduced on 12.1.0.2

    Concerning

    Jonathan Lewis

  • Incorrect results of natural join producing. I was wondering why.

    If anyones got a second, I have a small question about this bit of code. It seems that he should produce good results, but does not work.

    "Write a query to identify the highest salary paid in each country. It will take by using a subquery in the FROM clause. »

    It is the obvious answer:

    "select max (salary), country_id of."
    (select department_id, location_id, salary, country_id of)
    employees join natural services locations of natural join)
    Country_id group; »

    Here are the results:

    17000 WE
    CA 6000
    10000 UK


    I wrote this instead:

    Select max (salary), country_id of
    (select d.department_id, l.location_id, c.country_id, e.salary, c.country_name
    e employees
    Join departments d on (e.department_id = d.department_id)
    Join places l on (d.location_id = l.location_id)
    Join country c on (l.country_id = c.country_id)
    )
    Country_id group;

    Which produces:

    24000 WE
    13000 CA
    10000 OF
    14000 UK

    (Of course it also works with 'join using (column_name)')

    My answers look correct when passing through the countries/places/employee tables manually - look of the wrong example.

    I guess its something to do with the natural join? Maybe his join tables somewhere on several columns? I would like to understand exactly why the first example is if bad - because I sure hope will teach me more about why avoid natural joins.

    Also, are there ways to improve outcomes, for example if I was watching an average salary? I drew the tables to try to find ways that the records may be left out, and where the outer joins might be useful. The only thing I could think of would be that a Department/location not assigned a place/country respectively. But I'm not an outer join would allow it at all except if I created some NVL2 code to assign new place/country IDs based on other areas - I think that's a bit much for this example.

    Thank you very much

    Nick

    Published by: nick woodward on April 22, 2013 11:10

    Published by: nick woodward on April 22, 2013 11:12

    Hi, Nick.

    Nick woodward wrote:
    If anyones got a second, I have a small question about this bit of code. It seems that he should produce good results, but does not work.

    "Write a query to identify the highest salary paid in each country. It will take by using a subquery in the FROM clause. »

    In fact, a subquery in the FROM clause is not yet useful, at least not for me, and it is certainly not necessary.

    It is the obvious answer:

    "select max (salary), country_id of."
    (select department_id, location_id, salary, country_id of)
    employees join natural services locations of natural join)
    Country_id group; »

    Here are the results:

    17000 WE
    CA 6000
    10000 UK

    I wrote this instead:

    Select max (salary), country_id of
    (select d.department_id, l.location_id, c.country_id, e.salary, c.country_name
    e employees
    Join departments d on (e.department_id = d.department_id)
    Join places l on (d.location_id = l.location_id)
    Join country c on (l.country_id = c.country_id)
    )
    Country_id group;

    Which produces:

    24000 WE
    13000 CA
    10000 OF
    14000 UK

    (Of course it also works with 'join using (column_name)')

    AID is not as bad as NATURAL JOIN, but it's pretty bad. Forget the JOIN... Help me, just like you should forget NATURAL JOIN.

    My answers look correct when passing through the countries/places/employee tables manually - look of the wrong example.

    I guess its something to do with the natural join? Maybe his join tables somewhere on several columns?

    Exactly; There is a column manager_id employees and also in the departments.
    If the obligation is to get the highest salary of all employees in a country, then NATURAL JOIN does not meet the requirements.

    I would like to understand exactly why the first example is if bad - because I sure hope will teach me more about why avoid natural joins.

    Do you need any other reasons?

    Also, are there ways to improve the results

    As I said earlier, you don't need a subquery for this.
    In addition, you must the country table if you want to display the country_name, or if you need to reach the region table. In this problem, all you need is the country_id and you can get that from the communities table, then the following text also gets good results:

    SELECT    MAX (e.salary)     AS max_salary
    ,        l.country_id
    FROM      employees    e
    JOIN      departments  d  ON  d.department_id = e.department_id
    JOIN      locations    l  ON  l.location_id   = d.location_id
    GROUP BY  l.country_id
    ORDER BY  l.country_id
    ;
    

    Whenever a request is for 2 or more tables, it is recommended to describe each column with a table name or alias.

    say if I watched an average salary?

    You can use AVG much lke you use MAX. You can do both in the same query, if you wish; See the example below.

    I drew the tables to try to find ways that the records may be left out, and where the outer joins might be useful. The only thing I could think of would be that a Department/location not assigned a place/country respectively.

    Right; outer joins are useful when you want to get data from the table or not a matching rows in the table b. Departaments that have not been allocated a location is a situation that calls for an outer join. Another example is the country who have no departments in them. (There is a real possibility. You can set up a table with all countries in the world already in it, not because you'll need all of them, but because you might need one of them.)

    But I'm not an outer join would allow it at all except if I created some NVL2 code to assign new place/country IDs based on other areas - I think that's a bit much for this example.

    Not necessarily. Sometimes all you need is the NULL that you get automatically for all columns of table b when you say

    FROM             a
    LEFT OUTER JOIN  b  ...
    

    and one has no corresponding row in the b

    For example, if you want to include all countries in the table of localities, with the salary maximum and average of those that have employees, you can get these results:

    MAX_SALARY AVG_SALARY CO
    ---------- ---------- --
                          AU
                          BR
         13000       9500 CA
                          CH
                          CN
         10000      10000 DE
                          IN
                          IT
                          JP
                          MX
                          NL
                          SG
         14000 8885.71429 UK
         24000 5064.70588 US
    

    without using NVL, or NVL2 or something like that. In fact, only the functions you need are AVG and MAX.
    Try it.

    Published by: Frank Kulash on April 22, 2013 16:35

  • the two equii join and natural join are equall.will both display the output of the same

    the two equii join and natural join are equall.will both display even

    output?

    Hello
    It keeps you a little test and check yourself?

    See the link below.

    http://psoug.org/reference/joins.html

    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    
    SQL> CREATE TABLE parents (
      2  person_id  NUMBER(5),
      3  adult_name VARCHAR2(20),
      4  comments   VARCHAR2(40))
      5  PCTFREE 0;
    
    Table created.
    
    SQL>
    SQL> CREATE TABLE children (
      2  parent_id    NUMBER(5),
      3  person_id    NUMBER(5),
      4  child_name   VARCHAR2(20),
      5  comments     VARCHAR2(40))
      6  PCTFREE 0;
    
    Table created.
    
    SQL>
    SQL> INSERT INTO parents VALUES (1, 'Dan', 'So What');
    
    1 row created.
    
    SQL> INSERT INTO parents VALUES (2, 'Jack', 'Who Cares');
    
    1 row created.
    
    SQL> INSERT INTO children VALUES (1, 2, 'Anne', 'Who Cares');
    
    1 row created.
    
    SQL> INSERT INTO children VALUES (1, 1, 'Julia', 'Yeah Right');
    
    1 row created.
    
    SQL> INSERT INTO children VALUES (2, 1, 'Marcella', 'So What');
    
    1 row created.
    
    SQL> COMMIT;
    
    Commit complete.
    
    SQL>
    SQL> SELECT adult_name, child_name
      2  FROM parents NATURAL JOIN children;
    
    ADULT_NAME           CHILD_NAME
    -------------------- --------------------
    Jack                 Anne
    Dan                  Marcella
    
    SQL> select adult_name,child_name from parents a, children b
      2  where a.person_id=b.person_id;
    
    ADULT_NAME           CHILD_NAME
    -------------------- --------------------
    Jack                 Anne
    Dan                  Julia
    Dan                  Marcella
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  select adult_name,child_name from parents a, children b
      2* where a.person_id=b.parent_id
    SQL> /
    
    ADULT_NAME           CHILD_NAME
    -------------------- --------------------
    Dan                  Anne
    Dan                  Julia
    Jack                 Marcella
    
    SQL>
    

    Kind regards
    Avinash

  • NATURAL JOIN problem

    Hello

    After reading a natural join on wikipedia , I was interested to use this syntax for expressing equi-joins between the tables that share some common column names. I have so these 3 tables:

    -Courses (acronym, title, nbCredits)
    -Registration (acronym, garnish, matricule, numSect, cumulative, noteFinale)
    -Nobody (SIN, name, typPers, service number, pmatricule)

    When I specify * in the select clause, I don't get that 3 lines, which is logical given the restriction I've specified in where clause. But what I don't understand is that if I replace the * with specific column names, then oracle returns a whole bunch of additional lines that should have been rejected in the where clause. It seems to me that Oracle should return only the next 3 rows. Does make sense to anyone? Here's the query I am running:

    SELECT *.
    OF course, NATURAL JOIN, NATURAL JOIN registration person
    WHERE name = 'John '.
    AND name = "Doe".
    AND nbCredits > 2
    AND noteFinale <>'F'
    AND noteFinale IS NOT NULL;

    -3 lines selected (right)

    Individual SELECTION, the title
    OF course, NATURAL JOIN, NATURAL JOIN registration person
    WHERE name = 'John '.
    AND name = "Doe".
    AND nbCredits > 2
    AND noteFinale <>'F'
    AND noteFinale IS NOT NULL;

    -80 (bad) selected lines

    Ludovic

    Published by: user3968717 on December 5, 2009 12:41

    Published by: user3968717 on December 5, 2009 12:42

    I can't reproduce the problem - I get 1 row in both cases

    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT sigle, titre
      2  FROM Cours NATURAL JOIN Inscription NATURAL JOIN Personne
      3  WHERE prenom='Pierre'
      4  AND nom='Daigneault'
      5  AND nbCredits>2
      6  AND noteFinale != 'F'
      7* AND noteFinale IS NOT NULL
    SQL> /
    
    SIGLE
    ------------
    TITRE
    --------------------------------------------------------------------------------
    
    CY130
    Clavardage et communications
    
    1 row selected.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT *
      2  FROM Cours NATURAL JOIN Inscription NATURAL JOIN Personne
      3  WHERE prenom='Pierre'
      4  AND nom='Daigneault'
      5  AND nbCredits>2
      6  AND noteFinale != 'F'
      7* AND noteFinale IS NOT NULL
    SQL> /
    
     MATRICULE SIGLE
    ---------- ------------
    TITRE
    --------------------------------------------------------------------------------
    
     NBCREDITS TRIM    NUMSECT  CUMULATIF NO        NAS
    ---------- ---- ---------- ---------- -- ----------
    NOM
    --------------------------------------------------------------------------------
    
    PRENOM
    --------------------------------------------------------------------------------
    
    T PMATRIC
    - -------
       1470609 CY130
    
    ...
    
     MATRICULE SIGLE
    ---------- ------------
    TITRE
    -------------------------------------------------------------------------------
    
     NBCREDITS TRIM    NUMSECT  CUMULATIF NO        NAS
    ---------- ---- ---------- ---------- -- ----------
    NOM
    -------------------------------------------------------------------------------
    
    PRENOM
    -------------------------------------------------------------------------------
    
    T PMATRIC
    - -------
    
    1 row selected.
    

    I use 11.1.0.7, what version are you using? He had a number of bugs with joins SQL 99 when they were introduced. Maybe you're using an older version? Did you ask the most recent Group of patches for your version of Oracle?

    I would caution, however, I would strongly, strongly suggest avoiding natural joins. In my mind, they are an abomination of SQL syntax. I love the syntax of SQL 99 and I'm perfectly happy with the syntax

    x JOIN y USING (column_name )
    

    or

    x JOIN y ON (x.column_name = y.column_name)
    

    But join based on all the identical column names causes a lot of problems in maintenance. With any other join syntax, if you si vous allez are going to add new columns to a table, the results of your SQL queries do not change. If someone decides that they must add columns to one of your tables in the future (CREATED_BY, LAST_MODIFIED_DATE, etc.), your queries suddenly start returning incorrect data. You waive any reasonable chance to make a reasonable assessment.

    Justin

  • problems with natural join

    Hello!

    I learn SQL and I have a lot of doubts but I think with this example I can generalize.

    I have the tables:

    Book {idbook (PK), namebook}
    auhor {idauthor (PK), nameauthor, idcountry (FK)}
    fatherhood {idauthor (FK), idbook (FK)} (both the same strain of PK)
    country {idcountry (PK), namecountry}

    I want the name of the books that are the authors of the Canada.

    I assumed that a correct query would be:

    SELECT namebook FROM book NATURAL JOIN (paternity NATURAL JOIN (author country NATURAL JOIN)) WHERE country.namecountry = 'Canada ';

    The result I was waiting for was:

    Book3
    Book5

    but this query returns me all the books that have relationships in the co-author (with the authors of all countries), 2 times! as:

    Book2
    book3
    Book4
    Book5
    Book2
    book3
    Book4
    Book5

    the best I can do to get my correct result is:

    SELECT namebook FROM book NATURAL JOIN (author, NATURAL JOIN) WHERE author.idcountry = 2;

    But of course I can't use this one...

    Can someone explain to me what is happening?

    Thank you very much!

    Published by: user12040235 on 10/15/2009 09:37

    Published by: user12040235 on 10/15/2009 09:51

    Hello

    Maybe it's a bug.

    Get the correct results (2 ranks) to Oracle 10.1, but I get the same bad results you (12 rows) in Oracle 11.1.
    In Oracle 11, I get results so say "SELECT *" instead of "SELECT book.namebook".
    I also get the correct results, if I add the join columns in the SELECT clause. Adding a column to join no, for example

    SELECT  BOOK.namebook, author.nameauthor
    FROM            book
    ...
    

    Gets results.

    In the interest of all those who want to try this:

    DROP TABLE     author;
    
    create table     author     AS
    SELECT  1 AS idauthor, 'Jose Luiz do Rego' AS nameauthor, 1 AS idcountry     FROM dual     UNION ALL
    SELECT         2, 'Barbara Bela',                                      2          FROM dual     UNION ALL
    SELECT         3, 'Juan Domingues',                                    5          FROM dual     UNION ALL
    SELECT         4, 'José Mauro de Vasconcelos',                         1          FROM dual     UNION ALL
    SELECT         5, 'Vader',                                             2          FROM dual     UNION ALL
    SELECT         6, 'navathe',                                           4          FROM dual     UNION ALL
    SELECT         7, 'Machado de Assis',                                  1          FROM dual
    ;
    
    drop table     AUTHORSHIP;
    
    CREATE TABLE     authorship     AS
    SELECT         2 AS idauthor,          5 AS idbook     FROM dual     UNION ALL
    SELECT         1 AS idauthor,          1 AS idbook     FROM dual     UNION ALL
    SELECT         5 AS idauthor,          3 AS idbook     FROM dual     UNION ALL
    SELECT         6 AS idauthor,          2 AS idbook     FROM dual     UNION ALL
    SELECT         7 AS idauthor,          4 AS idbook     FROM dual     UNION ALL
    SELECT         7 AS idauthor,          6 AS idbook     FROM dual;
    
    drop table     book;
    
    CREATE TABLE   book     AS
    SELECT         1 AS idbook, 'book1' AS namebook     FROM dual     UNION ALL
    SELECT         2 AS idbook, 'book2' AS namebook     FROM dual     UNION ALL
    SELECT         3 AS idbook, 'book3' AS namebook     FROM dual     UNION ALL
    SELECT         4 AS idbook, 'book4' AS namebook     FROM dual     UNION ALL
    SELECT         5 AS idbook, 'book5' AS namebook     FROM dual     UNION ALL
    SELECT         6 AS idbook, 'book6' AS namebook     FROM dual     UNION ALL
    SELECT         7 AS idbook, 'book7' AS namebook     FROM dual;
    
    DROP TABLE     country;
    
    CREATE TABLE     country     AS
    SELECT         1 AS idcountry, 'Brazil' as namecountry     FROM dual     UNION ALL
    SELECT         2 AS idcountry, 'Canada' as namecountry     FROM dual     UNION ALL
    SELECT         3 AS idcountry, 'Chile' as namecountry     FROM dual     UNION ALL
    SELECT         4 AS idcountry, 'Venezuela' as namecountry    FROM dual     UNION ALL
    SELECT         5 AS idcountry, 'USA' as namecountry          FROM dual     UNION ALL
    SELECT         6 AS idcountry, 'Argentina' as namecountry    FROM dual     ;
    
  • Confusion about natural join

    In the HR schema, to the natural join done on employees of three tables, different places, that no row is returned and Department based on the columns in the select list

    For the query

    Select country_id, location_id, department_id as well as wages of employees
    departments of natural join
    locations of natural join

    32 rows are to be extracted.

    Time of the request

    Select the salary, department_id from employee
    departments of natural join
    locations of natural join

    736 lines are retrieved.

    Please explain how the name of column in select affect the natural join of the list

    Hello

    suchibm wrote:

    Hi frank,.

    Thanks for the advice, but it's accreditation. I use Oracle XE Databae.

    But which version?   If you are unsure, run

    SELECT *.

    SINCE the release of v$.

    The important part will be the bunch of numbers towards the end of the 1st line.

    This looks like a bug has been fixed in version 11.2.0.2.0 (or arose after that version).

    A book of preparation of certification (or something like that), said you should get different results of these 2 queries?

    If not, ignore the result 736 set line; 32 is the expected number of rows.

    Just curious, when you get 736 lines, they are 23 copies of the same 32 ranks that you get in the query on the other?  Use COUNT (*) in a GROUP BY query to find out.

    It's a shame that certification documents include such useless things as NATURAL JOIN.  While you are studying for certification, invest your time in something that you can use in real life.  There must be other subjects that you don't fully understand and do not have bugs in your version.

  • Beginner help - faced with natural join as an example

    Hello

    My apologies for how basic this query probably will, but I am teaching from zero and do not like to move from topic to topic without a good understanding of why I get the results I get, rather than knowing 'it's how he did

    I use the guide of the official examination for 1z0 - 051, and 320 page there is the following year:

    + "The table JOB_HISTORY sharing three columns with the same named with the EMPLOYEES table: EMPLOYEE_ID JOB_ID and department_id." You are required to describe the tables and fetch the EMPLOYEE_ID, JOB_ID, +.
    + Values for all rows retrieved using a natural join pure department_id, LAST_NAME, HIRE_DATE and end_date. Alias the table EMPLOYEES as EMP and the table JOB_HISTORY that JH and use dot notation where possible. » +


    Their solution (which is about what I came to start with) is:

    SELECT employee_id job_id, department_id, emp.last_name, emp.hire_date, JH.end_date
    OF JH job_history
    NATURAL JOIN employees emp;

    This translates into the only employee "Taylor" being returned.

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

    Is it possible someone could 'hold my hand' through this example please? I have no idea why only one result is returned, but in addition, I don't know what else I expected - clearly my understanding of the natural join clause is severely lacking. The book States that:

    + "The execution of this statement returns a single row with the same values EMPLOYEE_ID JOB_ID and department_id in the two tables." +

    I guess I'm confused because I thought that the join clauses should deploy additional content to other tables to display, to not limit lines - which is what it sounds like they mean by "returns a single row with the same number and job_id department_id values in the two tables."



    I am very confused!

    Thanks in advance,

    Nick

    Hi, Nick.

    Nick woodward wrote:
    Hello

    My apologies for how basic this query probably will, but I am teaching from zero and do not like to move from topic to topic without a good understanding of why I get the results I get, rather than knowing 'it's how he did

    Good thinking!

    I use the guide of the official examination for 1z0 - 051, and 320 page there is the following year:

    + "The table JOB_HISTORY sharing three columns with the same named with the EMPLOYEES table: EMPLOYEE_ID JOB_ID and department_id." You are required to describe the tables and fetch the EMPLOYEE_ID, JOB_ID, +.
    + Values for all rows retrieved using a natural join pure department_id, LAST_NAME, HIRE_DATE and end_date. Alias the table EMPLOYEES as EMP and the table JOB_HISTORY that JH and use dot notation where possible. » +

    Their solution (which is about what I came to start with) is:

    SELECT employee_id job_id, department_id, emp.last_name, emp.hire_date, JH.end_date
    OF JH job_history
    NATURAL JOIN employees emp;

    This translates into the only employee "Taylor" being returned.

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

    Is it possible someone could 'hold my hand' through this example please? I have no idea why the only one result is returned,

    This is because there is only one combination of rows from the tables emp and jh such as the join condition is TRUE. In a natural join, the join condition is not specified, so it would be better if you started looking at the equivalent natural join:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    ,       emp.hire_date
    ,       jh.end_date
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
                        AND  emp.job_id          = jh.job_id
                        AND  emp.department_id  = jh.department_id
    ;
    

    on the other hand I don't know what I was expecting - clearly my understanding of the natural join clause is severely lacking. The book States that:

    + "The execution of this statement returns a single row with the same values EMPLOYEE_ID JOB_ID and department_id in the two tables." +

    I guess I'm confused because I thought that the join clauses should deploy additional content to other tables to display, do not limit the rows...

    A join does both. Each row in the result set can have columns (or drift) the columns in the table and it can have any row of a table associated with the lines of all (or part) of the other table. There are 107 lines in the emp table and 10 rows in the table of jh. This means that could result in a join 2 tables can 0-107 * 10 = 1070 lines. If the join conditions are so strict that no combination of lines met, then the result set has 0 rows. However, if they are so loose that all combinations of lines 2 brands joins tables condition TRUE, then each of the 107 rows in the emp table will join each of the 10 lines in the table of jh.

    Try to play with the join condition and see what happens. For example, we will comment on 2 of the 3 parts of the join condition, so that the only condition on the left is "emp.employee_id = jh.employee_id". Also, we'll include a few other columns in the table of jh and no poster not dates, just to make room for the new coplumns. So, if we execute this query:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    -- ,       emp.hire_date          -- Commented out to save space
    -- ,       jh.end_date          -- Commented out to save space
    ,        jh.job_id             -- For debugging
    ,      jh.department_id     -- For debugging
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
    --                    AND  emp.job_id          = jh.job_id
    --                    AND  emp.department_id  = jh.department_id
    ;
    

    then we get these results:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         101 AD_VP              90 Kochhar    AC_ACCOUNT        110
         101 AD_VP              90 Kochhar    AC_MGR            110
         102 AD_VP              90 De Haan    IT_PROG            60
         114 PU_MAN             30 Raphaely   ST_CLERK           50
         122 ST_MAN             50 Kaufling   ST_CLERK           50
         176 SA_REP             80 Taylor     SA_REP             80
         176 SA_REP             80 Taylor     SA_MAN             80
         200 AD_ASST            10 Whalen     AD_ASST            90
         200 AD_ASST            10 Whalen     AC_ACCOUNT         90
         201 MK_MAN             20 Hartstein  MK_REP             20
    
    10 rows selected.
    

    Why do we get only 10 lines? There are other employee_ids, such as 100 and 206 in the emp table. Why do we see these employee_ids in the results? In addition, the emp.employee_id is unique; in other words, no 2 rows in the emp table have the same employe_id. How the result set can have two lines with the same employee_ids, such as 101 or 200?

    Now try to do the more difficult condtion join, by a non-commenting on the terms that we have commented earlier:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    -- ,       emp.hire_date
    -- ,       jh.end_date
    ,        jh.job_id
    ,      jh.department_id
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
                        AND  emp.job_id          = jh.job_id     -- *** CHANGED  ***
    --                    AND  emp.department_id  = jh.department_id
    ;
    

    The results are now:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         200 AD_ASST            10 Whalen     AD_ASST            90
         176 SA_REP             80 Taylor     SA_REP             80
    

    He was 2 of 10 lines in the previous result set. What happened to the other 8 lines that have been in this result set? Fior example, why has this line:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         176 SA_REP             80 Taylor     SA_MAN             80
    

    in the result of 10 rows value (produced by the query with a join condition only 1), but not in the result of 2 ranks (produced by the query with the additional condition 'AND emp.job_id = jh.job_id')?

    If you do not completely understand the natural joins, don't worry too much. In practice, nodody uses a natural join. However, natural joins are a type of INNER join and inner joins are extremely frequent. It is very important for you to understand how joins inside, such as the 2 I posted more top, work.

    Published by: Frank Kulash, March 18, 2013 17:08
    Results and applications added

  • Why FULL NATURAL JOIN work?

    Select * from version of v$.
    Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production

    Why these constructions are analysed with success and full/cross/left are ignored?
    Select * from
    (select 1 of the double)
    natural full join
    (select 2 from two);

    Select * from
    (select 1 of the double)
    natural left join
    (select 2 from two);

    Select * from
    (select 1 of the double)
    right to natural join
    (select 2 from two);

    Select * from
    (select 1 of the double)
    cross the natural join
    (select 2 from two);

    The same time if we try to alias tables it gets an error:
    Select * from
    (select double 1) t1
    natural full join
    (select double 2) t2;

    ORA-00905: lack of keyword
    00905 00000 - 'lack the key word'
    * Cause:
    * Action:
    Error on line: column 5: 29

    Published by: Slobodcicov on November 28, 2012 23:46

    Try this...

    select * from
    (select 1 a from dual) t1
    natural full join
    (select 2 a from dual) t2;
    

    A
    --
    2
    1

    See you soon,.
    Manik.

  • the basic sql question

    Hi all I have a basic sql question
    Watch below two querries
    1.  select 1 from dual where 1 in (select 1 from dual union all select null from dual) 
    
    It gives output  as 1
    
    but below one 
    
    2.   select 1 from dual where 1 not in  (select 2 from dual union all select null from dual)
    
    It gives output as no data found 
    I think as operator will not compare with all the values, but not the outcome it will compare with all values... When comparing with the value null, the result is automatically null

    I'm wrong
    Please help me on this

    and why performance wise in operator is more better than no of?

    Thanks to all in advance

    Thanks for posting your explain plan command

    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 3249215828
    
    -----------------------------------------------------------------------------
    | Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    -----------------------------------------------------------------------------
    |   0 | SELECT STATEMENT |          |     2 |     6 |     4   (0)| 00:00:01 |
    |   1 |  NESTED LOOPS    |          |     2 |     6 |     4   (0)| 00:00:01 |
    |   2 |   FAST DUAL      |          |     1 |       |     2   (0)| 00:00:01 |
    |   3 |   VIEW           | VW_NSO_1 |     2 |     6 |     2   (0)| 00:00:01 |
    |   4 |    SORT UNIQUE   |          |     2 |       |     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 |
    -----------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       7 - filter(NULL IS NOT NULL)
    
    02:12:54 SQL> select 1 from dual where 1 not in  (select 2 from dual union all select null from dual);
    Elapsed: 00:00:00.01
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 3291682568
    
    -----------------------------------------------------------------
    | Id  | Operation        | Name | Rows  | Cost (%CPU)| Time     |
    -----------------------------------------------------------------
    |   0 | SELECT STATEMENT |      |     1 |     4   (0)| 00:00:01 |
    |*  1 |  FILTER          |      |       |            |          |
    |   2 |   FAST DUAL      |      |     1 |     2   (0)| 00:00:01 |
    |   3 |   UNION-ALL      |      |       |            |          |
    |*  4 |    FILTER        |      |       |            |          |
    |   5 |     FAST DUAL    |      |     1 |     2   (0)| 00:00:01 |
    |   6 |    FAST DUAL     |      |     1 |     2   (0)| 00:00:01 |
    -----------------------------------------------------------------
    
  • A curious "anomaly" with the NATURAL JOIN compared to join HER for HELP

    The following query returns I expect since the HR [106 lines] schema:

    Select last_name, department_name of departments who JOIN employees USE (department_id);

    However, when I do the full natural join with this, I get only 32 ranks.

    Select last_name, department_name of the departments of NATURAL JOIN employees;

    The complete NATURAL JOIN not use department_id join on? But if not what else would it use? I just stumbled on this and am especially curious that I do not use the syntax of natural join in production but prefer instead the JOIN now.

    Thank you.

    The NATURAL keyword indicates that a natural join is underway. A natural join is based on all of the columns in the two tables with the same name. He selects the rows of two tables that have equal values in the corresponding columns.

  • Apex SQL question - weeks of the month

    I have a sql question.
    I want to create a dynamic list.

    If the user select may2009, then I want a dynamic list of show

    04/27/2009-05/03/2009
    05/04/2009-05/10/2009
    05/11/2009-05/17/2009
    05/18/2009-05/24/2009
    05/25/2009-05/31/2009

    If the user selects Jun 2009, then the list will be
    06/01/2009-06/07/2009
    06/08/2009-06/14/2009
    06/15/2009-06/21/2009
    06/22/2009-06/28/2009
    06/29/2009-07/05/2009

    Thank you.

    Using this SQL statement, you can get this list:

    SELECT w_start || ' - ' || w_end d, w_start r
      FROM (SELECT     (week_start_list + (LEVEL - 1) * 7) + 1 w_start,
                       week_start_list + (LEVEL) * 7 w_end
                  FROM (SELECT TO_CHAR (TRUNC (TO_DATE (:my_date, 'dd.mm.yyyy'),
                                               'mm'
                                              ),
                                        'IW'
                                       ) week_begin,
                               TRUNC (TO_DATE (:my_date, 'dd.mm.yyyy'),
                                      'mm'
                                     ) m_begin,
                               TO_CHAR
                                  (TRUNC (TO_DATE (:my_date, 'dd.mm.yyyy'), 'mm'),
                                   'd'
                                  ) day_month_begin,
                               TO_CHAR
                                  (TRUNC (ADD_MONTHS (TO_DATE (:my_date,
                                                               'dd.mm.yyyy'
                                                              ),
                                                      1
                                                     ),
                                          'mm'
                                         ),
                                   'IW'
                                  ) week_end,
                               TRUNC (ADD_MONTHS (TO_DATE (:my_date, 'dd.mm.yyyy'),
                                                  1
                                                 ),
                                      'mm'
                                     ) m_end,
                               TO_CHAR
                                  (TRUNC (ADD_MONTHS (TO_DATE (:my_date,
                                                               'dd.mm.yyyy'
                                                              ),
                                                      1
                                                     ),
                                          'mm'
                                         ),
                                   'd'
                                  ) day_month_end,
                                 TRUNC (TO_DATE (:my_date, 'dd.mm.yyyy'),
                                        'mm'
                                       )
                               - TO_NUMBER
                                          (TO_CHAR (TRUNC (TO_DATE (:my_date,
                                                                    'dd.mm.yyyy'
                                                                   ),
                                                           'mm'
                                                          ),
                                                    'd'
                                                   )
                                          ) week_start_list,
                                 TRUNC
                                    (ADD_MONTHS (TO_DATE (:my_date, 'dd.mm.yyyy'),
                                                 1
                                                ),
                                     'mm'
                                    )
                               + TO_NUMBER
                                    (TO_CHAR
                                        (TRUNC
                                              (ADD_MONTHS (TO_DATE (:my_date,
                                                                    'dd.mm.yyyy'
                                                                   ),
                                                           1
                                                          ),
                                               'mm'
                                              ),
                                         'd'
                                        )
                                    )
                               - 1 week_end_list
                          FROM DUAL)
            CONNECT BY LEVEL <=
                          (SELECT   TO_NUMBER
                                       (TO_CHAR
                                           (TRUNC
                                               (ADD_MONTHS (TO_DATE (:my_date,
                                                                     'dd.mm.yyyy'
                                                                    ),
                                                            1
                                                           ),
                                                'mm'
                                               ),
                                            'IW'
                                           )
                                       )
                                  - TO_NUMBER
                                          (TO_CHAR (TRUNC (TO_DATE (:my_date,
                                                                    'dd.mm.yyyy'
                                                                   ),
                                                           'mm'
                                                          ),
                                                    'IW'
                                                   )
                                          )
                             FROM DUAL))
    

    It is a question of SQL and has nothing to do with the Apex.

    Denes Kubicek
    -------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    -------------------------------------------------------------------

  • SQL * PLUS connects with SQL &gt; question

    People,

    I'm confused on the command. / sqlplus with Oracle database.

    According to my understanding,. / sqlplus connected with SQL > using option 3: sysdba, sysoper, sysasm.

    I connect with SQL > to run the rel853.sql script to create a table PSOPRDEFN but a field "OPERPSWDSALT" did not appear in the table PSOPRDEFN although it is in the Create Table statement.

    I connect with SQL * in the directory/home/user/OracleDB_Home/bin as below:


    $ export SYSTEM_PASS = AccessId/mypass

    $ export ORACLE_HOME = / home/user/OracleDB_Home

    $ export ORACLE_SID = HRCS90

    $. / lsnrctl start LISTENER

    $. / AccessId/mypass sqlplus as sysdba

    SQL > startup

    SQL > @/opt/PT8.53/scripts/rel853.sql

    SQL > select OPERPSWDSALT in the AccessId.PSOPRDEFN;

    It returns: "OPERPSWDSALT": invalid identifier.


    I checked the table PSOPRDEFN that the OPERPSWDSALT field did not appear in the table PSOPRDEFN.

    I tried the sysoper option as below:

    $. / sqlplus AccessId/mypass as sysoper

    SQL > startup

    SQL > @/opt/PT8.53/scripts/rel853.sql

    SQL > select OPERPSWDSALT in the AccessId.PSOPRDEFN;

    It returns: "OPERPSWDSALT": invalid identifier.

    The sysoper error is the same thing with sysdba error.

    I tried the sysasm option as below:

    $. / sqlplus AccessId/mypass as sysasm

    He returned: connection refused.


    Someone told me this connection as SYS causes this error. If not a sysdba, sysoper, or sysasm, use the command. / sqlplus AccessId/mypass cannot connect with SQL >.


    My question is:


    First of all, why is what OPERPSWDSALT did not appear in the PSOPRDEFN table while it is in the CREATE TABLE statement?


    Seocond, if not use SYS which is one of the 3 options, how to run the command. / sqlplus AccessId/mypass to connect with SQL > so that CREATE TABLE PSOPRDEFN correctly?

    Thank you.

    user8860348 wrote:

    People,

    Hello. Thanks much for the reply. I just do the commands below:

    $ export SYSTEM_PASS = AccessId/mypass

    $ export ORACLE_HOME = / home/user/OracleDB_Home

    $ export ORACLE_SID = HRCS90

    $. / lsnrctl start LISTENER

    $. / AccessId/mypass sqlplus as sysdba

    SQL > show user;

    Its release: the USER is "SYS".

    SQL > connect AccessId/mypass

    Its output:

    Error: ORA - 01034:ORACLE not available

    ORA-27101: shared memory realm does not exist

    64 - Linux_x86 error: no such file or directory.

    As we see above, unable to connect to the Oracle database AccessId.

    My question is:

    What to do on AccessId, so that it can connect to the Oracle database?

    Thank you.

    so much for the use that you refuse to actually use COPY it PASTE & so that we can see the whole session.

    In the past, you did

    > SQL > startup

    Maybe the database is out of order & must be started.

    do exactly as below (line by line)

    ID

    sqlplus

    / as sysdba

    startup

    connect AccessId/mypass

    COPY the results from above then PASTE all back here

  • SQL question: horizontally to group by

    Hello
    I have a problem to find a way to display the result of a join table. Can someone please help?


    SQL > select * from tab1;

    GNAME SNO
    ---------- ----------
    ABC 5
    DEF 2
    ABC 3

    SQL > select * from tab2.

    GNAME SNO
    ---------- ----------
    ABC 10
    XYZ 5
    DEF 15



    My output using a UNION ALL operation current:

    SQL > select gname, sno1 count (*) Group of tab1 by gname
    2 Union all the
    3 select gname, sno2 count (*) of the tab2 group by gname.

    SNO1 GNAME
    ---------- ----------
    ABC 2
    DEF 1
    ABC 1
    DEF 1
    XYZ 1



    The output I expect:

    GNAME SNO1 SNO2
    ---------- ----------     ----------
    1 2 ABC
    1 1 DEF
    0 1 XYZ



    Thanks in advance.

    Way the easiest would be to encapsulate a slightly modified version of your query in an external as query:

    select gname, sum(sno1) sno1, sum(sno2) sno2
    from (select gname, count(*) sno1 0 sno2
          from tab1
          group by gname
          union all
          select gname, 0 sno1, count(*) sno2
          from tab2
          group by gname);
    

    John

  • running of SQL question

    Hi expert,


    When I ran after SQL, error message reads "table or view does not exist" pointing to the table "dba_tab_cols" and "dba_all_tables". There is no other question for this statement, because if I changed table "user_tab_cols" and "user_all_tables", it works well.


    declare
    v_old_table DBA_tab_columns.table_name%type;
    v_where Varchar2 (4000);
    Boolean v_first_col: = true;
    type rc is ref cursor;
    c rc;
    v_rowid varchar2 (20);
    Val varchar2 (50): = "Test note";

    Start
    for r in)
    Select
    t.*
    Of
    dba_tab_cols t, dba_all_tables a
    where t.table_name = a.table_name
    and t.data_type like '% CHAR % '.
    and a.owner = 'QA'
    order by t.table_name loop)

    If v_old_table is null then
    v_old_table: = r.table_name;
    end if;

    If v_old_table <>r.table_name then
    v_first_col: = true;

    -dbms_output.put_line ('search' | v_old_table);

    Open c for ' select rowid from ' ' |. ' v_old_table | '" ' || v_where;

    extract the c in v_rowid;
    loop
    When the output c % notfound;
    dbms_output.put_line (' rowid: ' | v_rowid |) "in" | v_old_table);
    extract the c in v_rowid;
    end loop;

    v_old_table: = r.table_name;
    end if;

    If v_first_col then
    v_where: = 'where ' | r.column_name | "as" %' | Val | '%''';
    v_first_col: = false;
    on the other
    v_where: = v_where | "or" | r.column_name | "as" %' | Val | '%''';
    end if;

    end loop;
    end;


    But if I choose these DBA tables in the toad sql Editor, it works well, I am currently using my own credentials, not the administrator credentials. why he get different effects running in these two ways?

    Thank you very much

    >

    Hello

    When I ran after SQL, error message reads "table or view does not exist" pointing to
    Table 'dba_tab_cols' and 'dba_all_tables '. There is no other question for this statement, because

    If I changed table "user_tab_cols" and "user_all_tables", it works well.
    But if I choose these DBA tables in the toad sql Editor, it works well, I am currently using my own
    credentials, not the administrator credentials. why he get different effects running in these two ways?

    You answered your own question - you know that you have the administrator privileges when you
    Open a session under your own credentials - but your id user obviously does TOAD.

    You use SQL * for the query that fails?

    BTW, you do not give us your version of Oracle - you must always tell us what it is

    Please read the forum FAQ and also the thread "sticky" by BluShadow at the top of the list of positions
    on the forum homepage. These forums are an excellent resource - you will get the best out of them if
    you follow the instructions.

    HTH,

    Paul...

    Published by: Paulie July 24, 2012 16:40

Maybe you are looking for