triple join using sqlite - sql

Let's say we have 3 tables:
table1: firstname, id
table2: id, lastname
table3: lastname, height
and we want to display: firstname, height
I'd likely use full join for that:
select firstname, height from
(select firstname, lastname ln from table1
full join table2 on table1.id=table2.id)
full join table3 on ln=table3.lastname
but in sqlite, full and right joins do not exist and I need to some how use union to get it.
Any ideas, how to do it?

Query:
SELECT firstname, height
FROM table1 INNER JOIN table2 ON table1.id=table2.id
INNER JOIN table3 ON table2.lastname=table3.lastname

you need a JOIN here between three tables
SELECT T1.firstname, T2.lastname, T3.height
FROM Table1 T1
JOIN Table2 T2
on T1.id = T2.id
JOIN Table3 T3
on T3.lastname = T2.lastname

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

join two select statements side by side Sql

I have two select query statements. I dont know to create table here.
Below are two tables. Id is same, but Fname and Lname is different.
Table1 have two columns
id Fname
1 Ahila
Table2 have two columns
id Lname
1 Kavitha
Output table should be single row with id and Fname and Lname : 1 and Ahila and Kavitha respectively
Please help me on this.
you need to use a join to join the two tables together, something like:
select t1.id, t1.Fname, t2.Lname
from Table1 t1
inner join Table2 t2 on t1.id = t2.id
Use JOIN statement
SELECT T1.id id , T1.Fname Fname, T2.Lname Lname
FROM table1 T1
JOIN table2 T2 ON T1.id = T2.id
select Fname
from Table1
join Table2
on Table1.id = Table2.id
where id = 1

SQL LEFT JOIN with WHERE class

there are two tables TABLE1 and TABLE2 in TABLE1 there are records which does not exist in TABLE2 with left join below i wanted to query all records which are in TABLE1 if the record does not exist in table2 however.
Note: about WHERE class in my code that is required this is because, there can be several records in the name of 'IN PROGRESS' in TABLE2 with one record in the name of 'GRADUATED' i wanted to distinct records based on table 1 ID that if there is any record in the name of 'GRADUATE' it should show only that else it should show inprogress.
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED'
I see some odds with your query:
exists part are not related with you main query. I think you need some relation
distinct in part not exists are not needed
You filter columns with the same conditions as filter main row set
As I understand you want to get all rows from table1 with state 'GRADUATED' int table2 and any row from table1 where rows in table2 are not exists or state not equal 'GRADUATED'
SELECT DISTINCT
t1.ID,
t2.TRAINING_STATUS_CHECK
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.ID = t2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT NULL /*its not nesessary what you need*/
FROM TABLE1 sub_t1
JOIN TABLE2 sub_t2 ON sub_t1.ID = sub_t2.FK_ID_CLASS /* left join replaced to inner */
WHERE sub_t2.TRAINING_STATUS_CHECK = 'GRADUATED'
AND sub_t1.ID = t1.ID /*relation with outer query*/
)
OR t2.TRAINING_STATUS_CHECK = 'GRADUATED'
where the relatonship between tables does not exist - but only if the comparison involves rows in table that are not 'graduated' (I think)
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND TABLE2.TRAINING_STATUS_CHECK <> 'GRADUATED'
WHERE TABLE2.FK_ID_CLASS IS NULL
Not sure about your question but if you want all the records from table 1 who are not in table 2, you just have to do this :
SELECT TABLE1.ID
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.FK_ID_CLASS IS NULL
Try this:
SELECT DISTINCT TABLE1.ID, TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND (NOT EXISTS (SELECT 1
FROM TABLE2 t
WHERE TABLE1.ID = t.FK_ID_CLASS
AND t.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
For the record, conditions on the right table of a LEFT JOIN need to be placed inside the ON() clause or the join will transfer into an INNER JOIN due to NULL comparison.
It seems to me you have three distinct cases that can be "ORed together" using UNION; personally I find keeping all three separated like this makes things much easier to read and understand:
--- ID with GRADUATED exists in TABLE2
( SELECT ID, 'GRADUATED' AS TRAINING_STATUS_CHECK
FROM TABLE1
INTERSECT
SELECT FK_ID_CLASS, 'GRADUATED'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID without GRADUATED exists in TABLE2
( SELECT ID, 'IN PROGRESS'
FROM TABLE1
MINUS
SELECT FK_ID_CLASS, 'IN PROGRESS'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID does not exist in TABLE2
( SELECT ID, '{{NONE}}'
FROM TABLE1
WHERE ID NOT IN ( SELECT FK_ID_CLASS FROM TABLE2 ) );

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)

Retrieving data from the table in SQL Server 2008

I have 4 tables
table1 (id, stateId(fk), name, carId(fk))
table2 (stateId(pk), state, countryId(fk))
table3 (countryId, country, currency)
table4 (carId, car)
I want to retrieve name, state, country, car from the above tables through stored procedure using joins. And if some other easy way then please tell.
thanks.
A simple JOIN should do:
CREATE PROCEDURE schema.YourStoredProcedureName
AS
SELECT t1.name, t2.state, t3.country, t4.car
FROM table1 t1
JOIN table2 t2 ON t1.stateId = t2.stateId
JOIN table3 t3 ON t2.countryId = t3.countryId
JOIN table4 t4 ON t1.carId = t4.carId
GO
No other easy method. Join is basic and easy (http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx)
select tbl1.name, tbl2.state, tbl3.country, tbl4.car
From table1 tbl1
inner join table2 tbl2 on tbl1.stateid =tbl2.stateid
inner join table3 tbl3 on tbl2.countryid = tbl3.countryid
inner join table4 tbl4 on tbl1.carId = tbl4.carId