SQL query to get output from referring data in single table - sql

I have 3 tables and the expected result like this:
This is the example table:
This is the expected output from the table:
To get the result I must refer the id and parentid from table2.
How to write a SQL query based on the pictures above?

You must join table1 to 2 copies of table2, the first is directly linked to the id of table1 and the 2nd is linked to the parentid of table2.
Then join to table3.
SELECT t1.*, t3.city
FROM table1 t1
INNER JOIN table2 c ON c.id = t1.id
INNER JOIN table2 p ON p.id = c.parentid
INNER JOIN table3 t3 ON t3.id = p.parentid

Related

SQL Query of 2 tables finding missing value in table 2

I have 2 tables in SQL.
-Table 1-
Name
ID
-Table 2-
ID
Program
Table 1 only holds a single record for each company. In table 2, there can be multiple entries for a record in table 1. So I could join the two tables together on ID = ID and get anywhere from 0 to 7 results. I need to query the 2 tables where I am looking for any company from table 1 who is not in a specific program but they can be in other programs. I know this is easy to do but I just can't get it to work for me.
SELECT *
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.id = t2.id
AND t2.program = 'asdf'
WHERE
t2.id IS NULL
Just use an OUTER JOIN place the program requirement on the join condition and then say where a record for the Table2 doesn't exist.
Trying to make this a little less ambiguous and adapting to your specific companies and programs:
SELECT *
FROm
Companies c
LEFT JOIN Programs p
ON c.Id = p.CompanyId
AND p.Program = 'some title'
WHERE
p.Program IS NULL
And the NOT exists method:
SELECT *
FROM
Companies c
WHERE
NOT EXISTS (SELECT 1
FROM Programs p
WHERE
p.CompanyId = c.Id
AND p.Program = 'some title')
I don't show the NOT IN method because I don't recommend it typically.
You want to get companies where they aren't in a program that you specify in the query? This is what I came up with:
select t1.*
from Table1 t1
left join Table2 t2 on t1.ID = t2.ID
where coalesce(t2.Program, '') <> 'Name of Program'
or
where coalesce(t2.Program, '') not in (<comma-delimited list of programs>)
SELECT t1.Name
FROM Table1 t1
LEFT JOIN
Table2 t2
ON t1.id = t2.id
WHERE t2.program IS NULL;

MS Access 2007 Inner Join on Same Table

I have a table t1. It has columns [id] and [id2].
Select count(*) from t1 where id=1;
returns 31,189 records
Select count(*) from t1 where id=2;
returns 31,173 records
I want to know the records where id2 is in id=1 but not in id=2.
So, I use the following:
Select * from t1 a left join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1
And b.id2 Is Null;
It returns zero records.
Using an inner join to see how many records have id2 in common, I do...
Select * from t1 a inner join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1;
And that returns 31,060. So where are the extra records in my first query that don't match?
I am sure I must be missing something obvious.
Sample Data
id id2
1 101
1 102
1 103
2 101
2 102
My expected results is to find the record with '103' in it. 'id2' not shared.
Thanks for any help.
Jeff
You are attempting to do what is generally called an exclude join. This involves doing a LEFT JOIN between two tables, then using a WHERE clause to only select rows where the right table is null, i.e. there was no record to join. In this way, you select everything from the left table except what exists in the right table.
With this data, it would look something like this:
SELECT
t1.id,
t1.id2
FROM test_table t1
LEFT JOIN
(SELECT
id,
id2
FROM test_table
WHERE id = 2) t2
ON t2.id2 = t1.id2
WHERE t1.id = 1
AND t2.id IS NULL --This is what makes the exclude join happen
And here is a SQLFiddle demonstrating this in MySQL 5.7 with the sample data you provided.
I think maybe Access changes the left join to an inner join when you add a where clause to filter rows (I know SQL Server does this), but if you do the filtering in derived tables it should work:
select
a.*
from
(select * from t1 where id = 1) a
left join
(select * from t1 where id = 2) b
on a.id2 = b.id2
where b.id2 is null

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

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)

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