Join using same table related to other tables - sql

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;

Related

what operation does "select from table1, table2 " imply? [duplicate]

This question already has answers here:
Select from Table1, Table2
(3 answers)
Closed 4 years ago.
I know different joins, but I wanted to know which of them is being used when we run queries like this:
select * from table1 t1, table2 t2
is it full outer join or natural join for example?
Also does it have a unique meaning among different databases or all do the same?
UPDATE: what if we add where clause ? will it be always inner join?
The comma in the from clause -- by itself -- is equivalent to cross join in almost all databases. So:
from table1 t1, table2 t2
is functionally equivalent to:
from table1 t1 cross join table2 t2
They are not exactly equivalent, because the scoping rules within the from clause are slightly different. So:
from table1 t1, table2 t2 join
table3 t3
on t1.x = t3.x
generates an error, whereas the equivalent query with cross join works.
In general, conditions in the WHERE clause will always result in the INNER JOIN. However, some databases have extended the syntax to support outer joins in the WHERE clause.
I can think of one exception where the comma does not mean CROSS JOIN. Google's BigQuery originally used the comma for UNION ALL. However, that is only in Legacy SQL and they have removed that in Standard SQL.
Commas in the FROM clause have been out of fashion since the 1900s. They are the "original" form of joining tables in SQL, but explicit JOIN syntax is much better.
To me, they also mean someone who learned SQL decades ago and refused to learn about outer joins, or someone who has learned SQL from ancient materials -- and doesn't know a lot of other things that SQL does.
demo: db<>fiddle
This is a CROSS JOIN (cartesian product). So both of the following queries are equal
SELECT * FROM table1, table2 -- implicit CROSS JOIN
SELECT * FROM table1 CROSS JOIN table1 -- explicit CROSS JOIN
concerning UPDATE
A WHERE clause makes the general CROSS JOIN to an INNER JOIN. An INNER JOIN can be got by three ways:
SELECT * FROM table1, table2 WHERE table1.id = table2.id -- implicit CROSS JOIN notation
SELECT * FROM table1 CROSS JOIN table2 WHERE table1.id = table2.id -- really unusual!: explicit CROSS JOIN notation
SELECT * FROM table1 INNER JOIN table2 ON (table1.id = table2.id) -- explicit INNER JOIN NOTATION
Further reading (wikipedia)

Right join or which type of join?

I have 4 tables.
1 with just sitenames.
3 tables, which contains sitenames and the ammount of hits on them for different user types.
I need to make a report on the number of hits on thoose sites for each user type like this
Site Userype1hits Userype2hits Userype3hits
so in the select part I neeed to crosscheck with a table called noanswer
like
select *
from table
where site in (select site from noanswer)
So from what I understand I need to use join, and in this case right join?
How do I do with join in this query?
You would use left join:
select sn.*, t1.cnt, t2.cnt, t3.cnt
from sitenames sn left join
table1 t1
on t1.name = sn.name left join
table2 t2
on t2.name = sn.name left join
table3 t3
on t3.name = sn.name;
Your question is vague on the field names.
A left join keeps all rows in the first table and matches rows in the subsequent tables. It is much more commonly used than right join, probably because it is easier to read the logic thinking "I'll keep all of these rows". A series of right joins actually keeps all rows in the last table, so you have to wait to see which rows stay.
I did like this, googled some further as I did not get it to work even with the answer here, joins are totaly new to me. The internalusers, external and so on is just fake to post what I did here as I cant use the real names.
select s.Site,s2.Total_hits as 'Internalusers',s3.Total_Hits as 'ExternalUsers',
s4.Total_hits as 'Comapany2users' from Database.Table as S
left outer join HITAnalyze.dbo.Internal as s2 on s2.site = s.Site
left outer join HITAnalyze.dbo.External1 as s3 on s3.site = s.Site
left outer join HITAnalyze.dbo.Company2 as s4 on s4.site = s.Site

what is the use of right join exactly as we can get the same result from left join [duplicate]

This question already has answers here:
When or why would you use a right outer join instead of left?
(11 answers)
Closed 6 years ago.
I want to know that what is the use of right join exactly as we can get the same result from left join by interchanging the tables.
So let's take an example here -
Suppose i need to join two tables TAB_A and TAB_B with right join as below to get the result -
SELECT * FROM TAB_A RIGHT JOIN TAB_B
but i can also get the same result of query by using left join also instead of right join as below
SELECT * FROM TAB_B LEFT JOIN TAB_A
So my question is what is the purpose of right join in sql exactly, is there any performance related comparision or anything that can not be done by left join?
Suppose you need to do a double join like this:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.name = table2.name1
RIGHT JOIN table3 ON table2.position = table3.job;
that is when the RIGHT JOIN is useful...
Regards
In case of only tow table you can switch the tables to get the same result from right and left join. But if you are having more than two table and you need to put left join on few tables right join on few tables so in that case you need both the joins.
As stated in http://dev.mysql.com/doc/refman/5.7/en/outer-join-simplification.html
At the parser stage, queries with right outer joins operations are converted to equivalent queries containing only left join operations.
Using RIGHT JOIN will cause parser conversions, but it should be negligible in pracitce.

MS Access Inner Join On 3 Tables with the same field_name

I'm doing an assignment for class, and I'm at my whits end. Basically, I need to write a query that uses INNER JOIN's to combine all the data in 4 tables, while avoiding having titles columns with identical names (Ie Table1.Master_Number and Table2.Master_Number). The literal instructions are as follows:
Step 1: Create a view of all the columns (only list the columns once if the columns are duplicated in the tables) in all the tables of the Lecture 5 database. Save this query as Lecture_5_View and us this query to build the following 3 queries. In the “FROM” clause, you MUST use an “INNER JOIN” in this query. Remember ACCESS does not support the “CREATE VIEW” command so you must create a select query. `
Here is a screenshot of the database, that shows all the headings and column headings.
Based on other posts like this, I thought that it would be pretty simple. I have this so far, but it does not want to run
(ie Syntax error(missing operator) in query expression 'Table1.Master_Number = Table2.Master_Number INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number)
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Master_Number = Table2.Master_Number
INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number
Where am I going wrong so far and how to I accomplish this query?
Much thanks,
Josh
With Access you need to use parentheses when doing more than one join:
SELECT *
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This is essentiall spliting down you query so you have at most one join per section, i.e. Your First query is:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number;
Then you have:
SELECT *
FROM YourFirstQuery
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This differs slightly from subqueries as you are still able to reference all fields from Table1 and Table2.
EDIT
To avoid duplicating columns you will need to explicitly list the columns you want (although you should be doing this anyway):
SELECT Table1.Master_Number,
Table1.Asset_Tag,
Table1.Serial_Number,
Table2.Last_Name,
Table2.First_Name,
Table4.Office_Number,
Table4.Location,
Table4.Department
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
Try this:
SELECT * FROM ((Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number)

Are there any succinct approaches to this left join?

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