SQL three table joins using two ids and another column - sql

I have three tables
t1 t2 t3
I want to select data from only two of the tables but need the ids and columns from other tables for reference.
How to select data from t3 and t2
WHERE t1.id = t2.id = t3.id
AND t1.fid = t2.fid = t3.fid
AND t1.type = 'abc'
id column will be the same value for all tables. fid column will have incremental fid's but need the ones where t1.type = 'abc' also
Would this work?
select data
from t3
select data
from t2
join on t1.id = t2.id and t2.id = t3.id
join on t1.fid = t2.fid and t2.fid = t3.fid and t1.type = 'abc'
where id = 1

Generally, this kind of problem can be solved using EXISTS as follows:
select data
from t2
join t1 on t1.id = t2.id and t1.fid = t2.fid
where exists (select 1 from t3 where t2.id = t3.id and t2.fid = t3.fid)
and t1.type = 'abc'
and t1.id = 1

You could try something like this:
select columns_that_you_want
from t1 x
inner join t2 y on x.id = y.id
inner join t3 z on y.id = z.id
where x.type = 'abc'
;
This will join the three tables, and filter just the type 'abc' that you want from table t1. Also you can add more filter on the where clause in case you need to filter more the result.

Related

Speed up a join with 2 keys

Can I speed this somehow up?
CREATE TABLE TEST AS
SELECT t1.Tech, t2.Coloar,t2.Car from Table1 t1
INNER JOIN Table2 t2 on (t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI) OR (t1.RA = t2.RA)
where...
If I create the table just with
(t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI)
or with this key
(t1.RA = t2.RA)
it takes seconds, but both together a couple of minutes and I have more and bigger tables to create and sometimes I need to LEFT JOIN this key pair, like
CREATE TABLE...
INNER JOIN...
LEFT JOIN on (t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI) OR (t1.RA = t2.RA)
Remove the OR from the ON clause:
CREATE TABLE TEST AS
SELECT t1.Tech,
COALESCE(t2.Color, tt2.Color),
COLAESCE(t2.Car, tt2.Car)
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI LEFT JOIN
Table2 tt2
ON t1.RA = tt2.RA AND t2.ID IS NULL
WHERE (t2.ID IS NOT NULL OR tt2.ID IS NOT NULL) AND
...
This should be able to take advantage of appropriate indexes for both the joins.
Try this
CREATE TABLE TEST AS
select t1.Tech, t2.Coloar,t2.Car
From Table1 t1
left join Table2 t2 on (case when t1.RA = t2.RA then 1 when t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI then 1 else 0 end = 1)

How to optimize multiple subqueries that are running for lookup tables

I have two looK up tables, and need is to fetch value by correlating the both tables.
As of now I am doing this with multiple sub queries and trying to find a easiest way.
SELECT
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'A') AND T1.ID = 'AA')as A,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'B') AND T1.ID = 'BB')AS B,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'C') AND T1.ID = 'CC')AS C,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'D') AND T1.ID = 'DD')AS D,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'E') AND T1.ID = 'EE')AS E,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'F') AND T1.ID = 'FF')AS F
FROM MYTABLE;
I assume you need something like this.
SELECT MAX ( CASE WHEN T1.ID = 'AA' AND T2.ID = 'A' THEN TYPE_NAME END ) as A,
MAX ( CASE WHEN T1.ID = 'BB' AND T2.ID = 'B' THEN TYPE_NAME END ) as B,
MAX ( CASE WHEN T1.ID = 'CC' AND T2.ID = 'C' THEN TYPE_NAME END ) as C,
..
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
I'll try something like:
SELECT
T1.TYPE_NAME,
T1.ID
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
WHERE
T1.ID||T1.ID = T2.ID
You could try rephrasing these queries as a single query consisting of a join between the two tables:
SELECT
T1.TYPE_NAME,
T1.ID
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
WHERE
(T1.ID = 'AA' AND T2.ID = 'A') OR
(T1.ID = 'BB' AND T2.ID = 'B') OR
(T1.ID = 'CC' AND T2.ID = 'C') OR
(T1.ID = 'DD' AND T2.ID = 'D') OR
(T1.ID = 'EE' AND T2.ID = 'E') OR
(T1.ID = 'FF' AND T2.ID = 'F');
You can keep track of to which subquery each returned record corresponds by checking the value of the ID column in the first table. This would serve as a marker for the source subquery.

postgresql join query

I have two tables T1 and T2
T1
-id
-columnA
-columnB
-columnC
T2
-id
-columnX
-columnY
-columnZ
I have a query like
Select t1.* t2.columnZ
from T1 t1
left join on T2 t2 on t1.id = t2.id
where t2.columnZ = 'test'
I want result like if "where t2.columnZ = 'test'" does not return any row then it should return value of columnZ as null value
Try:
SELECT t1.*
, t2.columnZ
FROM T1 t1
LEFT JOIN T2 t2
ON t1.id = t2.id
AND t2.columnZ = 'test'
You also missed a comma, and you had a misplaced on.

SQL: select from t1 all rows for which there's no corresponding in t2

