Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to find rows that aren't present in other tables

Tags:

sql

mysql

Here's what I'm trying to accomplish:

I've got two tables, call them first and second. They each have an ID column. They might have other columns but those aren't important. I have a third table, call it third. It contains two columns, ID and OTHERID. OTHERID references entries that may or may not exist in tables first and second.

I want to query third and look for rows who don't have an OTHERID column value that is found in either tables first or second. The goal is to delete those rows from table third.

Example:

first table:

ID
1
2
3

second table:

ID
6
7
8

third table

ID  | OTHERID
21        1
22        2
23        3
24        4
25        5
26        6
27        7
28        8

In this case, I'd want to retrieve the IDs from third who don't have a matching ID in either table first or table second. I'd expect to get back the following IDs:

24
25

What I've tried:

I've done something this to get back the entries in third that aren't in first:

select t.* from third t where not exists (select * from first f where t.otherid = f.id);

and this will get me back the following rows:

ID  | OTHERID
24        4
25        5
26        6
27        7
28        8

Similarly, I can get the ones that aren't in second:

select t.* from third t where not exists (select * from second s where t.otherid = s.id);

and I'll get:

ID  | OTHERID
21        1
22        2
23        3
24        4
25        5

What I can't get my brain about this morning is how to combine the two queries together to get the intersection between the two results sets, so that just the rows with IDs 24 and 25 are returned. Those would be two rows I could remove since they are orphans.

How would you solve this? I think I'm on the right track but I'm just spinning at this point making no progress.

like image 724
itsmatt Avatar asked Jun 25 '10 13:06

itsmatt


People also ask

How do you get rows which are not present in another table?

We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

How do you check if a record exists in another table SQL?

The SQL EXISTS Operator The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.

How do you select all records from one table that do not exist in another table in Excel?

select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. SQL> select e. select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. select column_name from table 1 full outer join table 2 on(connection); here all the data from table 1 and table 2 will get retrieved.


3 Answers

Maybe this :

SELECT third.*
FROM third
LEFT JOIN first ON third.otherID = first.id
LEFT JOIN second ON third.otherID = second.id
WHERE first.id IS NULL AND second.id IS NULL
like image 115
Serty Oan Avatar answered Oct 20 '22 13:10

Serty Oan


Just use

select t.*
from third t
where 
       not exists (select * from first f  where t.otherid = f.id)
  and  not exists (select * from second s where t.otherid = s.id)
like image 34
Aaron Digulla Avatar answered Oct 20 '22 13:10

Aaron Digulla


SELECT t.ID
FROM third t
WHERE t.OTHERID NOT IN (
    SELECT ID
    FROM first

    UNION

    SELECT ID
    FROM second
)
like image 4
SAGExSDX Avatar answered Oct 20 '22 14:10

SAGExSDX