title: PostgreSQL - GROUP BY clause or be used in an aggregate function
body: <p>I found some topics here on SO, but I still can't find the right setup for my query.</p>
  
  <p>This is query, that works me well on localhost:</p>
  
  <pre><code>@cars = Car.find_by_sql('SELECT cars.*, COUNT(cars.id) AS counter 
                           FROM cars 
                           LEFT JOIN users ON cars.id=users.car_id 
                           GROUP BY cars.id ORDER BY counter DESC')
  </code></pre>
  
  <p>But on Heroku gives me the error above - <strong>GROUP BY clause or be used in an aggregate function</strong>.</p>
  
  <p>Then I have read somewhere, that I should specify all columns in the table, so I tried this:</p>
  
  <pre><code>@cars = Car.find_by_sql('SELECT cars.id, cars.name, cars.created_at, 
                                  cars.updated_at, COUNT(cars.id) AS counter 
                           FROM cars 
                           LEFT JOIN users ON cars.id=users.car_id 
                           GROUP BY (cars.id, cars.name, cars.created_at, cars.updated_at) 
                           ORDER BY counter DESC')
  </code></pre>
  
  <p>But this doesn't work on localhost and also not on Heroku...</p>
  
  <p>What should be the right config of the query?</p>