sql: select not in - sql

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);

Related

SQL — Union two tables but Join on one of the columns

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);

SQL Server why Incorrect syntax near the keyword 'select'

select *
from
(select *
from
(select t1.stdid
from t1) as table1
cross join
select *
from
(select t2.Subid
from t2) as table2
) as table3
Try the query below. You included an addition select * from that you should have left out.
select *
from (
select *
from (
select t1.stdid
from t1
) as table1
cross join
-- select * from : This line is extraneous and is causing your error.
(
select t2.Subid
from t2
) as table2
) as table3
Alternatively, you will get the same result if your query was written as:
select *
from (
select t1.stdid
from t1
) as table1
cross join
(
select t2.Subid
from t2
) as table2
Please don't use * in the query. Just select the column which you want.Please see below:
select ID
from
(select t1.id as ID
from
(select t1.id
from t1) as t1
cross join
(select t2.id
from t2) as t2
) as table3

How to get Top 1 with Sort from Join

Instead of doing something like this:
SELECT
t1.*,
(SELECT TOP 1 t2.Name FROM Table2 t2 Order By t2.Number) Val1
FROM Table1 t1
How would it be done with a Join instead?
SELECT
t1.*,
TOP 1 t2.Name
FROM Table1 t1
JOIN Table2 t2 Order By t2.Number
or is it even possible?
select top 1 * from (SELECT t1.*, t2.Number num FROM table1 t1 JOIN table2 t2 on t1.commoncolumn=t2.commoncolumn ) t order by t.num
Try this (id is the join field)
with Table2Data as
(
SELECT Id , Name ,row_number() over(partition by Id order by number) rowNumber FROM Table2
)
select t1.* ,t2.name from table1 t1
inner join Table2Data t2 on t1.Id=t2.Id and t2.rowNumber=1
The following accomplishes the same thing.
SELECT t1.*, MIN(t2.Name) OVER() val1 FROM Table1 INNER JOIN Table2 t2 on t1.id = t2.id
It can be done with a CROSS APPLY. Assuming Id is the common field:
SELECT t1.*
, t3.Name
FROM Table1 t1
CROSS APPLY (
SELECT TOP 1 t2.Name
FROM Table2 t2
WHERE t1.Id = t2.Id
ORDER BY t2.Number
) t3

How to use outside column in subquery

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

display data from table1 where its id (Table1 Id)is not contained in table2

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)