I have the following tables
t1: t2: t3:
id id f1 id f2
1 1 a 3 a
2 2 b 4 b
3 3 c 5 c
4
5
and I am trying to get the following result
t4:
id f1 f2
1 a
2 b
3 c a
4 b
5 c
I am using the following query
SELECT t1.id, t2.f1, t3.f2
FROM (t1
LEFT JOIN t2 ON t1.id = t2.id) AS a
LEFT JOIN t3 ON t1.id = t3.id
It works but the query takes a while to run. Is there a better way to do this? Thanks
To offer an alternative which doesn't involve additional tables: you could perhaps try structuring the SQL to perform a nested SELECT query such that only tables are joined for each SELECT query, but I doubt that this would offer a drastic improvement over what you have:
SELECT tmp.id, tmp.f1, t3.f2
FROM
(SELECT t1.id, t2.f1 FROM t1 LEFT JOIN t2 ON t1.id = t2.id) AS tmp
LEFT JOIN t3 ON tmp.id = t3.id
Related
I have two tables identical to each other like below
table 1
col1
1
2
3
4
5
table 2
col1
1
2
3
4
5
is there way to write a SQL query to join every row of table 1 to every row of table 2?
Do you want a Cartesian product? If so, use cross join:
select t1.col1, t2.col2
from table1 t1 cross join
table2 t2;
It sounds like an inner join as they are identical tables:
select t1.col, t2.col, ...
from table t1
inner join t2 on t1.col = t2.col
Let's say i have:
Table1:
ID Name
1 Ann
2 Mike
3 Stan
4 Kyle
Table2:
Pair ID Person1ID Person2ID
1 1 2
2 3 4
I want to select pairs, but with names instead of IDs, so this would be the output:
1 Ann Mike
2 Stan Kyle
I imagine a simple:
inner join Table1 on Table1.ID=Table2.Person1ID
won't work, because I want both of them, not only one.
I'm pretty new to SQL so i'm sorry if there is a simple answer.
You have to join twice and use alias to diference the tables
SELECT t2.PairID, A.Name, B.Name
FROM Table2 t2
JOIN Table1 A
ON t2.Person1ID = A.ID
JOIN Table1 B
ON t2.Person2ID = B.ID
You can join sub query like below:
Select p.PairID, p.Name, q.Name
from
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person1ID) p
inner join
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person2ID) q
on p.PairID = q.PairID
See the result here in the demo.
I'm not sure how to explain what I need but here's the data first:
Table 1
District
-1
3
2
1
3
Table 2
ID ID_Name
1 Main 1
2 Main 2
3 Main 3
How do I join the tables so that it looks like this?
District
-1
Main 3
Main 2
Main 1
Main 3
I'm assuming the second column is named Name for this, but you can do it with a COALESCE and a LEFT JOIN:
Select Coalesce(T2.Name, Str(T1.District)) As District
From Table1 T1
Left Join Table2 T2 On T1.District = T2.Id
assuming table 2 have
Table 2
ID col2
1 Main 1
2 Main 2
3 Main 3
you could use a left join
select table1.Distric, table2.col2
from table1
left join table2 on table1.dictrict = t2.ID
order by table2 col2
You can use left join:
Select coalesce(t2.col, t1.District) from table1 t1
left join table2 t2 on t1.District = t2.Id
Why does this query produce duplicates in some scenarios?
Table_1
ID
1
2
3
Table_2
ID
1
2
4
Table_3
ID
1
3
4
Query:
SELECT COALESCE(Table_1.ID, Table_2.ID, Table_3.ID)
FROM Table_1
FULL OUTER JOIN TABLE_2
ON Table1.ID=Table_2.ID
FULL OUTER JOIN TABLE_3
ON Table1.ID=Table3.ID;
Result:
1
2
3
4
4
The query duplicates all values where T1 is null and T2/T3 share the same value. Duplicates are removed for any other combination.
This is a little bit hard to explain. If you show the other ids, you will see the full range of what happens:
"coalesce" "id1" "id2" "id3"
1 1 1 1
2 2 2 .
3 3 . 3
4 . 4 .
4 . . 4
You can see the results here.
So, You get one row because t1 & t2 create a row with t2.id = 4 and t1.id = null when they don't match. Then, you get the same thing when t3.id = 4. The comparison is to t1.id -- so you get another row. There is no comparison to t2.id.
I suspect that you intend logic more like this:
select coalesce(t1.id, t2.id, t3.id)
from t1 full join
t2
using (id) full join
t3
using (id);
Here is the SQL Fiddle.
Hard to make a good title for this (feel free to edit), but hope it will make more sense ..
Say I have the following tables:
t1:
i1 v1
_________
1 bob
2 NULL
3 sam
4 NULL
5 kenny
5 NULL
t2:
i2 v2 item
______________
1 bob prod_1
2 nick prod_2
3 sam prod_3
4 jj prod_4
5 kenny prod_5
5 cartman prod_6
I need to JOIN the tables on t2.i2 = t1.i1 but only where t2.v2 does not exist in t1.v1. So I'm trying to get the following results:
Goal:
i2 v2 item
__________________
2 nick prod_2
4 jj prod_4
5 cartman prod_6
This query below was my first attempt, and it's not working, so I'm trying to find a working and more efficient solution with JOINs.
SELECT * FROM t2
WHERE v2 NOT IN (
SELECT v1 FROM t1 WHERE t2.i2 = t1.i1
)
Your query is fine, although I would use NOT EXISTS:
SELECT dm2.*
FROM web.delete_me2 dm2
WHERE NOT EXISTS (SELECT 1
FROM web.delete_me1 dm1
WHERE dm2.some_int2 = dm1.some_int1 and dm2.some_var2 = dm1.some_var1
);
Although you can write this as a join, this version should be at least as good performance wise. You want an index on web.delete_me1(some_int1, some_var1).
A LEFT JOIN should be enough
select * From t2
left join t1 on t2.i2 = t1.i1 and t1.v1= t2.v2
where t1.i1 is null
SELECT DISTINCT T1.*, T2.*
FROM T1
JOIN T2
ON T1.ID = T2.ID
LEFT JOIN T2 AS T2NO
ON T2NO.NAME = T1.NAME
WHERE T2NO.NAME IS NULL
or try this
SELECT DISTINCT T2.*
FROM T2
JOIN T1
ON T1.ID = T2.ID
LEFT JOIN T1 AS T1NO
ON T1NO.NAME = T2.NAME
WHERE T1NO.NAME IS NULL
Do you need all records from t2 where i2 is in t1 and v2 is not?
If so, you can write this query
select * from t2
where i2 in (select i1 from t1)
and v2 not in (select v1 from t1 where v1 is not null)
for not in to work as we need, v1 shouldn't be null