Use of if exists( ) in select statement - sql-server

I need to fetch a column from table2, if row exists, if not return it as null.
If i use ,case when it fetches only matched rows between table1 and table2.
If i use left outer join it fetches all the rows from table1 even though
condition table1.code='A'
So i need ,some thing like this.
select table1.id,
if(row exist in table2 for query(table2.relation_type_id=55 and table1.id=table2.related_id)
then
return table2.parent_id
else
null
as parent_id,
table1.description,
from table1,table2 where table1.code='A'

SELECT table1.id, table2.parent_id as parent_id
FROM table1
LEFT OUTER JOIN table2 ON (table1.id = table2.related_id)
WHERE table1.code = 'A';
EDIT based on comment :
SELECT table1.id, sub.parent_id as parent_id
FROM table1
LEFT OUTER JOIN (select parent_id,related_id from table2 where relation_type_id =55) sub ON (table1.id = sub.related_id)
WHERE table1.code = 'A';

Have you tried a sub-query?
select table1.id,
(select table2.parent_id from table2 where table2.relation_type_id=55 and table2.related_id=table1.id) parent_id,
table1.description,
from table1 where table1.code='A'

Related

Join three Tables with 2 different Columns

Here is the thing:
I have three tables:
Table1:
COL1 COL2
Table2:
COL2 COL3
Table3:
COL3 COL4
And I want to select the COL4 from Table3 when Table.COL3 = TABLE2.COL3, which the Table2.COL3 from when Table2.COL2 = Table1.COL2
It likes two join table, but when I use the following query it doesn't work.
SELECT * FROM
table3
INNER JOIN table2 ON table3.col3
=
(
SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2
)
You are selecting from table1 joining to the others. So table1 goes in the from.
SELECT TABLE3.col4
FROM TABLE1
JOIN TABLE2 ON TABLE2.COL2 = TABLE1.COL2
JOIN TABLE3 ON TABLE3.COL3= TABLE2.COL3
In the first table in the join statement. You have used a column name i.e. table3.col4 in stead of a table name i.e. table3. So, it's giving error.
Your correct query will be as follows
SELECT table3.col4 FROM table3
INNER JOIN table2
ON table3.col3 =
( SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2)

Find rows only present in the result of one of two queries

I have two JOIN queries which give an output column of Id. How do I find all the Id values returned by query 1 but not returned by query 2?
select Id from Table1 join Table2;
select Id from Table2 join Table3;
SELECT id FROM dbo.Table1 INNER JOIN dbo.Table2 ON ...
EXCEPT
SELECT id FROM dbo.Table2 INNER JOIN dbo.Table3 ON ...;
try this one:
select Id from Table1 join Table2
union
select Id from Table2 join Table3

Using where condition in sql query

I have an sql query like this
Select col1, (select abc from table2 where def=1) as col2
From Table1 inner join table3 on Table1.id = table3.id
Where col2 = 4
The problem is that the where condition doesn't work. I get an error saying
Invalid column name 'col2'
Kindly help me fix this sql query.
Thanks in advance
You can define it in a CROSS APPLY and then reference in the SELECT and WHERE
SELECT col1,
col2
FROM Table1
INNER JOIN table3
ON Table1.id = table3.id
CROSS APPLY (SELECT abc
FROM table2
WHERE def = 1) C(col2)
WHERE col2 = 4
Using a CTE (Common Table Expression):
WITH SubQuery AS (Col2) {
SELECT
ABC
FROM
table2
WHERE
def = 1
}
SELECT
T.Col1,
S.Col2
FROM
SubQuery S,
Table1 T
INNER JOIN table3 t3
ON T.id = t3.id
WHERE
S.Col2 = 4
Although I must say I agree with the first comment - this makes no sense since your subquery is not correlated (joined) to the rest of your query...

Opposite Of An Inner Join Query

Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);

Sql server DELETE and WITH clause

I need to build an SQL statement to delete from certain table the records that match another select statement.
In Teradata we use
delete from table1
where (col1, col2) in (
select col1,col2
from table2
)
While in SQL Server it's not allowed to have more than 1 column in the WHERE..IN clause. I thought I can use the WITH clause:
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete from table1
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
How to use WITH..DELETE clause? Is there another way?
This should do it:
DELETE Table1
from Table1 t1
inner join tempTable t2
on t2.Col1 = t1.Col1
and t2.Col2 = t1.Col2
First build a query that selects the rows you need:
SELECT t1.*
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col2]=t2.[Col2]
Test it to make sure it returns exactly the rows you want to delete. Then turn it into a delete statement by changing the "SELECT" to "DELETE" and removing the column list:
DELETE t1
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col
delete from table1 t1 where exists
(
select 1 from table2 t2 where t1.col1 = t2.col1 and t1.col2 > t2.col2
)
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete table1 from tempTable
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
This works for me
WITH CTE AS
(
SELECT TOP 50000 *
from v020101hist order by data
)
DELETE FROM CTE

Resources