NATURAL JOIN problem
HelloAfter 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
Tags: Database
Similar Questions
-
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:51Hello
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 exampleSELECT 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 ;
-
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:12Hi, 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 UKI 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
-
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
-
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 join32 rows are to be extracted.
Time of the request
Select the salary, department_id from employee
departments of natural join
locations of natural join736 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,
NickHi, Nick.
Nick woodward wrote:
HelloMy 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:46Try this...
select * from (select 1 a from dual) t1 natural full join (select 2 a from dual) t2;
A
--
2
1See you soon,.
Manik. -
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.
-
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 -
Hi, I have a problem with my query:
SELECT t1.step, t2.name FROM RIGHT JOIN t1 t2 ON t2.step = t1.step
I have two tables, small t1 and t2 great.
table T1 (7 lines):
name of the step
1 a
2B
3 C
4 D
5 e
$4
7 g
table T2 (10000 lines);
name of the step ID
100 1A
101 2B
102 3 c
103 4 D
104 5th
105 1A
106 2 b
107 3 c
108 d 4
109 5 e
110 1A
111 2B
112 3 c
113 4 D
114 5 e
115 1A
116 2B
117 c 3
118 d 4
119 5th
… … ..
I want external right to join them, and have values in t2.name NULL and the range of steps (1-7).
My query returns this:
name of the step
1 a
2B
3 C
4 D
5 e
1 a
2B
3 C
4 D
5 e
1 a
2B
3 C
4 D
5 e
....
Thank you.
So there is indeed a key score in T2, it is "ROW_NR", and we can do this:
select t1.col_nr , t2.row_nr , t1.sheet_name , t1.cell , t1.column_name , t2.string_val from t1 left outer join t2 partition by (t2.row_nr) on t2.col_nr = t1.col_nr ;
-
Outer join problem - revisited
Hello
I'm starting a new thread since my previous post was an inaccurate representation of the problem I have.
I have data that looks like this.
create table cour_off (offNum number, varchar2 (10) courseId, start_date date);
create table activity (number oid, courseId varchar2 (10));
create table ncr_course (course_oid number, incept_date date, expiry_date date);
insert into cour_off values (1, 'MAT101', to_date (' 15 / 05/2012 ', ' DD/MM/YYYY'));
Insert in activity values (10, 'MAT101');
insert into ncr_course values (10, to_date (' 01 / 05/2012 ', ' DD/MM/YYYY'), to_date (' 10 / 05/2012 ', ' DD/MM/YYYY'));
When I write the query using ANSI notation, the query works fine. 1 form is returned with a NULL value for the incept_date column.
Select activity.courseId, ncr_course.incept_date
of cour_off
Join the activity on cour_off.courseId = activity.courseId
activity.Oid left join ncr_course = ncr_course.course_oid
and cour_off.start_date between ncr_course.incept_date
and ncr_course.expiry_date
where cour_off.offNum = 1
However, when the style non ANSI query as shown below, no records are returned.
Select activity.courseId, ncr_course.incept_date
activity, cour_off, ncr_course
where cour_off.offNum = 1
and cour_off.courseId = activity.courseId
and activity.oid = ncr_course.course_oid (+)
and cour_off.start_date between ncr_course.incept_date and ncr_course.expiry_date
I would like to know how I can resolve the application of style not ANSI so that the 1 record is returned.
Thank you!Well, native Oracle outer join does not allow outside join two tables, to do something like:
select courseId, ncr_course.incept_date from ( select activity.courseId, activity.oid, cour_off.start_date from cour_off, activity where cour_off.offNum = 1 and cour_off.courseId = activity.courseId ), ncr_course where oid = ncr_course.course_oid(+) and start_date between ncr_course.incept_date(+) and ncr_course.expiry_date(+) / COURSEID INCEPT_DA ---------- --------- MAT101 SQL>
SY.
-
I want left join two tables (a, b) where a.account = b.accountNumber number and this join must occur only for the specified account numbers. For this, my request is as follows.
Select a.PRIMARY_FIRST_NAME, a.PRIMARY_LAST_NAME, a.ACCOUNT_NUMBER, a.PRIMARY_SSN, a.ADDRESS_1, a.ADDRESS_2,
a.CITY, a.STATE, a.ZIP, b.phone_number, cust_account_details a b.status
Join phone_data b left
on
a.Account_Number = b.account_number
and
a.Account_Number in
('11 ', ' 22')
The above does not work correctly. the join occurs not only for the numbers in the range. What could be the problem?Try this: -.
select a.PRIMARY_FIRST_NAME,a.PRIMARY_LAST_NAME,a.ACCOUNT_NUMBER,a.PRIMARY_SSN,a.ADDRESS_1,a.ADDRESS_2, a.CITY,a.STATE,a.ZIP,b.phone_number,b.status from cust_account_details a left join phone_data b on a.account_number=b.account_number *where* a.account_number in ('11','22')
Thank you.
-
Hi Experts,
My version of the database is Oracle9i Enterprise Edition Release 9.2.0.1.0
This is the date of the sample
create table bill_coll (doc_date date, fees_id number, coll_amt number).
insert into bill_coll values('30-AUG-13',10,2540);
insert into bill_coll values('30-AUG-13',10,5560)
insert into bill_coll values('30-AUG-13',10,3000)
insert into bill_coll values('30-AUG-13',10,3000)insert into bill_coll values('22-AUG-13',10,6000)
insert into bill_coll values('22-AUG-13',10,4300)create table bnk_trn (doc_date date, number of trn_amt);
insert into bnk_trn values('22-AUG-13',10300);
insert into bnk_trn values('29-JUN-13',25000);
insert into bnk_trn values('29-JUN-13',5000);
Based on the two tables above, I have to get the comparison bill on data collection and the amount deposited to the Bank.
The below mentioned query is not helped and what gives unexpected output:
Select nvl (a.doc_date, b.doc_date) doc_date, a.coll, b.trn_amt
Of
(select doc_date coll, sum (coll_amt) of the bill_coll in doc_date group) a
full join
b (select doc_date, sum (trn_amt) group bnk_trn by doc_date trn_amt)
by order of a.doc_date = a.doc_date b.doc_date
/DOC_DATE COLL TRN_AMT
----------------------------------------------22 AUGUST 13 10300 10300
30 AUGUST 13 11100
29 JUNE 13
29 JUNE 13Please help me to get an output as follwos:
DOC_DATE COLL TRN_AMT
----------------------------------------------22 AUGUST 13 10300 10300
30 AUGUST 13 11100
29 JUNE 13 30000Thanks in advance.
This is perhaps a bug concerning the implementation of the standard ANSI-SQL-join-Systax. (I don't like the alias in the order by clause)
Maybe you can rewrite the statement as below
Select doc_date, sum (coll), sum (trn_amt)
de)
Select doc_date, coll_amt coll, trn_amt null in bill_coll
Union of all the
Select doc_date, null, bnk_trn trn_amt coll
)
Doc_date group
order of doc_date
DOC_DATE SUM(COLL) SUM(TRN_AMT) 22 AUG 13 10300 10300 29 JUNE 13 - 30000 30 AUGUST 13 14100 (the order is not accurate because I used a varchar column not a date column)
-
Outer join - problem with the name of the table in the select list
Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production
create table (j1)
number of C1,
number of C2);
create table (j2)
number of C1,
number of C2);
insert into values j1 (1, 10);
insert into j1 values (1, 100);
insert into values j1 (1, 1000);
insert into values j2 (1, 2);
insert into values j2 (1, 4);
insert into values j2 (1, 8);
commit;
Select c1, j1.c2, j2.c2 of outer join of j1 j2 using (c1); - DOES NOT
Select c1, j1 j2 (c1) using outer join j2.c2. - WORK
Why?
Hello
Interesting question! Oracle goes very far in trying not to trigger an error.
The OUTER keyword (if used; it is always optional) must be preceded by one of the keywords, right or LEFT. Since neither LEFT, RIGHT, or FULL comes before OUTER in queries in your first message, it does not recognize as keyword OUTER and treats him like a table alias for table j1. Since j1 has an alias, the real table name cannot be used in the SELECT clause. This query is executed:
SELECT c1
outer.c2
j2.c2
External J1
Join the HELP of j2 (c1)
;
There is an INTERNAL join. Add to your sample data:
insert into values of j1 (-1, -10);
insert into values of j2 (-2, -20);
to be specified.
Moreover, USING is short for confUSING. I suggest you use IT for all the join conditions. Everyone (unless they you write or read a manual) does.
Maybe you are looking for
-
Re: Need repair manual for Satellite P200
Hi guy´sso, after 3 years my P200-1 | 2 harman/kardon collect a hell of dirt and dust.Is what I am looking for a repair manual so I can participate the notbook clean the fan´s and put everything back together.I hope that there is something?icesaint
-
Satellite A300 PSAG4E - FN keys displayed on the screen
Toshiba Satellite A300 PSAG4EWindows Vista You press the FN list does not appear on the screen of choiceJobs such as Bluetooth or Air other options and note that it works, but without the watch on the screen list Tried to download your site definitio
-
Chicony Cam is locked by another application on Satellite A200
Hi, can you please help me to resolve if there is problem with my webcam settings. I am trying to open my webcam using my yahoo messanger but the camera is not accessible and the message display as saying "Cam is locked by another Application." Can y
-
Cannot change the appearance of custom settings window
Hello In the attached vi, I changed a few custom settings in the appearance window category vi properties box. What should I do to change the settings back to the 'top-level Application window"? Simply by clicking on the respective radio button doesn
-
Can't access drive c / programs on another computer in homegroup
I have a desktop computer running W7PE and a laptop running W7U. I created the homegroup in accordance with the instructions, and see themselves on the map of the network, they cannot access the files from each other. When I try to connect to the