T-SQL Left Join Symbol - tsql

What is the symbol (like *=) for doing a left join?
I've got table A and B, must always return all records from table A even if there is no records in table B.

This is the new ansi standard syntax, much clearer imo.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.ID = B.ID

You shouldn't be using that operator, as it was deprecated in Sql Server 2008, and will be removed in future versions.
You should use ANSI compliant LEFT JOIN or LEFT OUTER JOIN instead.
It was deprecated for a reason. That operator's syntax is confusing (it conflicts with many language's standard overload of "multiply and assign") and is non-standard.

Besides of that, use of ANSI standard syntax LEFT [OUTER] JOIN for joins simplifies looking for errors a lot, believe me.
It allows also for clearer distinction between filters in WHERE clause and join operands.
I would recommend also use ANSI syntax for inner joins.

Avoid any ancient syntax like that. Rewrite it to include the newer "LEFT OUTER JOIN" and/or "RIGHT OUTER JOIN" syntax:
SELECT
a.*, B.*
FROM TableA a
LEFT OUTER JOIN TableB b ON a.id=b.id
Difference between * = and LEFT Outer Join
from the link:
In earlier versions of Microsoft® SQL
ServerT 2000, left and right outer
join conditions were specified in the
WHERE clause using the *= and =*
operators. In some cases, this syntax
results in an ambiguous query that can
be interpreted in more than one way.
SQL-92 compliant outer joins are
specified in the FROM clause and do
not result in this ambiguity. Because
the SQL-92 syntax is more precise,
detailed information about using the
old Transact-SQL outer join syntax in
the WHERE clause is not included with
this release. The syntax may not be
supported in a future version of SQL
Server. Any statements using the
Transact-SQL outer joins should be
changed to use the SQL-92 syntax

Related

Is there an equivalent to PostgreSQL's Lateral Join in Redshift?

As the title suggests, does an equal to the lateral join exist in redshift? I haven't run into a situation where it might be necessary yet, but it seems hard to imagine replicating the functionality of a lateral join if I needed to with what I know now.

TSQL -- how does optimizer deal with joins with unused tables

TSQL -- how does optimizer deal with joins with unused tables
SELECT table1.col1, table2.col1, table2.col2 -- etc.
FROM dbo.table1
LEFT JOIN dbo.table2
on (table1.id = table2.id)
LEFT JOIN dbo.table3
on (table1.id = table3.id)
In the simple example above it is obvious that table 3 isn't needed, however more intricate cases can exist.
Question: Is the TSQL query optimizer able to determine that the JOIN with table 3 isn't needed? If so, would it potentially miss other optimizations for more complex queries, if table3 wasn't manually removed from the query?
I use SSMS 14.017 with an underlying SQL database of select ##version = Microsoft SQL Server 2014 (SP3)
Yes, query optimizer could skip table3 because it is not referenced in SELECT list and the join condition is LEFT OUTER JOIN so there is no filtering.
Related: 10 Cool SQL Optimisations That do not Depend on the Cost Model by Lukas Eder:
Unneeded Table Accesses
JOIN Elimination
JOIN Elimination: An Essential Optimiser Feature for Advanced SQL Usage

AWS docs say that merge joins can be used for outer joins, but not full joins. Are those the same things?

Redshift documentation says:
Merge Join
Typically the fastest join, a merge join is used for inner joins and
outer joins. The merge join is not used for full joins.
But I've always read that full joins and outer joins are the same thing: rows from both tables are kept, regardless of if they exist in the other table.
Are they just referring to left outer joins and right outer joins as those that work for merge sort, while "outer join"s (full outer joins) do not?
Good spot, perhaps the docs could be clearer. You can submit a pull request for our docs if you feel motivated to do so.
In this case, the doc means that a merge join is used when one table is the "primary" table, e.g, it will be used for INNER JOIN, LEFT [OUTER] JOIN, RIGHT [OUTER] JOIN but not used for FULL [OUTER] JOIN where rows from both side must be retained.

