It is order by you should check here, not group by. http://www.postgresql.org/docs/9.3/static/sql-select.html#SQL-DISTINCT