I have three tables Table1, Table2 and Table3 and the following query which deletes rows in Table1
delete from Table1
where EXISTS
(select (1) from Table2
where Table1.col1=Table2.col1
AND Table1.col2=Table2.col2
AND Table1.col3=(select **Table3.col3 from Table3** inner join Table2 on Table3.col1=Table2.col1)
Is this query correct? If not, how to use a third table inside the where condition?
Edit : Also, please explain how to rewrite the query if we want to delete the rows from table2 which itself is joined with table3?
Here's one way to do it:
delete from table1
where (col1, col2, col3) in (
select t1.col1, t1.col2, t1.col3
from table1 t1
join table2 t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2
join table3 t3 on t1.col3 = t3.col3 and t2.col1 = t3.col1
);
SQL Fiddle Demo
Or using EXISTS might be faster:
delete table1
where exists (
select *
from table2
join table3 on table2.col1 = table3.col1
where table1.col3 = table3.col3 and
table1.col1 = table2.col1 and
table1.col2 = table2.col2);
Related
I am trying to use the below query, its everytime saying cannot insert null for the second column.
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2
WHERE t1.id = t2.id);
Try this:
UPDATE TABLE1 SET (COL1,COL2,COL3) =
(SELECT COL1,NVL(COL2,0),COL3 FROM TABLE1 T1, TABLE2 T2 where t1.id = t2.id)
WHERE <YOUR WHERE CONDITION>
I would use a MERGE:
MERGE INTO TABLE1 t1
USING (SELECT id, COL1,COL2,COL3
FROM TABLE2) t2
ON (t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET t1.COL1 = t2.COL1
, t1.COL2 = t2.COL2
, t1.COL3 = t2.COL3;
There are rows in table1 where not corresponding row exists in table2 thus the sub-query returns nothing which means the UPDATE statement will take that as null for all three columns.
You have to add a WHERE clause that only updates rows in table1 that do have a matching row in table2
UPDATE TABLE1
SET (COL1,COL2,COL3) = (SELECT COL1,COL2,COL3
FROM TABLE2 t2
WHERE table1.id = t2.id)
where exist (select *
from table2 t2
where table1.id = t2.id);
There is a need in the SQL code below will also get the table name from which the column was fetched to maintain a lineage for analysis at later point. I need suggestion to implement such a SQL:
select
COALESCE(t1.col1,t2.col1,t3.col1) new_col1,
COALESCE(t1.col2,t2.col2,t3.col2) new_col2,
COALESCE(t1.col3,t2.col3,t3.col3) new_col3
from
table1 t1
left join table2 t2 on t1.id = t2.id
left join table3 t3 on t1.id = t3.id
In the result, I need to get an output similar to this:
new_col1 new_col2 new_col3 new_col1_source new_col2_source new_col3_source
val1 val2 val3 table1 table1 table3
in the above result, the last 3 columns should provide the table names from which the first 3 columns were fetched from.
You can do this:
select
COALESCE(t1.col1,t2.col1,t3.col1) new_col1,
COALESCE(t1.col2,t2.col2,t3.col2) new_col2,
COALESCE(t1.col3,t2.col3,t3.col3) new_col3,
case when t1.col1 is not null then 'table1'
when t2.col1 is not null then 'table2'
when t3.col1 is not null then 'table3' end as new_col1_source,
case when t1.col2 is not null then 'table1'
when t2.col2 is not null then 'table2'
when t3.col2 is not null then 'table3' end as new_col2_source,
case when t1.col3 is not null then 'table1'
when t2.col3 is not null then 'table2'
when t3.col3 is not null then 'table3' end as new_col3_source
from
table1 t1
left join table2 t2 on t1.id = t2.id
left join table3 t3 on t1.id = t3.id
I'm not saying it's elegant. On the contrary, combining data and metadata in a single query inevitably results in clunkiness.
This is my statement
select * from table1
where col1 not in (select col2 from table2);
I want to try(but failed)
select t1.* from table1 t1
inner join table2 on t1.col1<> t2.col2
Is there another way to do this?
select t1.* from table1 t1
left join table2 t2 on t1.col1 = t2.col2
where t2.col2 is null
This is standard implementation of missing record search.
To get the records that exists in table1 but does't exists in table2 you need a left join, not an inner join.
Try this:
select t1.*
from table1 t1
left join table2 t2 on t1.col1 = t2.col2
where t2.id is null -- any non-nullable column will do, best to use pk for performance.
You have not defined t2 object and use left join.
SELECT t1.* FROM table1 t1
LEFT JOIN table2 t2 ON t2.col2 = t1.col1 WHERE t2.id IS NULL
If You try to find out the data of t1.c1 (t1=table1, c1=column1) that is exist or not exist in t2.c2 then use flowing script
select t1.c1 from t1 left join t2 on t1.c1=t2.c2
other wise if you want to find only t1.c1 data that is not in t2.c2 then below script is help full
select t1.c1 from t1 where t1.c1 not in (select t2.c2 from t2)
I have two sql in netezza:
1.
SELECT T1.Col1, T2.Col2 FROM TableA T1 JOIN TableB T2 ON T1.Col3 = T2.Col3
2.
SELECT T1.Col1, T2.Col2 FROM TableA T1 FULL OUTER JOIN TableB T2 ON T1.Col3 = T2.Col3
I assume that 2 should return more or equal results as 1. However, results from 1 is more than 2.
Could someone help me understand why?
I am working on SQL 2008. I have two identical tables with same column names.
On Table2, i am missing some records. Some records got deleted in the Table2.
I have to compare Table1 and Table2 and retrieve only missing records from table1.
Use a LEFT JOIN and check for IS NULL like below. where t2.col2 is null will be TRUE only when there are records in table1 which are not present in table2. Which is what you are looking for. [This is a sample code and have no resemblance with your original query]
select t1.col1,t1.col2,t1.col3
from table1 t1
left join table2 t2 on t1.some_column = t2.some_column
where t2.col2 is null
You should use SQL Except. There is no Join involved.
Select * from dbo.TableA
Except
Select * from dbo.TableB
In set theory, the difference of sets A, B (A-B) is the set of elements that belong to A and do not belong to B.
With an " not exists", you have a solution :
select * from Table1 t1 where not exists (select 1 from Table2 t2
where t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
and ... // check here all columns ...
)
There is however a little problem in this solution in the case of null values, which can only be tested via a "IS NOT NULL" or "IS NULL", hence the complementary solution:
select * from Table1 t1 where not exists (select 1 from Table2 t2
where (t1.col1 = t2.col1 or (t1.col1 IS NULL and t2.col1 IS NULL))
and (t1.col2 = t2.col2 or (t1.col2 IS NULL and t2.col2 IS NULL))
and (t1.col3 = t2.col3 or (t1.col3 IS NULL and t2.col3 IS NULL))
and ... // check here all columns ...
)