Difference between INNER JOIN and WHERE?

First Query:
Select * from table1 inner join table2 on table1.Id = table2.Id
Second Query:
Select * from table1, table2 where table1.Id = table2.Id
What is difference between these query regarding performance which should one use?
The two statements you posted are logically identical. There isn't really a
practical reason to prefer one over the other, it's largely a matter of
personal style and readability. Some people prefer the INNER JOIN syntax and
some prefer just to use WHERE.
Refering to Using Inner Joins:
In the ISO standard, inner joins can
be specified in either the FROM or
WHERE clause. This is the only type of
join that ISO supports in the WHERE
clause. Inner joins specified in the
WHERE clause are known as old-style
inner joins.
Refering to Join Fundamentals:
Specifying the join conditions in the
FROM clause helps separate them from
any other search conditions that may
be specified in a WHERE clause, and is
the recommended method for specifying
joins.
Personaly, I prefer using INNER JOIN. I find it much clearer, as I can separate the join conditions from the filter conditions and using a seperate join block for each joined table.
To amplify #Akram's answer - many people prefer the inner join syntax, since it then allows you to more easily distinguish between the join conditions (how the various tables in the FROM clause relate to each other) from the filter conditions (those conditions that should be used to reduce the overall result set. There's no difference between them in this circumstance, but on larger queries, with more tables, it may improve readability to use the inner join form.
In addition, once you start considering outer joins, you pretty well need to use the infix join syntax (left outer join,right outer join), so many find a form of symmetry in using the same style for inner join. There is an older deprecated syntax for performing outer joins in the WHERE clause (using *=), but support for such joins is dying out.

t-sql condition placement

Should SQL Server yield the same results for both of the queries below? The primary difference is the condition being placed in the WHERE clause with the former, and with the latter being placed as a condition upon the join itself.
SELECT *
FROM cars c
INNER JOIN parts p
ON c.CarID = p.CarID
WHERE p.Desc LIKE '%muffler%'
SELECT *
FROM cars c
INNER JOIN parts p
ON c.CarID = p.CarID
AND p.Desc LIKE '%muffler%'
Thanks in advance for any help that I receive upon this!
For INNER JOINS it will make no difference to semantics or performance. Both will give the same plan. For OUTER JOINs it does make a difference though.
/*Will return all rows from cars*/
SELECT c.*
FROM cars c
LEFT JOIN parts p
ON c.CarID = p.CarID AND c.CarID <> c.CarID
/*Will return no rows*/
SELECT c.*
FROM cars c
LEFT JOIN parts p
ON c.CarID = p.CarID
WHERE c.CarID <> c.CarID
For inner joins the only issue is clarity. The JOIN condition should (IMO) only contain predicates concerned with how the two tables in the JOIN are related. Other unrelated filters should go in the WHERE clause.
For inner joins the two queries should yield exactly the same results. Are you seeing a difference?
Yes, they both get the same results. The difference is when the condition is checked, if during the join or afterwards.
The execution plan will be identical in your example. Next to the parse button should be the "Show execution plan" button. It will give you a clearer picture.
I think in a more complex query with many joins it can be an issue in efficiency, as stated above, before or after.
EDIT: sorry assuming your using sql server management studio.
My recommendation for this kind of situation would be:
put the JOIN condition (what establishes the "link" between the two tables) - and only that JOIN condition - after the JOIN operator
any additional conditions for one of the two joined tables belongs in the regular WHERE clause
So based on that, I would always recommend to write your query this way:
SELECT
(list of columns)
FROM
dbo.cars c
INNER JOIN
dbo.parts p ON c.CarID = p.CarID
WHERE
p.Desc LIKE 'muffler%'
It seem "cleaner" and more expressive that way - don't "hide" additional conditions behind a JOIN clause if they don't really belong there (e.g. help establish the link between the two tables being joined).