Join Unlike Tables - sql-server-2005

I have 2 unlike tables and a large set of subqueries that have a key for each of those two tables. I need to join the two tables to each subquery.
Table 1
Table1ID
Table 2
Table2ID
Subqueries
Table1ID
Table2ID
Is there any way to join everything together?
I have tried something similar to
SELECT Table1.Table1ID, Table2.Table2ID
FROM Table1, Table2
LEFT JOIN (SELECT Table1ID, Table2ID FROM ....) q1 ON Table1.Table1ID = q1.Table1ID AND Table2.Table2ID = q1.Table2ID
...

This following query will select all fields from a join of all three tables on the respective table IDs:
SELECT *
FROM Table1 t1
INNER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
INNER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id
If you need absolutely all records from both Table1 and Table2, whether they are joined via the Subqueries table, then you can change the join to FULL OUTER:
SELECT *
FROM Table1 t1
FULL OUTER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
FULL OUTER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id

Related

SQL joining of two different queries

Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;

How to query the specific vales in columns form different tables(SQLite)?

I have two tables, table1 and table2. table1 has 'id' column. table2 has 'id' and 'quantity' columns. I want to compare the same values under 'id' columns from both tables and show the value under 'quantity' column form table2.
The simple logic is "select id=1 from table1 compare with select id=1 from table2, then show the quantity value from table2". Is there any way to query this statement?
Join the tables:
SELECT *
FROM
table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
Using INNER JOIN will cause the results to show only rows where the same ID is present in both tables. If you want to show all rows from table 1 and matching rows from table 2 plus blanks for any rows where there is no table 2 id for a particular table 1 id, then use LEFT JOIN instead of INNER JOIN.
If you only want certain columns in your output you can list them:
SELECT t1.id, t2.quantity
...
etc
More about joins - for future learning
In most other database systems there exists the corollary of LEFT join; RIGHT join. If you want all rows from table 2 plus matching rows from table 1, and blanks where there is no related table 1 row, use RIGHT JOIN. SQLite doesn't support RIGHT joins, so you'll have to rewrite your query (swap the table names around) so t2 is on the LEFT. It's the table name that matters, not the order of appearance in the ON section:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
--all rows from t2 plus any matching rows from t1
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Eventually you'll come across a FULL [OUTER] JOIN which is "all rows from t1 and any matching from t2, plus any additional rows from t2 and their possible matches from t1". SQLite doesn't support this either, but it can be emulated. Ordinarily it's emulated with a LEFT JOIN UNION RIGHT JOIN, but as SQLite doesn't support RIGHT, you'll have to emulate it with two LEFT joins, one with the tables swapped round:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
UNION
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Cor. All that for the sake of being able to say this in e.g. SQLServer:
SELECT *
FROM
table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = t2.id

SQL Joining Tables

I am trying to to join two tables in order to replace the numeric values of one from the other, the problem is that i have two columns in the same table. See this example to make it clear :
Table_1 t1_ID INT, t1_Name VARCHAR(20)
Table2 t2_ID INT , ONE Table_1 , TWO Table_1
in table 2 i store the id and i want to make a join in order to replace these id's with t1_Names.
I have tried this structure but it gives wrong answers
Select *
FROM table1
JOIN table2 ON table1.id=table2.table1_id
JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id
You can join the same table twice, but you need to give this table different names (alias name)
Select t2.id, t1_1.name, t1_2.name
FROM table2 t2
JOIN table1 t1_1 ON t1_1.id = table2.table1_id
JOIN table1 t1_2 ON t1_2.id = table2.table2_id

Selecting a mapping table with fields from two other tables

I have a mapping table referring to ids from two different tables. I would like to select the mapping table with each id being replaced by another field in the respective table.
To be a little more explicit: there are three tables with two columns each:
Table1 has a id (the primary key) and field1
Table2 has a id (the primary key) and field2
Table3 (the mapping table) has fields Table1_id (which takes values in Table1.id) and Table2_id (which takes values in Table2.id)
What I want is to get the content of Table3 with Table1.field1 and Table2.field2 as columns.
I know how to replace one of the columns in the mapping table with another column of one of the other tables, by using a inner join:
SELECT Table1.field1, Table3.Table2_id
FROM Table1
INNER JOIN Table3
ON Table1.id=Table3.Table1_id;
however I don't know how to do basically the same thing with both columns.
If i understood correctly you are trying to get field1 from Table1 and field2 from table 2. If so you just need to join the three tables
SELECT a.field1, c.field2
FROM Table1 a
INNER JOIN Table3 b
ON a.id=b.Table1_id
INNER JOIN Table2 c
ON b.Table2_id = c.id
Do another join.
SELECT Table1.field1, Table2.field
FROM Table1
INNER JOIN Table3
ON Table1.id = Table3.Table1_id
INNER JOIN Table 2
ON Table2.id = table3.table2_id;
You just need to join all three tables; something like
SELECT Table1.Field1, Table2.Field2
FROM Table3
JOIN Table1 ON Table1.Id = Table3.Table1_id
JOIN Table2 ON Table2.Id = Table3.Table2_id
I do not know if I understood correctly what you want to achieve but this should get you results from both tables joined by thier keys
SELECT Table1.field1, Table2.field2
FROM Table1
INNER JOIN Table3 ON Table1.id = Table3.Table1_id;
INNER JOIN Table2 ON Table2.id = Table3.Table2_id;

Joining empty table to return all rows

I have a table (Table1) which has a composite primary key(Column1 + Column2). I am using it as a foreign key in another table (Table2).
Now I want to a SELECT statement to select all records from Table1 and Table2.
But its returning me 0 rows, because table2 is Empty. I want all records from table1 and if it does not exist in table2, value of Columns in Table2 should be null.
I know, I only need to Join it. But I am not getting it right.
Thanks
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id = T2.FK
FK is your foreign key on the second table.
A Left Join will return all rows from table1 even if they don't exist in table2.
You need an outer join
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.column1 = table2.column1
AND table1.column2 = table2.column2
Left means preserve all rows from the left (first) table in the query.
You need a LEFT JOIN
SELECT Table1.*, Table2.*
FROM Table1
LEFT JOIN Table2 ON Table1.Column1 = Table2.Column2
Try that out.
Use LEFT JOIN for join you tables. See SQL SERVER JOINS to understand the concept.