This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Oracle: What does (+) do in a WHERE clause?
Consider the simplified SQL query below, in an Oracle database environment (although I'm not sure that it's Oracle-specific):
SELECT
t0.foo, t1.bar
FROM
FIRST_TABLE t0, SECOND_TABLE t1
WHERE
t0.ID (+) = t1.ID;
What is that (+) notation for in the WHERE clause? I'm sorry if this is an ignorant newbie question, but it's been extremely difficult to search for on Google or StackOverflow... because even when using quote marks, search engines see a '+' sign and seem to want to treat it as some kind of a logical directive.
This is an Oracle-specific notation for an outer join. It means that it will include all rows from t1, and use NULLS in the t0 columns if there is no corresponding row in t0.
In standard SQL one would write:
SELECT t0.foo, t1.bar
FROM FIRST_TABLE t0
RIGHT OUTER JOIN SECOND_TABLE t1;
Oracle recommends not to use those joins anymore if your version supports ANSI joins (LEFT/RIGHT JOIN) :
Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator. Outer join queries that use the Oracle join operator (+) are subject to the following rules and restrictions […]
Related
This question already has answers here:
Oracle: What does `(+)` do in a WHERE clause?
(4 answers)
Closed 5 years ago.
Recently I observed the following syntax:
AND TRUNC (SYSDATE) + 1 BETWEEN a(+) AND b(+)
I know that the (+) sign is used to address left or right join in conditions like:
and a = b(+)
... but I have no idea what it means in the BETWEEN function.
Can someone explain, please, or better give an example using BETWEEN function?
Thank you,
The Oracle docs says:
Oracle recommends that you use the FROM clause OUTER JOIN syntax
rather than the Oracle join operator. Outer join queries that use the
Oracle join operator (+) are subject to the following rules and
restrictions, which do not apply to the FROM clause OUTER JOIN syntax:
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Explicit vs implicit SQL joins
Is there a difference using join andselect from multi-tables?
SQL Joins: Future of the SQL ANSI Standard (where vs join)?
What is the difference between JOIN and declaring multiple tables in the FROM clause?
Such as:
SELECT *
FROM table1 AS t1,
table2 AS t2
WHERE t1.id = t2.id
Compared to:
SELECT *
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t2.id = t1.id
The second version, with the explicit JOIN and join condition is standardized SQL.
The implicit join syntax with a WHERE clause is deprecated syntax (or, rather, considered bad) - partially because it is easy to forget the WHERE clause and cause a Cartesian product.
Why Use the new syntax?
As others have stated, the new syntax has become the preferred convention. In larger queries the new syntax is easier to read, debug, and ensure the join criteria is added (meaning no accidental CROSS JOINS.
Is the old syntax deprecated (for inner joins)?
Not according to ANSI -- both are valid, even if the first is disfavored. Although, performing outer joins in the old syntax has been deprecated -- mainly because it can be ambiguous.
How consensus is the "use the new syntax" view ?
Aaron Bertran considers it a "bad habit to kick"
SQL Server Central the consensus was "Kill kill kill it with fire"
Joe Celko -- meh, but it's easier to read.
Both will output the same and are just different variations of writing the query.
SELECT *
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t2.id = t1.id
Is the preferred join method as you are explicitly stating which type of join you are using, i.e. LEFT, OUTER, INNER.
We have a Oracle 10g and most of our applications are running Oracle Forms 6i. I found that all of the queries written in views/packages/procedures/functions are JOINING tables at WHERE clause level. Example
SELECT * FROM
TABLE_A A,
TABLE_B B,
TABLE_C C,
TABLE_D D
WHERE
A.ID=B.ID(+)
AND B.NO=C.NO(+)
AND C.STATUS=D.ID
AND C.STATUS NOT LIKE 'PENDING%'
This query applies only to ORACLE since it has the (+) join qualifier which is not acceptable in other SQL platforms. The above query is equivalent to:
SELECT * FROM
TABLE_A A LEFT JOIN TABLE_B B ON A.ID=B.ID
LEFT JOIN TABLE_C C ON B.NO=C.NO
JOIN TABLE_D D ON C.STATUS=D.ID
WHERE
C.STATUS NOT LIKE 'PENDING%'
Non of the queries I have seen is written with join taking place in the FROM clause.
My question can be divided into three parts:
Q: Assuming that I have the same Oracle environment, which query is better in terms of performance, cache, CPU load, etc. The first one (joining at WHERE) or the second (joining at FROM)
Q: Is there any other implementation of SQL that accepts the (+) join qualifier other than oracle? if yes, which?
Q: Maybe having the join written at WHERE clause makes the query more readable but compromises the ability to LEFT/RIGHT join, that's why the (+) was for. Where can I read more about the origin of this (+) and why it was invented specifically to Oracle?
Q1. No difference. You can check it using profiling and compare execution plan.
Q2. As I know, only oracle support it. But it is not recommended to use in latest version of Oracle RDBMS:
Oracle recommends that you use the FROM clause OUTER JOIN syntax
rather than the Oracle join operator. Outer join queries that use the
Oracle join operator (+) are subject to the following rules and
restrictions, which do not apply to the FROM clause OUTER JOIN syntax:
Q3. Oracle "invent" (+) before outer join was specified in SQL ANSI.
There should be no performance difference. Assuming you're on a vaguely recent version of Oracle, Oracle will implicitly convert the SQL 99 syntax to the equivalent Oracle-specific syntax. Of course, there are bugs in all software so it is possible that one or the other will perform differently because of some bug. The more recent the version of Oracle, the less likely you'll see a difference.
The (+) operator (and a variety of other outer join operators in other databases) were created because the SQL standard didn't have a standard way of expressing an outer join until the SQL 99 standard. Prior to then, every vendor created their own extensions. It took Oracle a few years beyond that to support the new syntax. Between the fact that bugs were more common in the initial releases of SQL 99 support (not common but more common than they are now), the fact that products needed to continue to support older database versions that didn't support the new syntax, and people being generally content with the old syntax, there is still plenty of code being written today that uses the old Oracle syntax.
A1:
As far as I know they vary in-terms of syntax not in performance. So there is no difference between joining at 'where' clause and joining at 'from' clause.
A2:
To answer this in better way, 'Joining at FROM' clause is standard across all the platforms. So forget about (+) symbols
A3
I have worked in Oracle for sometimes. People use (+) symbols for left/right join because it's easy to write. Some ppl use join at (FROM) clause because it's more readable and understandable.
Hope these points helps you. Please let me know incase am wrong with anything.
One difference between Oracle syntax and ANSI syntax is:
In Oracle syntax you cannot make a FULL OUTER JOIN, there you have to use ANSI syntax.
Oracle introduced ANSI syntax in Oracle 9i - including several bugs. In the meantime since Oracle 11 or 12 it works quite well, but you may discover some obstacles in Oracle 9/10.
Another advandage of ANSI Join syntax is you cannot forget any join condition.
"SELECT * FROM TABLE_A, TABLE_B" performs implicitly a Cross-Join. "SELECT * FROM TABLE_A JOIN TABLE_B" raise an error, you are forced to provide the join condition. If you want to Cross-Join you have to specify it, i.e. "SELECT * FROM TABLE_A CROSS JOIN TABLE_B"
This question already has answers here:
Oracle "(+)" Operator
(4 answers)
Closed 9 years ago.
The query is as follows
select tbl1.x, tbl2.y
from
tbl1, tbl2
where tbl1.some_column (+) = tbl2.some_column
AND tbl1.some_column2 (+) = 'Y'
What confuses me is this part
AND tbl1.some_column2 (+) = 'Y'
What does it mean?? If I remove the (+) the query returns completely different number of rows. I do understand the application of (+) when there are columns names on the both sides of the =, but what does (+) do when there is a string on the one side of the = ?
It's legacy syntax that Oracle had before they had LEFT OUTER JOIN (or, in this case, probably RIGHT OUTER JOIN).
No need to use it anymore in modern Oracle (at least from 9i, maybe even 8i).
Now you can use the standard SQL join syntax:
SELECT tbl1.x, tbl2.y
FROM tbl1 RIGHT OUTER JOIN tbl2
ON ( tbl1.some_column = tbl2.some_column AND tbl1.some_column2 = 'Y')
Is there a way to do oracle style joins in SQL Server?
select *
from t1, t2
where t1.id = t2.id (+)
EDIT
Why is it preferable to use the "left outer join" and "inner join" types instead? I find it easier to read the older form, especially if there are complex table joins involved.
Microsoft is using the ANSI ISO SQL Standard. Mark Rittman has a good explanation of ANSI joins and why you should use them. SQL Server, however, doesn't support the NATURAL JOIN syntax that's listed in that article and the ANSI standard. You may be more familiar with the old Oracle syntax, but it is the standard and something that you'll find on other database products.
To answer your question - there is no way to perform Oracle style joins on SQL Server. Even the *= and =* style joins have been deprecated and are being removed completely in the next version of the product.
SELECT *
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.id
This AskTom article shows you the syntax equivalencies from (+) to (LEFT AND RIGHT) OUTER JOIN for Oracle, and SQL Server uses the OUTER JOIN syntax.