What Does *= means in WHERE Clause in TSQL? - sql

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.

Related

What's the scope of table aliases?

This question is in the context of Sqlite but I wonder in general what's the rule for aliases. Suppose I have the following query:
SELECT *
FROM (SELECT * FROM tableA as t1 inner join tableB as t2 on t1.id=t2.some_id) as t3,
(SELECT * FROM tableC as t1 inner join tableD as t2 on t1.id=t2.some_id) as t4
WHERE t3.id=t4.id
The question is, will the two t1's and t2's clash? in my experience with multiple database vendors they won't clash, but, could it confuse the optimizer to the point of producing a buggy result? I have this hypothesis for a query giving bogus results in Sqlite.
No, the aliases will not clash. They are known only in their respective subqueries.
Of course, the query will fail because of the trailing comma. And in general, you should avoid commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.

Are these SQL queries equivalent [duplicate]

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.

NESTED INNER JOIN using MS Access 2003 via ODBC

If this works:
SELECT COUNT(t1.ID) AS count FROM Project t1
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2 ON t1.Site=t2.Site AND t1.id=t2.id
and this works:
SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number
Why doesn't this work:
SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2 ON t1.Site=t2.Site AND t1.id=t2.id
Ultimately, I have 10 tables like the Wall table that I am trying to get a total count from the first SELECT....
SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN (Project t1
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2
ON t1.Site=t2.Site AND t1.id=t2.id)
ON t3.Project_number=t1.Project_number
Maybe it's just a syntax error? Office Help at the bottom where they mention nesting. The other possibility is that the aliases are somehow scoped so that they are not available to the join, but I'm no expert on MS Access. Maybe you should just try dropping the aliases altogether.
You have a couple of minor issues with your code: a table name that starts with an underscore character (_Equipment_id) and an AS clause ("alias") that is a SQL keyword (AS count). When these are corrected, your SQL is valid SQL-92 syntax.
Sadly, the problem is that Access (ACE, Jet, whatever) does not support the SQL-92 Standard. Access insists that each nested JOIN clause is put in parentheses.
[Aside: JOINs in parentheses are allowed in Standard SQL because it can potentially change the query results. However Access, does not respect the order specified by the coder and allows itself to evaluate JOINs in order it sees fir. So not only Access's syntax non-compliant with the Standard, there is also a loss of functionality! However, this further problem with Access will have no ill effect for this particular query.]
You have two JOINs in the same scope here:
...
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number
INNER JOIN
...
Your code needs to work around Access's problem by enclosing the JOIN in parentheses; because all your JOINs are INNER flavour, it probably doesn't matter where they go.
Also, as regards correcting your AS clause, Access again doesn't support Standard SQL's quoted identifiers (...AS "count"...) and insists you use its proprietary square brackets syntax (...AS [count]...) -- of course, you could choose a different name but there may exist application code that relies on it.
Code to workaround both Access problems:
SELECT COUNT(t3.ID) AS [count]
FROM (Wall t3
INNER JOIN Project AS t1
ON t3.Project_number = t1.Project_number)
INNER JOIN (
SELECT DISTINCT t.Site,t.id
FROM _Equipment_id AS t
WHERE t.OEM LIKE '%ABC%'
) AS t2
ON t1.Site = t2.Site
AND t1.id = t2.id;

What does =* mean?

I'm trying to trace some SQL in Microsoft Server. I came across a join that is using a convention unfamiliar to me. What does "=*" mean?
WHERE table1.yr =* table2.yr -1
This:
WHERE t.column =* s.column
...is old TSQL (pre SQL Server 2005) outer join syntax, and is not an ANSI JOIN.
Reference: SQL Server 2005 Outer Join Gotcha
I believe that is old syntax indicating an outer join condition from table1 to table2
Old style:
SELECT * FROM table1, table2
WHERE table1.yr =* table2.yr -1
New style (SQL92):
SELECT * FROM table2
LEFT OUTER JOIN table1 ON table1.yr = table2.yr - 1
This is the old style syntax for expressing joins
It means the code needs to be replaced immediately! This style join is supposed to be a right join. Unfortunately it will sometimes be interpreted as a cross join, so the results of using this join may not be correct. Also, this syntax is deprecated and cannot be used inteh next version of SQl server.
That is the ANSI SQL 1989 syntax for RIGHT OUTER JOIN, where *= would be the LEFT OUTER JOIN.
You should note also that putting the join syntax in the WHERE clause is deprecated in SQL 2008. http://scarydba.wordpress.com/2009/09/15/no-join-predicate/ <== A timely article on this.
This is the old style of joins which were deprecated in ANSI SQL92. New syntax uses INNER and OUTER JOIN which join tables based on expressions rather than equality
A ??? outer join is specified using the symbol =* in place of = in the WHERE clause.
yeap, it's another syntax for a left outer join
from
table1 left outer join table2 on table1.yr = table2.yr - 1
SELECT *
FROM table1, table2
WHERE table1.yr =* table2.yr -1
Means the same thing as this:
SELECT *
FROM
table2
LEFT OUTER JOIN
table1
ON table1.yr = (table2.yr - 1)
The * syntax is considered outdated, and is not in line with the ANSI standards.
Oracle has a similar construct like this:
WHERE table1.yr (+)= table2.yr
To be plain and simple.
This is a SQL-92 outer join operator ( more info )
Don't use it, its very old school, but its similar to LEFT JOIN, and RIGHT JOIN.
All its doing is telling which side of the join is the "Parent" side, so rows on that side will be considered first.
If you try to run this on SQL 2005, it will throw an error, saying that you need to run this in compatibility mode.
There are a lot of silly answers here. You didn't give the FROM clause, so there's no way to tell if your *= represents a LEFT or a RIGHT outer join.
WHERE table1.yr =* table2.yr -1
is old syntax for an outer join, for sure. But anyone who claims to know whether it's a LEFT or RIGHT outer join is mistaken. It depends on the order in which table1 and table2 are named in the FROM clause, and that's not given.

What does (+) do in Oracle SQL?

I'm using Oracle SQL Developer to query an Oracle DB (not sure which version it is) and I'm going to use the SQL I make for a Crystal report. Many of the reports the previous developers have written don't use JOIN keywords to make the joins (and I'm not too familiar with JOIN keywords as a result).
Many of the joins they make are made in the WHERE statement. I'll notice something like this.
Select * From TableA, TableB WHERE TableA.PrimaryKey(+) = TableB.ForeignKey
My question is concerning the (+). What purpose does it serve and how do I use it in my code?
It is not recommended. See this previous answer
Difference between Oracle's plus (+) notation and ansi JOIN notation?
That represents a “right outer join” (right because the = is on the right side of the +).
SELECT *
FROM TableA, TableB
WHERE TableA.PrimaryKey(+) = TableB.ForeignKey
is equivalent to
SELECT *
FROM TableA
RIGHT OUTER JOIN TableB
ON (TableA.PrimaryKey = TableB.ForeignKey)
right outer join
(+) is used to perform right outer join in Oracle
RIGHT OUTER JOIN is one of the JOIN operations that allow you to specify a JOIN clause
For details
http://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj57522.html