Let's say I have a table t1 with only one column: id, and I have a table t2 with two columns: id and Memo. I need to select those id from t1, for which there is NO row in t2 that satisfies both of the following two conditions t1.id = t2.id and t2.Memo = 'myText'. How can I do that? I have tried using join, but that selects row that do satisfy some conditions, whereas I need the opposite.
SELECT *
FROM t1
WHERE NOT EXISTS (SELECT 1
FROM t2
WHERE t2.id = t1.id
AND t2.Memo = 'myText')
One way to do it is using LEFT JOIN:
select id
from t1
left join t2
on t1.id = t2.id and t2.Memo = 'myText'
where t2.id is null
I'm not good in understanding your question:
You mean those t1.id EXISTS in t2 BUT the corresponding t2.Memo <> 'myText'?
SELECT t1.id FROM t1 JOIN t2
ON t1.id = t2.id
HAVING t1.id NOT IN (SELECT id FROM t2 WHERE Memo = 'myText');
Or all t1.id either NOT EXISTS in t2 or EXISTS but Memo <> 'myText'?
SELECT id FROM t1 WHERE id NOT IN (SELECT id FROM t2 WHERE Memo = 'myText');

Combining INNER JOIN and LEFT JOIN

Edited
I'm not asking How to write good query but those 3 queries return same results.
Query 1
SELECT v1.id
FROM (
SELECT DISTINCT t1.id
FROM t1 LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id IS NULL
) v1 INNER JOIN (
SELECT DISTINCT t3.id
FROM t3 LEFT JOIN t4 ON t3.id = t4.id
WHERE t4.id IS NULL
) v2 ON v1.id = v2.id;
Query 2
SELECT DISTINCT t1.id
FROM (t1 LEFT JOIN t2 ON t1.id = t2.id)
INNER JOIN (t3 LEFT JOIN t4 ON t3.id = t4.id) ON t1.id = t3.id
WHERE t2.id IS NULL AND t4.id IS NULL;
Query 3
SELECT DISTINCT t1.id
FROM t1 LEFT JOIN t2 ON t1.id = t2.id
INNER JOIN t3 ON t1.id = t3.id LEFT JOIN t4 ON t3.id = t4.id
WHERE t2.id IS NULL AND t4.id IS NULL;
Queries are not hard coded by programmer but generate dynamically by user input.
For example when user inserts find id in t1 (but not in t2) and in t3 (but not in t4), his indention is Query 1. But currently my program generates Query 3 and it looks like OK. I'm wondering this query has a bug in some cases, so that should be changed like Query 2 or 1.
User input (shown above) is just example and converting user input to JOIN statement is difficult at last to me.
Thanks in advanced.
This is the original query:
SELECT v1.id
FROM (SELECT DISTINCT t1.id
FROM t1 LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id IS NULL
) v1 INNER JOIN
(SELECT DISTINCT t3.id
FROM t3 LEFT JOIN t4 ON t3.id = t4.id
WHERE t4.id IS NULL
) v2
ON v1.id = v2.id;
If I understand correctly, you want ids that are in t1 and t3, but not in t2 and t4.
I would express the second query as:
SELECT distinct t1.id
FROM t1 INNER JOIN
t3
on t1.id = t3.id LEFT JOIN
t2
on t1.id = t2.id LEFT JOIN
t4
on t1.id = t4.id
WHERE t2.id IS NULL AND t4.id IS NULL;
I read your original query as asking for "all ids in T1 which are in both T1 and T3 but not in T2 or T4". Is that correct? If so, my query would be:
SELECT DISTINCT t1.id
FROM t1
WHERE EXISTS (SELECT 1 FROM t3 WHERE t1.id = t3.id)
AND NOT EXISTS (SELECT 1 FROM t2 WHERE t1.id = t2.id)
AND NOT EXISTS (SELECT 1 FROM t4 WHERE t1.id = t4.id)
Not sure what the real objective is, nor which dbms is being targeted
-- oracle
SELECT id FROM ( SELECT id FROM t1 MINUS SELECT id FROM t2 ) a
INTERSECT
SELECT id FROM ( SELECT id FROM t3 MINUS SELECT id FROM t4 ) b
;
-- sql server
SELECT id FROM ( SELECT id FROM t1 EXCEPT SELECT id FROM t2 ) a
INTERSECT
SELECT id FROM ( SELECT id FROM t3 EXCEPT SELECT id FROM t4 ) b
;
SELECT c.id from (
SELECT t1.id
FROM t1 LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id IS NULL
UNION ALL
SELECT t3.id
FROM t3 LEFT JOIN t4 ON t3.id = t4.id
WHERE t4.id IS NULL
) c GROUP BY c.id HAVING count(*) >= 2
;
The next one is potentially quite efficient BUT has a caveat
-- conditions apply, assumes t2 and t4 cannot have ids not in t1 or t3 respectively
SELECT c.id from (
SELECT a.id from (
(SELECT ID FROM T1)
UNION ALL
(SELECT ID FROM T2)
) a GROUP BY a.id HAVING count(*) = 1
UNION ALL
SELECT b.id from (
(SELECT ID FROM T3)
UNION ALL
(SELECT ID FROM T4)
) b GROUP BY b.id HAVING count(*) = 1
) c GROUP BY c.id HAVING count(*) >= 2
;
Fiddles:
| Oracle
| SQL Server |
MySQL |