SQL joining of two different queries - sql

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;

Related

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

force table name before field in select

SELECT *
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
Both tables have a date_createField, so when I use select *, date_createField is returned twice. I could solve this by changing my select to:
SELECT T1.date_createField, T2.date_createField
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
But is it possible to not specify the specifik fields (keep select *), and force the table name in front of the property?
I'm having this problem because I'm joining 2 tables with a lot of columns and some columns have the same name. I would like to use select * and still have a distinction between columns present in both tables. Is this possible?
This is not possible you need to specify the column names or accept all columns. If this is a query you will execute often make a view which can be reused.
May this will help you
SELECT T1.*, T2.* FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK

How to join 3 tables by a single sql query?

I have 3 tables.
Table1: Group_Code, Group_Name,companyID;(PK: Group_Code)
Table2: PartyID,GroupID,companyID;(FK: GroupID, PK:PartyID)
Table3: VendorID, companyID;(FK:VendorID)
I want to fetch Group_Name from Table1 for all VendorID of Table3. How can I do this?
here i write a code. But it shows "Syntex error in FROM clause." My database is in ms access.
select Group_Name from Table1 join Table2 on Table1.Group_Code= Table2.GroupID
join Table3 on Table2.PartyID=Table3.VendorID
try this !!!
SELECT table1.group_name FROM (table1
INNER JOIN ON table1.group_code=table2.groupid)
INNER JOIN table3 ON table2.partyid=table3.vendorid
GROUP BY table1.group_name
select Group_Name from Table1
join Table2 on Table1.Group_Code = Table2.GroupID
join Table3 on Table2.PartyID = Table3.VendorID
SELECT table1.group_name FROM table1 join table2
ON table1.group_code=table2.groupid
join table3 ON table2.partyid=table3.vendorid
error becoz you didnt take the group name DB instance ?
You Can do this :
select Table1.Group_Name, Table3.VendorID from Table1 join Table2 on Table1.Group_Code= Table2.GroupID join Table3 on Table2.PartyID =Table3.VendorID
If you are data has been stored in proper relationship. the query should get you going. :)
use this code for question,
Select Table1.Group_Name from ((Table1
left join Table2 on Table1.Group_Code=Table2.GroupID)
left join Table3 on Table2.PartyID=Table3.VendorID)

SQL to select rows where at least one row in a group, has a corresponding entry in another table

I have two tables
Table1: FieldA, FieldB
Table2: FieldA
Table1 is grouped by FieldB, and FieldA is the link between the two tables.
For each grouping in Table1, if all rows in that group do not have an entry in Table2, then return no rows corresponding to this group. If at least one row in the group has an entry in Table2, then return all rows in the group.
Is this kind of query possible?
Thanks
Simon
it sounds like you need to do a simple inner join:
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.FieldA=t2.FieldA;
I'm unclear from your question if the values in Table2 are unique. If they are not unique then this subquery might work better:
SELECT * FROM Table1 WHERE FieldA in (SELECT distinct(FieldA) FROM Table2);
If I understand your problem correctly, this JOIN would to do it;
SELECT DISTINCT t1a.*
FROM Table1 t1a
JOIN Table1 t1b
ON t1a.FieldB = t1b.FieldB
JOIN Table2 t2
ON t2.FieldA=t1b.FieldA;
Demo here.
SELECT *
FROM Table1
WHERE FieldB IN (
SELECT t1.FieldB
FROM Table1 t1
JOIN Table2 t2
ON t1.FieldA = t2.FieldA
GROUP BY t1.FieldB
)

Join Unlike Tables

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