Useful queries

Find duplicated username

User.find_by_sql('
SELECT username, COUNT(id)
FROM users
GROUP BY username
HAVING COUNT(id) > 1 ORDER BY COUNT(id)').pluck(:username)