I need to fetch data from two tables
For example: I have tables test1, test2. Test1 is the parent table. I need to fetch data is that present in test1 but not in test2. How can I write a query for that?
select id from test1, test2 where test1.id not in (test2.id);
Do an LEFT OUTER JOIN: this will, for each line in table 1 find the corresponding line(s) in table2 – but the LEFT OUTER will cause it to return the line with NULLs for the table2 values when no values are found. Then, just ask those where the table2 fields are NULL (hence do not exist).
You could try a subquery too, but this will not perform better (usually worse):