title: Sql: select rows based on another table if is populated or empty
body: <p>I have two tables.
  First is called TempIds and it contains only one column called Id which is of type int</p>
  
  <p>TempIds:</p>
  
  <pre><code>Id
  45
  34
  77
  </code></pre>
  
  <p>The second one is called Countries which has two columns, Id and Name
  Countries:</p>
  
  <pre><code>Id      Name
  22      Austria
  45      Germany
  88      China
  12      Japan
  </code></pre>
  
  <p>What I want is to select all the rows from Countries table matching the Ids in the TempIds table. But the catch is, if TempIds is empty then the query should return all the rows from Countries.</p>
  
  <p>So far this is what I have tried:</p>
  
  <pre><code>select *
  from Countries c
  left join TempIds t on c.Id = t.Id or t.Id is null
  
  
  select *
  from Countries c
  left join TempIds t on c.Id = t.Id
  where (c.Id = t.Id or t.Id is null)
  </code></pre>
  
  <p>Both of the queries above do not return the correct records. My question is, how can I write a query that will return the correct result. That is, based on current TempIds table I should only get Germany. But if TempIds is empty, then I should get all countries from Countries table.</p>
  
  <p>Thanks is advance</p>