Joining Table via Linked Server - sql

I have 2 servers with 2 different databases.
S1(Server) WITH DB1(Database) WITH T1(Table)
and
S2(Server) WITH DB2(Database) WITH T2(Table)
DB1 has a linked server to S2 DB2.
So i can run a query such as
SELECT * FROM T1
JOIN [S2].[DB2].[T2] T2
ON T1.[ID] = T2.[ID]
And this runs correctly.
However, how can i run it in the opposite direction
e.g.
SELECT * FROM T2
JOIN [S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID]
This errors with "Could not find server [S1].[DB1].[T1]"
Is there anyway i can join in this direction without having to create a linked server from S2 to S1?

This query:
SELECT *
FROM T2 JOIN
[S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
Presumes that T2 is local. You can sort of do a hybrid:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
It is possible to link a server to itself, so if you did that, you could run this identical query on both servers:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[S2].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
If you are trying to implement "equivalent" queries on two servers, then synonyms could help. You might be interested in the answers to this question.

Gordon's answer is more thorough but you can rewrite your second query as:
SELECT *
FROM [S2].[DB2].[T2] T2
JOIN T1
ON T2.ID = T1.ID

Related

Sample SQL Join Code from Famous Sites Doesn't Validate?

Here's sample SQL Full Outer Join code from w3schools:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Testing it in online validators (https://www.piliapp.com/mysql-syntax-check/ and https://www.eversql.com/sql-syntax-check-validator/) throws an error:
You have an error in your SQL syntax; it seems the error is around: 'OUTER JOIN table2 ON table1.column_name = table2.column_name' at line 5
w3resource has this sample SQL code:
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Sample code found on SO is pretty much the same code and throws the same error in the validators and on my dev system.
Does that make any sense?
What is the correct SQL for this?
Those query are MSSQL Query not mysql and that two sites are mysql query validator.
You don't have FULL JOINS on MySQL, but you can sure emulate them.
sample code to emulate FULL OUTER JOIN on MYSQL :
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
WHERE t1.id IS NULL

Join 2 tables from different DB

How can I join 2 tables from different DB with SQL Server?
Simply just use multipart identifier names if they are in the same server.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [database_2].[dbo].[table_2] t2
ON t1.id = t2.id
If not you can use linked servers and 4-part names.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [linked_server].[database_2].[dbo].[table_2] t2
ON t1.id = t2.id

SQL - alternative to left outer join

There is a standard way in SQL to conut a number of rows joined to one table acepting also the 0?
That is one example :
SELECT t1.id, COUNT(t2.*)
FROM t1 LEFT OUTER JOIN t2 ON ( t1.id = t2.id )
GROUP BY t1.id
I need a alternative because i use odbc with different databases, and on some databases the left join aren't supported.
SELECT
t1.id,
(SELECT COUNT(*) FROM t2 WHERE t2.id = t1.id) as t2_count
FROM t1
Two Options:
Option 1: use the (+) operator:
SELECT t1.id, COUNT(t2.*)
FROM t1, t2
WHERE t2.id(+) = t1.id
GROUP BY t1.id
I don;t know if it works on all drivers. Option 2 that will work with all drivers is to create a view and create the view instead.

Join query where table references itself

I'm using Oracle 10, but the best way to ask this question is with an example.
select *
from t1, t2
where t1.id = t2.id
and t1.otherID = (select max(otherID)
from t2
where id = THE ID FROM THE OUTER QUERY T1
)
I think you see where I'm trying to go with this. I need to reference t1 in the subquery to join it to the max of t2.
I need to know how to create a query like this.
"THE ID FROM THE OUTER QUERY T1" is where my confusion is.
I tried using t1.id, but did not get results.
Try the following
select t1.*, t2.*
from t1
join t2 on t1.id = t2.id
join (select id, max(otherID) as max_otherID
from t2
group by id
) a ON a.id = t1.id and a.max_otherID = t1.otherID
Using a sub-query on the join often gives better performance than using it in the where clause.

Compound SQL Join?

I'm trying to construct a SQL query that I think requires multiple JOIN's, but I don't know the syntax.
Here is a rough example of the Tables (with column names) for each.
T1 (key, name)
T2 (key, fkeyT1)
T3 (key, fkeyT2)
I want to get all the rows from T3 that are linked to rows in T2 that are linked to rows in T1 with a given name.
I figure I'll need at least 2 JOIN's; I've got the first JOIN I think:
SELECT *
FROM T3 INNER JOIN T2
ON T3.fkeyT2 = T2.key
I figure I'll need to take these results and do another JOIN with T1 but I'm not sure of the syntax.
You probably want something like
SELECT *
FROM t3 INNER JOIN t2 ON (t3.fkeyT2 = t2.key)
INNER JOIN t1 ON (t2.fkeyT1 = t1.key)
WHERE t1.name = 'Foo'