title: SQL Server Left Join with Order BY
body: <p>I have a Left Join SQL query (2012) that reports a 00:00:00 second execution time without the Order By clause at the bottom. Adding the "Order By" makes the query take 00:00:24 seconds!</p>
  
  <p>Is this normal?</p>
  
  <p>It's pretty clear that I'm missing some fairly fundamental concept here but I would have assumed that the query is performed and then the results sorted. Apparently this is not the case, please could anyone point me in the right direction?</p>
  
  <p>A little other info: The two tables I'm joining have about 35,000 rows and 22,000 rows respectively. I'm selecting a date range and the left join is happening on two strings. The result set is between 300 - 400 results. I know that there's a massive penalty comparing strings and I'm guessing that's part of the problem here. </p>
  
  <p>Here is the SQL:</p>
  
  <pre><code>DECLARE @mystartdate as datetime
  SET @mystartdate = CAST(CAST((GETDATE() - 0) AS date) as nvarchar) + ' 06:00:00'
  
  DECLARE @myenddate as datetime
  SET @myenddate = CAST(CAST((GETDATE() + 1) AS date) as nvarchar) + ' 05:59:59'
  
  SELECT SUBSTRING(CAST([Event_Time] AS nvarchar),0,9) AS mytime
  ,[CombinedDateTime]
  ,[MediaFileName]
  ,[Duration]
  ,[High_Definition]
  
  FROM [dbo.schedules]
  
  LEFT JOIN dbo.subtitles
  ON REPLACE(SUBSTRING(dbo.subtitles.sub_filename, 1, LEN(dbo.subtitles.sub_filename) -4), '-', '') = REPLACE(dbo.schedules.MediaFileName,'/','')
  
  WHERE [CombinedDateTime] &gt;= @mystartdate AND [CombinedDateTime] &lt;= @myenddate
  
  Order by CombinedDateTime
  </code></pre>
  
  <p>Thanks</p>