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

Tags: Database

Similar Questions

  • Have a computer dell laptop. replaced the hard drive; tried to install my Windows XP and my product key is not accepted. I was wondering why and how do to accomplish this task.

    My dell laptop had the hard drive replaced.  My son, who knows computers, installed the new hard drive.  Then he tried to install my Windows XP Professional software - when it came time to put the product key #, it was not accepted.  It's authentic.  The product key is: * removed *.

    There no mention of this issue in section outages or other information provided in the booklet provided.  Can you help me with this problem?

    The license key and product type must match (XP Home vs XP Pro;)  (and OEM license vs the retail license).  If it's a Dell computer and you get the product key from a sticker on the case, then there is likely a version of Windows XP OEM this product key.  If you install Windows XP from a retail installation disk, then the types of licenses will not match.  You will need to use the license that accompanies the retail installation disc or you will need to contact Dell for an OEM installation disc to go with the license on your sticker case.

    HTH,
    JW

  • 6.1 LR, cloud not PC, Windows 7 64 bit version.  Under the selective brush, there is no cursor "Black or white", although these cursors exist in ACR.  I prefer LR as my post processing of starting point and I was wondering why these two options are missin

    I was hoping someone in this forum would have some tips. Thank you very much.

    The stand-alone version of LR receive these new features (until LR 7 comes out). Only the cloud of Lightroom version gets new features as they become available.

  • I just started using Lightroom 5 and I was wondering why I make a huge amount of "temporary files" in my pictures folder?

    I find a lot of cryptic files (temp files?) in my pictures folders whenever I use Lightroom. All this experience?

    I did research and just found out that it was adobe (of hundreds of them) preview files which were misdirected in the wrong folder. I've moved to a new folder and all is well.

  • I was wondering why I can find all sorts of browsers on Android Market, but Firefox is not found?

    I searched for about an hour to find Mozilla Firefox in the Android Market without success. I searched under "apps" for the terms: "Mozilla", "Firefox", "Fire Fox", "browser", "Mozilla Firefox", and yet nothing! I then tried a general search in all areas, and I also clicked on the options "Related Articles"... nothing! At out you of the market for some reason any? Android pull it? Or is there something else at work here? I found Opera (and), Dolphin HD (and), Skyfire, Chrome, Miren, UC, Galapagos, boat, xScope, Maxthon, and a few others. What happens here?

    You won't find because your phone/Tablet may not be supported [1]. What phone or Tablet do you use?

    [1] Firefox Mobile system requirements

  • Currently a subscriber of Lr 5 but I was wondering why I can't not or how to get Lr 6 or 7?

    Is it possible that I can get the later?

    Please consult the lower thread for the same

    How can I update to version 5 LR to LR?

    Hope this is useful

  • Hello, I was wondering why my adobe first pro ((when I try to open it)) say that "the application could not start correctly 0xc000007b" I need help as soon as POSSIBLE. Im trying to work on a film, but I can't until I have correct this problem. Help?

    Hello. I need help as soon as POSSIBLE, when click on my first adobe, it says "the application is unable to start correctly 0xc000007b" I need major help as soon as POSSIBLE - does anyone know how to solve this?

    ... 07 b problems http://forums.adobe.com/thread/1355374?tstart=0

  • Please explain CVI question #335358: using the operator of bit-shift twice on the same line of code produces incorrect results

    Hello

    I just stumbled on http://www.ni.com/white-paper/12323/en#335358_by_Category.

    With the help of the bit-shift operator twice on the same line of code produces incorrect results.
    Workaround: Separate the operations of shift of two bits in two distinct lines of code.

    Reported Version: 9.0    Solved Version: N/A    Added the: 14/02/2012

    Could someone please explain what kind of construction causes such a question?

    I have not noticed problems again and not have not noticed or found a thread about it.

    Thank you.

    The reported scenario looks like this:

    unsigned int a = 1, b = 62;
    unsigned  long long result = 1ULL << a << b;
    

    The problem arises because of the temporary variable used when do two little is placed in the same line. This separation into two lines will produce correct results:

    unsigned int a = 1, b = 62;
    unsigned  long long result = 1ULL << a;
    result = result << b;
    
  • 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

  • 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     ;
    
  • 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.

  • 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

  • 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.

  • OBIEE 11 g: get incorrect results when you browse detailed report summary.

    Hello

    I get incorrect results when you browse detailed report summary.

    Please find the attached document, I have gives step by step with screen shots.

    Please let me the problem and the solution.

    Kind regards

    Srinivas.

    1. What are you going through the link of action, only pass you the values (between 61 and 65) and Total, are

    If so that the parameter value will not restrict your report in detail, try filterout gender and age (between 61 and 65) group through action link.

    2 Total, to filter only the sex,

    Concerning

    Rajagopal.

Maybe you are looking for

  • Recent bizarre behavior of Firefox, safe mode doesn't help not, any help is welcome

    I have used Firefox for a while now, since Firefox 3, but my browser has recently ben having a very odd behavior. While browsing, Firefox suddenly crashes (work in the background or cursor busy", then"continuous normal navigation". It also has proble

  • Hotkeys in Windows 7 help

    Hey, can someone help me disable the keyboard shortcuts in my laptop HP G56 (fn1 etc), because I did not need them, I spent hours trying to find ways to make and nothing has done so far, my friend says that I pressed the fn + num lock key, but all th

  • Investigation of emergency BitLocker

    Is there a way to have Bitlocker enabled on a PC, have the encrypted HD and configure Bitlocker to encrypt any device of storage connected to the computer but make an exception for a specific hardware category? Scenario: Doctors office has Bitlocker

  • The destructive system restore blocked installation of Office 2010

    My office acted strange and I tried everything to operate normally, but nothing helped. So finally, frustration, I decided to do a destructive system restore. Worked very well and smoothly until "The Installation of the software." It is locked to "in

  • F.S Amillo sound fault in Windows 7

    My laptop Amillo F.S. don't have any sound saince I installed Windows7 operating system.