title: Running a SQL delete query in Laravel
body: <p>I have an SQL query:</p>
  
  <pre><code>DELETE n1 
    FROM satellites n1
       , satellites n2 
   WHERE n1.id &lt; n2.id 
     AND n1.norad_cat_id = n2.norad_cat_id
  </code></pre>
  
  <p>What this query does is delete rows that have the same <code>norad_cat_id</code> and only leave one with the highest <code>id</code>. I don't know if my SQL query is correct, but I will have to see. </p>
  
  <p>I am a bit stuck when it comes to running raw SQL queries in Laravel. From this documentation (<a href="https://laravel.com/docs/5.4/database#running-queries" rel="noreferrer">https://laravel.com/docs/5.4/database#running-queries</a>) you can see that you have a few options to run the query:</p>
  
  <p><code>DB::update('SQL QUERY HERE');</code></p>
  
  <p><code>DB::delete('SQL QUERY HERE');</code></p>
  
  <p><code>DB::statement('SQL QUERY HERE');</code></p>
  
  <p><code>DB::select( DB::raw('SQL QUERY HERE'));</code></p>
  
  <p>In my case I am trying to delete duplicate rows while only leaving the one with the highest <code>id</code>. What Laravel <code>DB</code> statement do I run to achieve the results I want or does it matter at all? </p>
  
  <p><strong>EDIT:</strong> SQL query for @MasudMiah</p>
  
  <p><code>delete satellites
     from satellites
    inner join (
       select max(id) as lastId, norad_cat_id
         from satellites
        group by norad_cat_id
       having count(*) &gt; 1) duplic on duplic.norad_cat_id = satellites.norad_cat_id
    where satellites.norad_cat_id &lt; duplic.lastId;</code></p>