title: SQL Server : joining two tables
body: <p>I am trying to join two tables together to display the information in the following way.</p>
  
  <pre><code>ServerName DatabaseName MessageText RestoreTime BackupTime Servername
  </code></pre>
  
  <p>However I am running into a problem, I am getting multiple records. The result should only show 31 records however I am getting 961 records. I believe that I did not write the following script correctly and would appreciate some help with it.</p>
  
  <pre><code>select * 
  from 
      (SELECT 
           ServerName, DatabaseName, MessageText,
           [datetime] as RestoreTime 
       FROM 
           [dbo].[CheckDBHist]
       WHERE 
           datetime &lt; (SELECT getdate()) 
           AND datetime &gt; (SELECT getdate() - 1) 
           AND ServerName = 'servername' 
      ) allnames
  inner join
      (select 
           BackupStartDate, ServerName
       from [dbo].[DBBackupFileDate]
       where BackupStartDate &lt; (SELECT getdate()) 
         and BackupStartDate &gt; (SELECT getdate()-1)
      ) servern on servern.ServerName = allnames.ServerName
  order by 
      RestoreTime desc
  </code></pre>