-1

I have 2 tables.

table1

----------------------------------------
oid || oname     || desc      || creator
----------------------------------------
101    name101      desc101      cr101          
102    name102      desc102      cr102        
103    name103      desc103      cr103      
104    name104      desc103      cr104       
105    name105      desc105      cr105        

table2

----------------------------------------
sid || sname     || sloc      || oid
----------------------------------------
s101    sname101     sloc101      101       
s102    sname102     sloc102      102       
s103    sname103     sloc103      103         

Now I want the records from table1 which are not available in table 2.

104    name104      desc103      cr104                  
105    name105      desc105      cr105                 

What should be the sql query?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
user2636874
  • 889
  • 4
  • 15
  • 36
  • This is a faq. Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Sep 11 '19 at 06:46
  • Possible duplicate of [How to find rows in one table that have no corresponding row in another table](https://stackoverflow.com/questions/1415438/how-to-find-rows-in-one-table-that-have-no-corresponding-row-in-another-table) – philipxy Sep 11 '19 at 06:59

3 Answers3

0

You can use not exists:

select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.oid = t1.oid);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0
SELECT t1.* FROM table1 t1 WHERE t1.oid NOT IN(SELECT t2.sid FROM table2 t2);
0

Use an OUTER JOIN:

SELECT t1.*
  FROM TABLE1 t1
  LEFT OUTER JOIN TABLE2 t2
    ON t2.OID = t1.OID
  WHERE t2.OID IS NULL
  ORDER BY t1.OID

dbfiddle here