0

For example we have the following SQL query

select T1.name
from table1 T1
join table2 T2 ON T1.id = T2.id

I have made join table2 only if it is NOT empty. If table2 is empty I don't make join

select T1.name
from table1 T1

Can I solve it by one SQL query? It would be good to use only SQL standard.

Nick
  • 109
  • 4
  • 11
  • left join or inner join? you have both tags. – Blorgbeard Sep 22 '14 at 07:53
  • Possible duplicate of http://stackoverflow.com/questions/14195117/t-sql-join-table-only-when-the-table-is-not-empty – Oasis Sep 22 '14 at 07:54
  • you need to use `left outer join`. Try `select T1.name from table1 T1 left outer join table2 T2 ON T1.id = T2.id;` – bprasanna Sep 22 '14 at 07:57
  • I have to use inner join. Oasis is right. This is duplicate question. To resolve it I use `EXISTS` and `OR NOT EXISTS` – Nick Sep 22 '14 at 08:00

0 Answers0