I am trying to join 2 tables and trying to fetch the records, which do not have values in the join column of the 2nd table.
For example:
Table 1 Id column values:
1,
2,
3,
4,
Table 2 Id column values:
1,
3,
Given the above example of the values in the join columns of the 2 tables, I want to fetch records from table1 with ids 2 and 4, because they are not present in table2.
Any help would be much appreciated.
My SQL has gotten rusty to the introduction of JPA frameworks, but today I cannot run away from not knowing it, it seems :(
Thanks!
select t1.id
from Table1 t1
left outer join Table2 t2 on t1.id = t2.id
where t2.id is null
SELECT * FROM table1 WHERE table1.id NOT IN (SELECT id from table2)
NOT EXISTS variant:
SELECT * FROM table1 WHERE NOT EXISTS
(SELECT NULL from table2 WHERE table2.id = table1.id)
Related
I'm currently turning a comma-separated string into a number table with the field name of ID. I'm then trying to do an nvl to select all if the generated table is null.
table1.ID = NVL(table2.ID, table1.ID)
I have two tables and need to filter table1 with results from table2. I need to return all of table1 if table2 is empty.
Scenario I
Table1
ID
1
2
3
4
Table2 (Empty)
ID
Return rows 1, 2, 3, 4
Scenario II
Table1
ID
1
2
3
4
Table2
ID
2
3
Return rows 2, 3
You can use filtering in the where clause:
select t1.id
from table1 t1
where not exists (select 1 from table2) or
exists (select 1 from table2 t2 where t2.id = t1.id);
I don't think join is the right way to express this logic.
You can also use UNION
select t1.id
from table1 t1
where not exists (select 1 from table2 where id = t1.id) union all
select t2.id
from table2 t2
where exist (select 1 from table1 where id = t2.id);
The task is: to join two tables, but not just by columns. Suppose there are tables t1 and t2. In t1, the columns {id, namet1}, t2 - {id, id_t1_with000, namet2}.
Example
t1.id:
1
2
3
....
Example
t2.id_t1_with000:
100
200
300
....
Problem: how to connect the tables t1 and t2 with t1.id and t2.id_t1_with000.
I thought that it is possible so:
SELECT * FROM t1 JOIN t2 on t1.id = t2.id_t1_with000 [3]
How can such a connection be made? How about this google? nothing I could not find.
You could join the tables using a substring.
example:
select * from table1 t1
join table2 t2 on substring(cast(t2.id as varchar), 1, len(t2.id)-2) = t1.id
With help of the SubString you can simply extract the ID from all those zero's.
"len(t2.id)-2" because you have 2 zero's behind your ID.
This only works if your id is always filled with the same amount of zero's
I have two tables.
Table1
ID Text
Table2
ID ParentID Text
I am trying to join to the same table twice using 2 different columns. I want all the rows where Table1.ID = Table2.ID as well as all the rows where Table1.ID = Table2.ParentID. I've tried the following but it returns no rows. When I run the script below in two different select statements each with one of the joins only, I get the result I want but not in the same select statement. Any ideas on what I'm doing wrong?
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = Table1.ID
JOIN Table2 22
on 22.ParentID = Table1.ID
You're only getting the records where Table 1 is both a Parent ID and an ID... maybe you want:
SELECT
* FROM Table 1
JOIN Table2 2 on 2.ID = Table1.ID
UNION
SELECT
* FROM Table 1
JOIN Table2 22 on 22.ParentID = Table1.ID
Or potentially UNION ALL?
You could try this query:
SELECT
*
FROM
TABLE1,
TABLE2
WHERE
TABLE1.ID = TABLE2.ID
OR
TABLE1.ID = TABLE2.ParentID
Your aliasing is a little off.
Give this a go:
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = 1.ID
JOIN Table2 22
on 22.ParentID = 1.ID
You may still get an error on the SELECT * by not having an alias there... I suggest doing SELECT 1.*, or even doing SELECT 1.myColumn, 1.myColumn2 etc...
there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2
consider the following example.
I have to select all the records from table1 which exist in table2 also + all the records from table2 which don't exist in table1 but exist in table2 and have IsActive=1 and status not null.
I initially tried it with a join but how to do the later part where I have to select the records which don't exist in table 1 ? I have to do it inside a single query presumably with a SQL view.
Edit
I need to combine the results like a UNION of 2 tables, so incase of rows absent in table1 but present in table2, the columns of belonging to table1 would be blank.
Here's an example query:
select *
from Table2 t2
left join
Table1 t1
on t1.id = t2.id
where t1.id is not null
or (isActive = 1 and status is not null)
The first line of the where clause takes care of "all the records from table1 which exist in table2". The second line is for "don't exist in table1 but exist in table2 and have IsActive=1 and status not null".
You will need an outer join here.
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Is this it? Not sure if I got right what you want to do.
SELECT
*
FROM
Table1 t1
JOIN
Table2 t2 ON (t1.ID = t2.ID OR (t1.ID IS NULL AND t2.isActive = 1 AND t2.Status IS NOT NULL))