I have a requirement where I have to fetch data from database table.
There are two tables with identical columns like
Ex. table names like Student and Teacher.
now if I don't find anything in student table then I will retrieve data from Teacher table.
how to write this query ?
To get results from the two tables you would use UNION ALL. Here however you only want to access table Teacher when there are no matching rows in table Student. This is slightly more difficult. You can still use UNION ALL, but need an EXISTS clause on your teacher query.
select * from student union all select * from teacher where not exists (select * from student);