Are there any succinct approaches to this left join? - sql

A simple inner join
select * from table1 a
inner join
dbo.table2 b
on a.inventory_id = b.inventory_id
Wouldn't it be nice and intuitive to put it like this?
select * from table1 a
inner join
dbo.table2 b
on inventory_id
Are there any comparable succinct approaches?
Thanks!

If you were using PostgreSQL, MySQL or Oracle, you can use a Natural Join
select *
from table1 a
natural join table2 b
Not sure why the question title includes "left", but you can do a natural left join as well.
Unfortunately, I'm sure you're using SQL Server due to the dbo., so no, the ON condition is required.

How about a natural join:
select *
from table1 a
natural join dbo.table2 b
However, your RDBMS may not support it, and I would recommend always specifying the join type and conditions in your queries. It's much more maintainable in the long run.
I'm guessing from the dbo. that you're using SQL Server though, and it's not supported there. See here for more info.
Edit:
There's another possibility that's again not supported by SQL Server but is worth noting. This could actually be worth using, as your join condition is clearly specified. More info here.
select *
from table1
inner join dbo.table2 using (inventory_id)

If you don't want to use ANSI standard JOINs, then use implicit syntax:
select * from table1 a, table2 b
where a.inventory_id = b.inventory_id

Related

Join using same table related to other tables

Good day everyone. I came across the following problem, I'm changing some selects that used an old T-SQL formatting, as in the example below.:
SELECT *
FROM TABLE1 M,
TABLE2 A,
TABLE3 TP
WHERE M.CODIGO1 *= A.CODIGO1
AND
M.CODIGO1 *= TP.CODIGO2
And when making the change to be able to use Join in FROM, I came across the following problem, as I already perform the join in .:
TABLE1 M LEFT JOIN TABLE2 A ON M.CODIGO1 = A.CODIGO1
How could I rerelated table TABLE1 using LEFT JOIN?
Thanks to anyone who can help!
Edited question.
You are using the ancient comma-join syntax, which was replaced in by ANSI in SQL-92, back in 1992.
This is the equivalent of the more modern syntax using sequential LEFT JOINs
SELECT *
FROM TABLE1 M
LEFT JOIN TABLE2 A ON M.CODIGO1 = A.CODIGO1
LEFT JOIN TABLE3 TP ON M.CODIGO2 = TP.CODIGO2;

SQL Inner Join with no WHERE clause

I was wondering, how does an inner join work when no WHERE clause is specified? For example,
SELECT table1.letter, table2.letter, table1.number, table2.number
FROM tbl AS table1, tbl AS table2;
tbl:
text, integer
a , 1
b , 2
c , 3
Tried finding some examples online but I couldn't seem to find any :-/
Thanks!
The current implicit join syntax you are using:
FROM tbl AS table1, tbl AS table2;
will result in a cross join if no restrictions are present in the WHERE clause. But really you should use modern ANSI-92 syntax when writing your queries, e.g.
SELECT
table1.letter,
table2.letter,
table1.number,
table2.number
FROM tbl AS table1
INNER JOIN tbl AS table2
-- ON <some conditions>
One obvious reason to use this syntax is that it makes it much easier to see the logic of your query. In this case, if your updated query were missing an ON clause, then we would know right away that it is doing a cross join, which most of the time is usually not what you want to be doing.
The comma operator generates a Cartesian product -- every row in the first table combined with every row of the second.
This is more properly written using the explicit cross join:
SELECT table1.letter, table2.letter, table1.number, table2.number
FROM tbl table1 CROSS JOIN
tbl table2;
If you have conditions for combining the two tables, then you would normally use JOIN with an ON clause.
You can use cross join
select * from table1 cross join table2
Here is a link to understand more about the use of cross join.
https://www.w3resource.com/sql/joins/cross-join.php

Better Approach of writing Inner Join?

Which approach should I use ?
This
Select * from table1,table2 where table1.id=table2.id;
or
Select * from table1 inner join table2 on table1.id=table2.id;
Note : Id is foriegn Key .
In most modern RMDBS both would yield the same execution plan but
the second one is the reccommended form since it makes clear what are the join conditions right after you declare said join
If your query gets big as they do, the second style is usually regarded as easier to read and comprehend as the JOIN and the WHERE parts of the query are separated.
Select * from table1
INNER JOIN table2 on table1.id=table2.id
INNER JOIN table3 on table1.id=table3.id
WHERE table2.something = 1
Indeed both styles should have the same execution pan under the hood.

