title: Alternate way for outer apply in SQL Server
body: <p>I have a query in SQL server in which I have used outer apply. Now I want to convert it so that query can run on SQL server and MySQL also.</p>
  <pre><code>select top 5 v.sVehicleName as VehicleNo, ll.Location 
  from vehicles v
  outer APPLY
       (select top 1 Location 
        from location_history 
        where vehicle_id = v.vehicle_id) ll
  </code></pre>
  <p>I have to covert this query so I can run on both databases.</p>
  <p>This is my tables</p>
  <pre><code>create table #vehicles (vehicle_id int, sVehicleName varchar(50))
  
  create table #location_history ( vehicle_id int, location varchar(50), date datetime)
  
  insert into #vehicles 
  values
  (1, 'MH 14 aa 1111'), 
  (2,'MH 12 bb 2222'),
  (3,'MH 13 cc 3333'),
  (4,'MH 42 dd 4444')
  
  insert into #location_history
  values
   ( 1, 'aaa', getdate()),
   ( 1, 'bbb', getdate()),
   ( 2, 'ccc', getdate()),
   ( 2, 'ddd', getdate()),
   ( 3, 'eee', getdate()),
   ( 3, 'fff', getdate()),
   ( 4, 'ggg', getdate()),
   ( 4 ,'hhh', getdate())
  </code></pre>
  <p>This is query which I execute in SQL server.</p>
  <pre><code>select v.sVehicleName as VehicleNo, ll.Location 
  from #vehicles v
  outer APPLY
    (select top 1 Location 
        from #location_history 
        where vehicle_id = v.vehicle_id) ll
  </code></pre>
  <p>This is output in SQL server.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>VehicleNo</th>
  <th style="text-align: center;">Location</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>MH14 aa 1111</td>
  <td style="text-align: center;">aaa</td>
  </tr>
  <tr>
  <td>MH12 bb 2222</td>
  <td style="text-align: center;">ccc</td>
  </tr>
  <tr>
  <td>MH13 cc 3333</td>
  <td style="text-align: center;">eee</td>
  </tr>
  <tr>
  <td>MH42 dd 4444</td>
  <td style="text-align: center;">ggg</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I want to execute this in MySQL. and I want same output mentioned above.</p>