Are these SQL queries equivalent [duplicate] - sql

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.

Related

What Does *= means in WHERE Clause in TSQL?

some one have asked me the output of below query.
Select *
from TableA t1, TableB t2
where t1.Id *= t2.Id
Can anyone explain me, if such type of any query exists, if so then how its works. Because i have never seen such type of query Thanks.
UPDATE:
Also when i run this query in SQL Server, i get this;
The query uses non-ANSI outer join operators ("*=" or "=*").
To run this query without modification, please set the compatibility level
for current database to 80, using the SET COMPATIBILITY_LEVEL option
of ALTER DATABASE.
It is strongly recommended to rewrite the query using ANSI outer join
operators (LEFT OUTER JOIN, RIGHT OUTER JOIN).
In the future versions of SQL Server, non-ANSI join operators will
not be supported even in backward-compatibility modes.
Using asterisk in a WHERE is an old non-ANSI compliant syntax for OUTER JOINing tables and therefore should not be used anymore.
Here's the link.
The asterisk in the where condition is actually part of a non-ANSI outer join operator, it is used to define an implicit outer join.
It will cause trouble in modern databases as this operator has been obsolete since 1992.
Essentially the below are the same:
SELECT * FROM TableA LEFT OUTER JOIN TableB ON t1.Id = t2.Id
SELECT * FROM TableA , TableB WHERE t1.Name *= t2.Name
The *= operator means LEFT OUTER JOIN.

Difference in INNER join and cartesian join in SQL Server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between Inner Join & Full join
What is the difference between these two, especially within SQL Server 2008 -
select * from table1 t1, table2 t2 where t1.col1 = t2.col1
AND
select * from table1 t1 INNER JOIN table2 t2 ON t1.col1 = t2.col1
They are the same.
But consider what your syntax would look like if you wanted to do an INNER JOIN and then also OUTER JOIN to a different table.
It's more consistent to follow the INNER JOIN syntax so that if you need to modify your SQL later, it's easier to do. Plus, consistency allows others to have a better idea of your intent.
The first is the old way of writing an inner join, the second is the way it's written after the join command was added to SQL.
As long as both ways are accepted, there is no difference at all in the result. The execution plans for the two queries will be identical.
The old way of writing a join is being phased out, and may be disallowed in certain modes in later versions of SQL Server. It's not in SQL Server 2008.

Oracle Style Joins in SQL Server

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.

What's the difference between the two SQL join notations?

SQL 1: select * from t1 join t2 on t1.f1 = t2.f2
SQL 2: select * from t1,t2 where t1.f1 = t2.f2
The results that they return are same. Are there any differences between them? For example, in how the DBMS runs them, or in the query plan?
There is no operational difference between the two queries.
However, the explicit join notation is the better style to learn and use; leave the other for (as yet unchanged) legacy code.
One is old style and one is new (ANSI) style. The main reason that I've found why you would want to use the new style is for standard support of outer joins. With the old style, outer joins are vendor-specific. New style has it standard:
select * from t1 left outer join t2 on t1.f1 = t2.f2
In your example, SQL 1 is the new and SQL 2 is the old style, btw.
Basically, there are no difference between the two queries in operation.
However, both have same execution plan and have same cost that mean both query take equal time to execute(same performance).
Use of join operator is a modern way.
The two are semantically equivalent (among other variations on the theme). One difference is that many users on Stackoverflow are very vocal in expressing their intolerant to 'old style' inner joins (your SQL 2), to the point where anyone posting them risks being downvoted in addition to being admonished in comments. You're also likely to see the term 'anti-pattern' applied, which is nonsense. I've not encountered this style intolerance outside of the SO community. In fact, 'old style' inner joins are very common in the SQL literature.

What does a (+) sign mean in an Oracle SQL WHERE clause? [duplicate]

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 […]