How to implement SQL joins without using JOIN?

How does one implement SQL joins without using the JOIN keyword?
This is not really necessary, but I thought that by doing this I could better understand what joins actually do.
The basic INNER JOIN is easy to implement.
The following:
SELECT L.XCol, R.YCol
FROM LeftTable AS L
INNER JOIN RightTable AS R
ON L.IDCol=R.IDCol;
is equivalent to:
SELECT L.XCol, R.YCol
FROM LeftTable AS L, RightTable AS R
WHERE L.IDCol=R.IDCol;
In order to extend this to a LEFT/RIGHT/FULL OUTER JOIN, you only need to UNION the rows with no match, along with NULL in the correct columns, to the previous INNER JOIN.
For a LEFT OUTER JOIN, add:
UNION ALL
SELECT L.XCol, NULL /* cast the NULL as needed */
FROM LeftTable AS L
WHERE NOT EXISTS (
SELECT * FROM RightTable AS R
WHERE L.IDCol=R.IDCol)
For a RIGHT OUTER JOIN, add:
UNION ALL
SELECT NULL, R.YCol /* cast the NULL as needed */
FROM RightTable AS R
WHERE NOT EXISTS (
SELECT * FROM LeftTable AS L
WHERE L.IDCol=R.IDCol)
For a FULL OUTER JOIN, add both of the above.
There is an older deprecated SQL syntax that allows you to join without using the JOIN keyword.. but I personally find it more confusing than any permutation of the JOIN operator I've ever seen. Here's an example:
SELECT A.CustomerName, B.Address1, B.City, B.State, B.Zip
FROM dbo.Customers A, dbo.Addresses B
WHERE A.CustomerId = B.CustomerId
In the older way of doing it, you join by separating the tables with a comma and specifying the JOIN conditions in the WHERE clause. Personally, I would prefer the JOIN syntax:
SELECT A.CustomerName, B.Address1, B.City, B.State, B.Zip
FROM dbo.Customers A
JOIN dbo.Addresses B
ON A.CustomerId = B.CustomerId
The reason you should shy away from this old style of join is clarity and readability. When you are simply joining one table to another, it's pretty easy to figure out what's going on. When you're combining multiple types of joins across a half dozen (or more) tables, this older syntax becomes very challenging to manage.
The best way to get a handle on the JOIN operator is working with it. Here's a decent visual example of what the different JOINs do:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
Some more info:
https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins
http://www.sqlservercentral.com/blogs/brian_kelley/2009/09/30/the-old-inner-join-syntax-vs-the-new-inner-join-syntax/
When SQL was an infant we didn't have "inner join" "left outer join" etc. All we did was list the tables like this:
FROM table1, table2, table3, .... tablen
Then we had a where clause that was like a novel in length, some of the conditions were for filtering the data, many of the conditions were to join tables, like this
FROM table1, table2, table2, .... tablen
WHERE table1.code = 'x' and table1.id = table3.fk and table2.name like 'a%' and table2.id = table1.fk and tablen.fk = table3.id and table2.dt >= '2014-01-01'
from this we hoped like heck we had all the tables nicely related and we crossed our fingers. The worst case scenario - which happened a lot - was that we forgot to include a table at all in the where clause. This was not nice because what we get when we do that is a "Cartesian product" (basically a multiplication of all rows by the number of rows in the table we missed).
Then came ANSI standard join syntax, and life was better. We now place the join conditions on the join - not in the where clause - and as a bonus the where clause is easier to understand.
I don't think you will find it easier to understand this ancient syntax, for example an outer join was join = bizarre(+) or maybe it was (+)bizarre = join (I try not to remember).
Try http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Left join using IS NULL does not work in DB2?

I am trying to left join to tables using a query like this
SELECT * FROM table1 a, table2 b WHERE (a.ID = b.ID OR b.ID IS NULL)
In Oracle, this is equivalent to a LEFT JOIN (and in other databases as well, afaik).
Doing the same thing in DB2 (z/OS) produces an inner join - the b.ID IS NULL clause has no effect on the result, removing it does not change anything.
Is there a way to make this work in DB2? Is this something that should work according to ANSI SQL?
PS: I am aware that I can use the JOIN syntax, I'm just interested in why this doesn't work and if there is a way around this.
You can use
SELECT a.*, b.*
FROM tbl1 a LEFT JOIN tbl2 b ON a.id=b.id;