I have following query-
select * from
Table1 t1 , table2 t2 ,
(select idCol from table3) t3
My question is - Can I use table t2 inside subquery?
like this
select * from
Table1 t1 , table2 t2 ,
(select idCol, t2.nameCol from table3) t3
Obviously this gives error invalid identifier t2.nameCol
But if I write as follows, it give unnecessaru extra rows
select * from
Table1 t1 , table2 t2 ,
(select idCol, t2.nameCol from table3, table2 t2) t3
any other way to do this?
EDIT
Basically what I am trying to achieve is following
select * from
Table1 t1 , table2 t2 ,
(select
case
when t2.nameCol = 'ABC' then 'ABC'
else idCol
end idCol from table3) t3
To join a table only when certain criteria is met is called an outer join. Here is how:
select *
from table1 t1
inner join table2 t2 on <join criteria here>
left outer join table3 t3 on t2.namecol <> 'ABC' and <join criteria here>;
It may suffice in your case, however, to move the subquery to your SELECT clause:
select
t1.*,
t2.*,
case when t2.namecol = 'ABC' then
'ABC'
else
(select idcol from table3 t3 where <join criteria here>)
end
from table1 t1
inner join table2 t2 on <join criteria here>;
You can try below. I am sure you have some kind of join between t1 and t2. IN such case, you can add same at the end of the statement.
SELECT t1.*,
t2.*,
( SELECT CASE
WHERE t2.nameco1 = 'ABC' THEN 'ABC'
ELSE idcol
END idcol
FROM table3) idcol
FROM table1 t1,
table2 t2
Related
I have two tables that look like this:
Table 1:
name | event | country
Table 2:
name | event
There are no overlapping rows between Table 1 and Table 2, because of the 'event' column. I wanted to union Table 1 with Table 2 since there are no overlaps, but I also want to fill in 'country' for Table 2 using the values from Table 1, with 'name' as the Join key. How do I do this?
If the relation of the 2 tables is 1:1 you can use UNION ALL for table1 and a query that joins (with a left join just in case there are no matches) table2 to table1 to retrieve the value of the column country:
select t1.* from table1 t1
union all
select t2.*, t1.country
from table2 t2 left join table1 t1
on t1.name = t2.name
or with a correlated subquery:
select t1.* from table1 t1
union all
select t2.*, (select t1.country from table1 t1 where t1.name = t2.name)
from table2 t2
Try this
SELECT * FROM TABLE1 t1
UNION
Select t2.*, t1.country from table2 t2
Join table1 t1 on t2.name=t1.name
This sounds like a full join:
select *
from table1 t1 full join
table2 t2
using (name, event);
If your database doesn't support full join, you can use:
select t1.name, t1.event, t1.country
from table1 t1
union all
select t2.name, t2.event, null
from table2 t2
where not exists (select 1 from table1 t1 where t1.name = t2.name and t1.event = t2.event);
I have some set of records, but now i have to select only those records from this set which have theeir Id in either of the two tables.
Suppose I have table1 which contains
Id Name
----------
1 Name1
2 Name2
Now I need to select only those records from table one
which have either their id in table2 or in table3
I was trying to apply or operator witin inner join like:
select *
from table1
inner join table2 on table2.id = table1.id or
inner join table3 on table3.id = table1.id.
Is it possible? What is the best method to approach this? Actually I am also not able to use
if exist(select 1 from table2 where id=table1.id) then select from table1
Could someone help me to get over this?
Use left join and then check if at least one of the joins has found a relation
select t1.*
from table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
where t2.id is not null
or t3.is is not null
I would be inclined to use exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.id = t1.id) or
exists (select 1 from table3 t3 where t3.id = t1.id) ;
The advantage to using exists (or in) over a join involves duplicate rows. If table2 or table3 have multiple rows for a given id, then a version using join will produce multiple rows in the result set.
I think the most efficient way is to use UNION on table2 and table3 and join to it :
SELECT t1.*
FROM table1 t1
INNER JOIN(SELECT id FROM Table2
UNION
SELECT id FROM Table3) s
ON(t.id = s.id)
Alternatively, you can use below SQL as well:
SELECT *
FROM dbo.Table1
WHERE id Table1.IN ( SELECT table2.id
FROM dbo.table2 )
OR Table1.id IN ( SELECT table3.id
FROM Table3 )
I'm doing something like:
SELECT T1.NAME, T2.DATE
FROM T1
INNER JOIN
(
SELECT * FROM OTHERTABLE
) AS T2 ON T2.USERID = T1.USERID
Which works, but if I query the alias table, I get an error saying that T2 is an invalid object name.
Example:
SELECT
T1.NAME,
T2.DATE,
CASE
WHEN EXISTS (SELECT TOP 1 1 FROM T2 WHERE T2.THISFIELD = T1.THISFIELD) THEN 'HELLO'
ELSE 'BYE'
END AS COMMENT -- THIS ALSO FAILS
FROM T1
INNER JOIN
(
SELECT * FROM OTHERTABLE
) AS T2 ON T2.USERID = T1.USERID
WHERE (SELECT COUNT(*) FROM T2) > 0
I thought that's what I did, "create" T2. Is there any way I can use T2 like such ?
My goal is to scrape all the related data from OTHERTABLE once because I'll have many CASE in the SELECT clause depending whether data exists in T2 or not. I don't want to do EXISTS for every field since that'll launch a new query in a huge table everytime.
Your query using a sub-query of SELECT * FROM OTHERTABLE which doesn't make sense. You can modify it like;
SELECT
T1.NAME,
T2.DATE,
...
FROM T1
JOIN OTHERTABLE T2 ON T2.USERID = T1.USERID
WHERE (SELECT COUNT(*) FROM OTHERTABLE ) > 0
You cannot use a sub-query multiple times in the same query. Instead use a Common Table Expression (CTE) for that purpose. T2 is a CTE in the following example.
;WITH T2 AS
(
SELECT UserId, col1, col2, [Date]
FROM OtherTable
)
SELECT T1.NAME, T2.DATE
FROM T1
JOIN T2 ON T2.USERID = T1.USERID
WHERE (SELECT COUNT(*) FROM T2) > 0
How can I display data from table1 where its id (Table1 Id)is not contained in table2
string Query = "SELECT * FROM Table1 WHERE Table1ID!=" Table2_Table1ID;
You can do:
SELECT * FROM table1
WHERE table1ID NOT IN (SELECT table1ID FROM table2);
SELECT * FROM TABLE1
WHERE NOT EXISTS(SELECT Table1ID from Table2 where Table1ID=Table1.Id)
You can use a LEFT OUTER JOIN:
SELECT T1.*
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T2.Table1ID = T1.Table1ID
WHERE T2.Table1ID IS NULL
This will attempt to match records between Table1 and Table2 using the Table1ID value and only pull those where Table2.Table1ID is NULL (no match was found).
You can try this
SELECT * FROM table1 WHERE able1ID NOT IN (SELECT table2id FROM table2)
how can I rewrite the following query using JOIN
SELECT *
FROM table1
WHERE id NOT IN
(
SELECT t1Id
FROM table2
);
SELECT *
FROM table1 t1
left outer join table2 t2 on t1.id=t2.id
where t2.id is null
SELECT * FROM table1 t1
WHERE NOT EXISTS(
SELECT *
FROM table2 t2
Where t1.Id = t2.t1Id);